一旦输入“-1”,使用循环数组获取输入和 stop/print 最后 10 个元素的问题

Problems using circular array to take inputs and stop/print the last 10 elements once "-1" is entered

我有一个 Java 程序,它应该执行上述标题所描述的操作。不幸的是,无论我在扫描仪中输入什么,我现在都遇到了只有零和错误消息的问题。

代码:

 public static void main(String[] args) {

  //Set variable
  int count = 0;
  int numInput = 0;
  int arrSize = 10;

  //Set and initialize string
  int[] numArray = new int[arrSize];

  //Set and initialize Scanner
  Scanner sc = new Scanner(System.in);
  System.out.println("Please input some numbers. Stop by typing -1:");

  while (numInput != -1) {
     numInput = sc.nextInt();
     //Check if input is integer or not
     if (numInput == (int)numInput) {
        if (count == -1) {
           count = numArray.length - 1;
        } else if (count == numArray.length) {
           //Append input to array
           numArray[count] = numInput;
           count++;
        }//End of conditional
     } else {
        //Error message
        System.out.println("Enter a valid integer:");
     }//End of conditional
  }//End of while loop

  //Print array with for loop
  for (int i = 0; i <= 10; i++) {
     System.out.print(numArray[i] + " ");
  }//End of for loop

  System.out.println("\n");

  sc.close();

}// End of main

输出:

0 0 0 0 0 0 0 0 0 0 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 10 out of bounds for length 10 at last10.main(last10.java:42)

如Namandeep_Kaur 评论所述,您的 for 循环条件不正确。您希望它是 i < 10 而不是 i <= 10,因为您的 numArray 长度设置为 10,但是数组索引在 Java 中是 zero-based。因此,您将访问以索引 0 开头的元素。numArray 将在索引 0-9 处包含元素。本质上,当 i 等于 10 时,您的循环将要停止,因为那时条件 10 < 10 为假,您将不会尝试访问 numArray[10] 处不存在的元素。我还有 re-factored 你的程序。

import java.util.Scanner;
import java.util.InputMismatchException;

public class LimboProgram {

public static void main(String[] args) {

 //Set variable
 int count = 0;
 int numInput = 0;
 int arrSize = 10;

 //Set and initialize string
 int[] numArray = new int[arrSize];

 //Set and initialize Scanner
 Scanner sc = new Scanner(System.in);
 System.out.println("Please input some numbers. Stop by typing -1:");

 while (numInput != -1) {
  
  try
  {
     // you do not need to check if numInput is a number because 
     // sc.nextInt() will throw an error if the input is not a number.
     numInput = sc.nextInt();
     
     if ( numInput == -1 ) {
        break;
     }
     
     // You can achieve a circular rotation using the modulo (%) operator.
     // So, if count becomes 10, then 10 % 10 results in 0. If count is 11,
     // then 11 % 10 results in 1. This allows you to "circle" the array.
     count = count % numArray.length;
     numArray[count] = numInput;
     count++;
  }
  catch (InputMismatchException e) {
     System.out.println(e);
  }
  
 }


 for (int i = 0; i < 10; i++) {
    System.out.print(numArray[i] + " ");
 }

 System.out.println("\n");

 sc.close();

 }

}

我希望这是您在程序中寻找的结果。如果有什么我可以进一步澄清的,我很乐意。

循环数组?这是一个例子。

public class Main
{
    public static void main(String[] args)
    {
        final byte[] array = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        System.out.println(array[cycle(-1,10)]); // 9
    }

    public static final int cycle(final int index, final int quantity)
    {
        return ((index % quantity) + quantity) % quantity;
    }
}