【问题标题】:looping through array - only first index printing循环遍历数组 - 仅第一个索引打印
【发布时间】:2013-12-04 11:58:56
【问题描述】:

我想遍历一个数组并打印任何不为空的值,这是我尝试使用的代码:

for (int i = 0; i < 10; i++) {
  if (studentNamesArray[i] != null) {
    studentFound = true;
    System.out.println("Which student would you like to delete?");
    System.out.println(i + ": " + studentNamesArray[i]);
    int studentChoice = input.nextInt();
  }
}

数组:

static String[] studentNamesArray = new String[10];

问题是它只打印出index[0]。我该如何解决这个问题?

编辑:

这是我的完整代码:

  static void deleteStudent() {
    boolean studentFound = false;
    for (int i = 0; i < 10; i++) {
      if (studentNamesArray[i] != null) {
        studentFound = true;
        System.out.println("Which student would you like to delete?");
        System.out.println(i + ": " + studentNamesArray[i]);
      }
        int studentChoice = input.nextInt();
        for (i = 0; i < 10; i++) {
        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;
        }
      }
    }
    if (!studentFound) {
      System.out.println("There are no students stored");
    }
  }

【问题讨论】:

  • 从这段代码中,输出永远不会是index[0]
  • 你会问Which student would you like to delete?这个问题10次吗?为什么?
  • @AycanYaşıt 他只问 studentNames 不为空的时间。
  • 您确定您的程序已退出该循环吗?还是在等待输入?
  • 向我们展示初始化 studentNamesArray[] 元素的代码。无论如何,为什么需要局部变量 studentChoice?您不能使用它,因为它的作用域以 if 语句结尾...

标签: java arrays for-loop


【解决方案1】:

仅使用 for 循环打印学生姓名。并在打印完所有学生后阅读studentChoice 一次。否则在打印studentNamesArray[0]后等待输入

System.out.println("Which student would you like to delete?");
for (int i = 0; i < 10; i++) {
  if (studentNamesArray[i] != null) {
    studentFound = true;
    System.out.println(i + ": " + studentNamesArray[i]);
  }
}
int studentChoice = input.nextInt();

【讨论】:

  • 在您发布的代码中: int studentChoice = input.nextInt();仍在for循环中。你只是把它排除在 if 条件之外。
  • 啊抱歉,我一定错过了一个括号!它现在似乎按预期工作
【解决方案2】:

确保数组的内容不为空,否则不会打印出来

【讨论】:

  • 我输入了十个元素,但只有第一个是打印
  • 是真正输入的元素,试着把if (studentNamesArray[i] != null) {...}拿走,看看打印出来的内容
  • 删除该行后,仅打印第一个元素
  • 那么我只能想象它在等待输入的第一个循环结束时停止它。
  • 如果这不是真的,那么我们需要查看更多代码来调试问题
【解决方案3】:

我在这里看到的唯一原因,其他元素应该是null。您可以按如下方式确定

 for (int i = 0; i < 10; i++) {
   if (studentNamesArray[i] != null) {
      studentFound = true;
      System.out.println("Which student would you like to delete?");
      System.out.println(i + ": " + studentNamesArray[i]);
      int studentChoice = input.nextInt();// you need to check this 
     }else{
      System.out.println(i + ": " + studentNamesArray[i]);
     }
   }

但是可能还有别的原因。如果input.nextInt() 取自Scanner,您的程序将在那里等待用户输入。然后您必须提供该输入才能继续。确保这些事情。

【讨论】:

  • 我已经添加了我的其余代码,我尝试将输入移到 for 循环之外,但同样的事情正在发生
猜你喜欢
  • 2011-10-07
  • 2019-10-14
  • 1970-01-01
  • 2022-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多