怎么返回String类型的啊. 我这样返回不对吗

import java.util.Scanner;

public class 计算机 {
    public static String[] jieguo(String[] shuzi) {
        Scanner in = new Scanner(System.in);
   // int zhi=Integer.parseInt(shuzi[0]);
        
        int chang = in.nextInt();
        int b=0;
        for(int i=0;i<chang;i++) {
            if(shuzi[i].equals("*")) {
                b=Integer.parseInt(shuzi[i-1]) *Integer.parseInt(shuzi[i+1]);
                String s=Integer.toString(b);
                String xin[]=new String [chang-2];
         
                for(int j=0;j<i-1;j++) {
                    xin[j]=shuzi[j-1];
                    xin[i]=s;
                }
                for(int n=i+2;n<xin.length;n++) {
                
                    xin[n]=shuzi[n+2];
                }
            }
            
        }return xin;
    }

2 条评论

  • @ 2019-06-10 20:01:42

    错误信息是这样的:

    3
    1 + 2 * 3
    Exception in thread "main" java.lang.NumberFormatException: null
    at java.base/java.lang.Integer.parseInt(Integer.java:620)
    at java.base/java.lang.Integer.parseInt(Integer.java:776)
    at 李成杰.计算机.jia(计算机.java:40)
    at 李成杰.计算机.zonghe(计算机.java:48)
    at 李成杰.计算机.main(计算机.java:54)

  • @ 2019-06-10 17:50:54

    你看了函数7.1节视频吗?做笔记了吗?

    • @ 2019-06-10 19:34:01

      刚又看了一遍

    • @ 2019-06-10 20:01:04
      import java.util.Scanner;
      /*找到第一个乘号的位置*/
      public class 计算机 {
          public static int weizhi(String[] a) {
              for (int i = 0; i < a.length; i++) {
                  if (a[i] == "*") {
                      return i;
                  } 
                  }
                  return 0;
          }
      /*创建乘完后的新数组*/
          public static String[] jieguo(String[] shuzi) {
              int chen = weizhi(shuzi);
              if (chen != 0) {
      
                  int a = 0;
                  String[] xin = new String[shuzi.length - 2];
                  a = Integer.parseInt(shuzi[chen + 1]) * Integer.parseInt(shuzi[chen - 1]);
                  String s1 = String.valueOf(a);
                  for (int i = 0; i < chen - 1; i++) {
                      xin[i] = shuzi[i];
                  }
                  xin[chen - 1] = s1;
                  for (int i = chen + 2; i < xin.length; i++) {
                      xin[i] = shuzi[i + 2];
                  }
                  return jieguo(xin);
              } else {
                  return shuzi;
              }
      
          }
          /*剩下的加法*/
          public static int  jia(String[] abc) {
              int zhi=0;
              for (int i =0;i<abc.length;i+=2) {
                  zhi=zhi+Integer.parseInt(abc[i]);
              }
              return zhi;
          }
      /*先乘后加*/
          public static int zonghe(String[]suan) {
              String[]m =new String[suan.length];
              m=jieguo(suan);
              return jia(m);
          }
          /*调用总函数*/
          public static void main(String[] args) {
              Scanner in = new Scanner(System.in);
              int n = in.nextInt();
              String []a=new String [n];
              zonghe(a);
          }
      }
      
      
  • 1