Java 在数组中输入整数,并在达到限制之前终止输入

Java 在数组中输入整数,并在达到限制之前终止输入,java,Java,我在做教科书中的一道练习题,将整数(负数和正数)加到数组中。我希望用户能够在数组到达末尾之前终止向数组中输入数字[50] 这就是我想到的: 用户输入存储在字符串中的数字。如果keeploping为true且索引

我在做教科书中的一道练习题,将整数(负数和正数)加到数组中。我希望用户能够在数组到达末尾之前终止向数组中输入数字[50]

这就是我想到的:

用户输入存储在字符串中的数字。如果keeploping为true且索引<数组的大小;它将逐个令牌解析字符串,并将数字放入int数组中

必须有一个更简单的方法来实现这一点,我无法让我的代码正常工作,任何帮助都将不胜感激:

// Create Objects to use in program
    Scanner keyboard = new Scanner(System.in);
    int[] arrayOfNumbers = new int[50];


    // Prompt for input
    System.out.println("Enter upto 50 integers separated by a space.");
    System.out.println("Type x to signal no more integers to enter.");

    int index = 0;
    boolean keepLooping = true;

    while (index < arrayOfNumbers.length && keepLooping) {
        String numToAdd = keyboard.nextLine();

        if ( numToAdd.equals("x") || numToAdd.equals("X") ) {
            keepLooping = false;
        }

        if ( !numToAdd.equals("x") || !numToAdd.equals("X") ) {
            arrayOfNumbers[index] = Integer.parseInt(numToAdd); 
        }
    }

    // DEBUG, Print Array
    for (int k=0; k < arrayOfNumbers.length; k++) {
        System.out.println(arrayOfNumbers[k]);
    }
//创建要在程序中使用的对象
扫描仪键盘=新扫描仪(System.in);
int[]数组编号=新的int[50];
//提示输入
System.out.println(“输入最多50个由空格分隔的整数”);
System.out.println(“键入x表示不再输入整数”);
int指数=0;
布尔值keeploping=true;
while(索引
如果您一步一步地调试程序(例如,在Eclipse中使用F6),您会注意到
索引的值没有改变。最快的解决办法是:

while (index < arrayOfNumbers.length && keepLooping) {
    String numToAdd = keyboard.nextLine();

    if ( numToAdd.equals("x") || numToAdd.equals("X") ) {
        keepLooping = false;
    }

    if ( !numToAdd.equals("x") || !numToAdd.equals("X") ) {
        arrayOfNumbers[index] = Integer.parseInt(numToAdd); 
    }

    index++;
}
while(索引


当然,这只解决了数组填充的问题。接下来是编程方面的良好实践,其余的答案将全面介绍这一点。

如果您一步一步地调试程序(例如,在Eclipse中使用F6),您会注意到
索引的值不会改变。最快的解决办法是:

while (index < arrayOfNumbers.length && keepLooping) {
    String numToAdd = keyboard.nextLine();

    if ( numToAdd.equals("x") || numToAdd.equals("X") ) {
        keepLooping = false;
    }

    if ( !numToAdd.equals("x") || !numToAdd.equals("X") ) {
        arrayOfNumbers[index] = Integer.parseInt(numToAdd); 
    }

    index++;
}
int index = 0;
boolean keepLooping = true;

while (index < arrayOfNumbers.length && keepLooping) {
    String numToAdd = keyboard.nextLine();

    if (numToAdd.equalsIgnoreCase("x")) { // Use EqualsIgnoreCase to shorten it
        keepLooping = false;
    } else { // Use an else statement instead of evaluating the inverse
        arrayOfNumbers[index] = Integer.parseInt(numToAdd); 
    }
    index++; // Increment the index to avoid eternal loop and actually fill the array
}
while(索引

当然,这只解决了数组填充的问题。接下来是编程方面的良好实践,其余答案将全面介绍这些实践。

int index=0;
int index = 0;
boolean keepLooping = true;

while (index < arrayOfNumbers.length && keepLooping) {
    String numToAdd = keyboard.nextLine();

    if (numToAdd.equalsIgnoreCase("x")) { // Use EqualsIgnoreCase to shorten it
        keepLooping = false;
    } else { // Use an else statement instead of evaluating the inverse
        arrayOfNumbers[index] = Integer.parseInt(numToAdd); 
    }
    index++; // Increment the index to avoid eternal loop and actually fill the array
}
布尔值keeploping=true; while(索引
int index=0;
布尔值keeploping=true;
while(索引
您可以使用
for
循环简化一点,然后
从循环中断开以退出:

for (int index = 0; index < arrayOfNumbers.length; ++index) {
  String numToAdd = keyboard.nextLine();

  if (numToAdd.equals("x") || numToAdd.equals("X")) {
    break;
  }
  arrayOfNumbers[index] = Integer.parseInt(numToAdd); 
}
for(int index=0;index
您可以使用
for
循环简化一点,然后
从循环中断开以退出:

for (int index = 0; index < arrayOfNumbers.length; ++index) {
  String numToAdd = keyboard.nextLine();

  if (numToAdd.equals("x") || numToAdd.equals("X")) {
    break;
  }
  arrayOfNumbers[index] = Integer.parseInt(numToAdd); 
}
for(int index=0;index
index
从不更改。错误是什么?如果(!numToAdd.equals(“x”)| |!numToAdd.equals(“x”)
则在while循环中增加index++,如果(!numToAdd.equals(“x”)
则此条件始终为真
index
从不更改。错误是什么?如果(!numToAdd.equals(“x”)则在while循环中增加index++| |!numload.equals(“X”)
此外,此条件始终为真