这个代码哪里错了,试了很多的算式都没有错

import java.util.Scanner;
import java.io.PrintStream;

public class wb {
    public static int jia(String[] a, int wz, int deShu) {
        int de = 0;
        int c = Integer.parseInt(a[wz + 1]);
        de = deShu + c;
        return de;
    }

    public static int xianChen(String[] a, int wz, int deShu) {
        int de = deShu;
        int c = Integer.parseInt(a[wz + 1]);
        de *= c;
        return de;
    }

    public static int chen(String[] a, int wz, int deShu) {
        int de = deShu;
        int b = Integer.parseInt(a[wz - 1]);
        int c = Integer.parseInt(a[wz + 1]);
        de += (b * c);
        return de;
    }

    public static int suanShu(String[] a) {
        int deShu = Integer.parseInt(a[0]);
        for (int i = 1; i < a.length; i += 2) {
            if (a[i].equals("+") == true) {
                if (a.length > i + 2 && a[i + 2].equals("+") == true) {
                    deShu = jia(a, i, deShu);
                    continue;
                }
                if (a.length > i + 2 && a[i + 2].equals("*") == true) {
                    deShu = chen(a, i + 2, deShu);
                    i += 2;
                    continue;
                } else {
                    deShu = jia(a, i, deShu);
                }
            } else if (a[i].equals("*") == true) {
                deShu = xianChen(a, i, deShu);
            }
        }

        return deShu;
    }

    public static void main(String[] args)
            throws java.io.UnsupportedEncodingException {
        PrintStream ps = new PrintStream(System.out, true, "UTF-8");
        Scanner in = new Scanner(System.in, "UTF-8");

        // 请从下一行开始你的程序
        int n = in.nextInt();
        String[] suZu = new String[n + (n - 1)];
        for (int i = 0; i < suZu.length; i++) {
            suZu[i] = in.next();
        }
        System.out.println(suanShu(suZu));
    }
}

1 条评论

  • @ 2019-06-05 11:27:18

    注意:这次算是给你重大提示了,希望以后你能自己认真思考和debug。

    1. 变量名非常好。既应用了驼峰命名法,又尽量起有助于理解的名字,大家应该向你学习!
    2. 试试1 + 2 + 3 + 4 * 5 + 6 * 7 * 8 + 9 + 10
  • 1