簡體   English   中英

如何在 while 循環中使用 Scanner hasNextInt()?

[英]How to use Scanner hasNextInt() inside a while loop?

我無法退出 while 循環。

我不知道為什么sc.hasNextInt()在上次讀取數字后不返回 false。

我應該使用其他方法還是我的代碼中有錯誤?

public static void main(String[] args) {

        // Creating an array by user keyboard input
        Scanner sc = new Scanner(System.in);

        System.out.println("Length of array: ");
        int[] numbers = new int[sc.nextInt()];

        System.out.printf("Type in integer elements of array ", numbers.length);
        int index = 0;
        **while ( sc.hasNextInt()) {**
            numbers[index++] = sc.nextInt();


        }
        // created method for printing arrays
        printArray(numbers);
        sc.close();


}

當您從控制台接收輸入時,放置在while循環條件中的 Scanner hasNextInt()方法將繼續讀取(意味着循環將繼續),直到發生以下情況之一:

  • 您提交了一個非數字符號(例如一個字母)。
  • 您提交一個所謂的“文件結束”字符,這是一個特殊符號,告訴掃描儀停止閱讀。

因此,在您的情況下,您的 while 循環條件中不能有hasNextInt() - 我在下面展示了一個解決方案,其中包含一個您可以使用的計數器變量。

但是while循環中的hasNextInt()方法在從與控制台不同的源(例如,從字符串或文件)讀取時有其實際用途。 這里示例的啟發,假設我們有:

String s = "Hello World! 3 + 3.0 = 6 ";

然后我們可以將字符串s作為輸入源傳遞給 Scanner(注意我們沒有將System.in傳遞給構造函數):

Scanner scanner = new Scanner(s);

然后循環直到hasNext() ,它檢查輸入中是否有任何類型的另一個標記。 在循環內,使用hasNextInt()檢查此標記是否為 int 並打印它,否則使用next()將標記傳遞給下一個:

while (scanner.hasNext()) {
    if (scanner.hasNextInt()) {
        System.out.println("Found int value: " + scanner.next());
    } else {
        scanner.next();
    }
}

結果:

Found int value: 3
Found int value: 6

在上面的示例中,我們不能在 while 循環條件本身中使用hasNextInt() ,因為該方法在它找到的第一個非 int 字符上返回 false(因此循環立即關閉,因為我們的 String 以字母開頭)。

但是,我們可以使用while (hasNextInt())從文件中讀取數字列表

現在,解決您的問題的方法是將index變量放在 while 循環條件中:

while (index < numbers.length) {
    numbers[index++] = sc.nextInt();
}

或者為了清楚起見,創建一個特定的計數器變量:

int index = 0;
int counter = 0;
while (counter < numbers.length) {
       numbers[index++] = sc.nextInt();
       counter++;
}

請執行下列操作:

使用輸入長度作為循環的結束。

        // Creating an array by user keyboard input
        Scanner sc = new Scanner(System.in);

        System.out.println("Length of array: ");

        int len = sc.nextInt();
        int[] numbers = new int[len]; // use len here

        System.out.printf("Type in integer elements of array ", numbers.length);
        int index = 0;
        for (index = 0; index < len; index++) { // and use len here
            numbers[index] = sc.nextInt();
        }
        // created method for printing arrays
        printArray(numbers);
        sc.close();

並且不要關閉掃描儀。

我無法退出 while 循環。

我不知道為什么sc.hasNextInt()在上次讀取數字后不返回 false。

我應該使用另一種方法還是我的代碼中有錯誤?

public static void main(String[] args) {

        // Creating an array by user keyboard input
        Scanner sc = new Scanner(System.in);

        System.out.println("Length of array: ");
        int[] numbers = new int[sc.nextInt()];

        System.out.printf("Type in integer elements of array ", numbers.length);
        int index = 0;
        **while ( sc.hasNextInt()) {**
            numbers[index++] = sc.nextInt();


        }
        // created method for printing arrays
        printArray(numbers);
        sc.close();


}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM