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

Java else语句多次打印-数组循环,java,arrays,if-statement,for-loop,Java,Arrays,If Statement,For Loop,我希望我的代码在数组中循环,并且仅当数组中有值时,才允许用户选择删除学生。如果所有数组值都为null,那么我希望它打印出一条消息。问题是,我的消息对数组中的每个空元素打印了多次 我的代码: static void deleteStudent() { for (int i = 0;i < 10;i++) { if (studentNamesArray[i] != null) { System.out.println("Which student wou

我希望我的代码在数组中循环,并且仅当数组中有值时,才允许用户选择删除学生。如果所有数组值都为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");
      }
    }
  }
static void deleteStustudent(){
对于(int i=0;i<10;i++){
if(studentNamesArray[i]!=null){
System.out.println(“您希望删除哪个学生?”);
System.out.println(i+“:”+studentNamesArray[i]);
int studentChoice=input.nextInt();
对于(i=studentChoice+1;i
以下是我如何确定所有元素是否为空的方法。您为每个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!");
boolean allNull=true;//默认情况下,所有内容都为空
对于(int i=0;i
for
循环中,使用相同的变量
i
,在该变量中,它覆盖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!");
试着这样做:

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");
      }
    }
}
boolean studentFound = false;

for (int i = 0;i < 10;i++) {
    if (studentNamesArray[i] != null) {
        studentFound = true;
...
static void deleteStustudent(){
对于(int i=0;i<10;i++){
if(studentNamesArray[i]!=null){
System.out.println(“您希望删除哪个学生?”);
System.out.println(i+“:”+studentNamesArray[i]);
int studentChoice=input.nextInt();
对于(int j=studentChoice+1;j

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

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

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");
      }
    }
}
boolean studentFound = false;

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

我建议使用“boolean”变量,让我们把它命名为“flag”。将其初始化为“false”。若在数组中发现非空元素,则在设置“flag=true”时。如果(!flag)System.out.println(“没有学生”);,则在“for”循环检查“之后