【问题标题】:else statement printing multiple times - array loopelse 语句打印多次 - 数组循环
【发布时间】:2013-11-30 19:10:27
【问题描述】:

我希望我的代码循环遍历一个数组,并且仅在数组中有值时才为用户提供删除学生的选项。如果所有数组值都为空,那么我希望它打印出一条消息。问题是我的消息为数组中的每个空元素打印了多次。

我的代码:

  static void deleteStudent() {
    for (int i = 0;i < 10;i++) {
      if (studentNamesArray[i] != null) {
        System.out.println("Which student would you like to delete?");
        System.out.println(i + ": " + studentNamesArray[i]);
        int studentChoice = input.nextInt();
        for (i = studentChoice + 1;i < studentNamesArray.length; i++) {
          studentNamesArray[i-1] = studentNamesArray[i];
        }
        nameArrayCount = nameArrayCount -1;
        studentNamesArray[studentNamesArray.length - 1] = null;
        for(i = studentChoice + 1;i < 9;i++) {
          for(int y = 0;y < 3;y++) {
            studentMarksArray[i-1][y] = studentMarksArray[i][y];
          }
        }
        markArrayCount = markArrayCount - 1;
        for(int y = 0;y < 3;y++) {
          studentMarksArray[9][y] = 0;
        }
      } else {
          System.out.println("There are no students stored");
      }
    }
  }

【问题讨论】:

    标签: java arrays if-statement for-loop


    【解决方案1】:

    else 块在每个循环迭代中运行。如果您只想在最后运行一次,请执行以下操作:

    boolean studentFound = false;
    
    for (int i = 0;i < 10;i++) {
        if (studentNamesArray[i] != null) {
            studentFound = true;
    ...
    

    然后在for之后:

    if (!studentFound) {
        System.out.println("There are no students stored");
    }
    

    【讨论】:

      【解决方案2】:

      以下是我将如何确定所有元素是否为空。您为每个 null 元素打印它的原因是 print 语句位于 for 循环内。

      boolean allNull = true; // default is that everything is null
      for(int i = 0; i < studentNamesArray.length; i++) { // iterate through entire array
          if(studentNamesArray[i] != null) { // if even 1 of them is not null
              allNull = false; // set allNull to false
              break; // and break out of the for loop
          }
      }
      
      if(allNull) System.out.println("There are no students stored!");
      

      【讨论】:

        【解决方案3】:

        我建议使用“布尔”变量,我们将其命名为“标志”。将其初始化为“假”。如果您在数组中发现非空元素,则设置'flag = true'。 'for'循环后检查'if (!flag) System.out.println("There are no students.");'.

        【讨论】:

          【解决方案4】:

          在您的 for 循环中,您使用相同的变量 i,它会覆盖 for 循环中使用的原始变量。

          试试这样的:

          static void deleteStudent() {
              for (int i = 0;i < 10;i++) {
                if (studentNamesArray[i] != null) {
                  System.out.println("Which student would you like to delete?");
                  System.out.println(i + ": " + studentNamesArray[i]);
                  int studentChoice = input.nextInt();
                  for (int j = studentChoice + 1;j < studentNamesArray.length; j++) {
                    studentNamesArray[j-1] = studentNamesArray[j];
                  }
                  nameArrayCount = nameArrayCount -1;
                  studentNamesArray[studentNamesArray.length - 1] = null;
                  for(int k = studentChoice + 1;k < 9;k++) {
                    for(int y = 0;y < 3;y++) {
                      studentMarksArray[k-1][y] = studentMarksArray[k][y];
                    }
                  }
                  markArrayCount = markArrayCount - 1;
                  for(int z = 0;z < 3;z++) {
                    studentMarksArray[9][z] = 0;
                  }
                } else {
                    System.out.println("hi");
                }
              }
          }
          

          虽然我不确定您要打印什么,但我根据直觉对其他变量名称进行了调整;结果可能与结果不完全相同,但我相信您可以对变量名称进行进一步调整,以免相互覆盖并导致过多的循环。

          【讨论】: