有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java正在跳过运行for循环

static int bookNum;

在此之前,我在其他代码中对bookNum做了一些操作,因此它有一个值,并且我在开始时已经定义了它

public static void processtrans()throws FileNotFoundException{

  Scanner input = new Scanner(transFile);
  String line = input.nextLine();

  double dollar = 0;
  int transNum = 1;

  while (input.hasNextLine()) {
     int Space = line.indexOf (" ");
     int Space2 = line.indexOf (" ", Space + 1);
     String action = line.substring(0,Space);

     if (action == "ORDER"){
        int Space3 = line.indexOf (" ", Space2 + 1);
        String isbn = line.substring(Space + 1, Space2);
        int num = Integer.parseInt(line.substring(Space2 + 1, Space3));
        int custNum = Integer.parseInt(line.substring(Space3 + 1));

        for (int x = 0; x < bookNum; x++){
           if (bookarray[x][0] == isbn){
              int stock = Integer.parseInt(bookarray[x][2]);
              if (stock >= num){
                 stock -= num;
                 bookarray[x][2] = Integer.toString(stock);
                 System.out.println("Order filled");
              }      
           }
        }
     }
     line = input.nextLine();
  }
}

这段代码已编译但未打印任何内容,因此我开始调试它,并意识到程序不执行for循环,它从以下位置跳转:

 int custNum = Integer.parseInt(line.substring(Space3 + 1));

致:

 line = input.nextLine();

有人能帮我吗


共 (1) 个答案

  1. # 1 楼答案

    bookNum未初始化,因此其值为0