else语句多次打印 - 数组循环

时间:2013-11-30 19:10:28

标签: java arrays if-statement for-loop

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

我的代码:

  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");
      }
    }
  }

4 个答案:

答案 0 :(得分: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");
}

答案 1 :(得分:0)

以下是我如何确定所有元素是否为空。你为每个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!");

答案 2 :(得分:0)

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");
      }
    }
}

虽然,我不确定你打算打印出什么,但我根据自己的直觉调整了其他变量名称;结果可能不完全一样,但我相信您可以使用变量名进行进一步调整,这样您就不会互相干扰并造成过多的循环。

答案 3 :(得分:0)

我建议使用'boolean'变量,让它命名为'flag'。将其初始化为“false”。如果在数组中找不到null元素,则设置'flag = true'。 'for'循环检查'if(!flag)System.out.println(“没有学生。”);'。