簡體   English   中英

如何使用不斷提示用戶的 Do-while 循環?

[英]How to use a Do-while loop that continuously prompts a user?

(我有一個作業問題,我一直在關注 Java 中的“do-while 循環”。)

它要求我有一個 do-while 循環,繼續提示用戶輸入“小於 100 的數字”,直到輸入的數字實際上小於 100。

(它將運行三個測試:) 例如:測試 1:對於用戶輸入 123、395、25,預期輸出為:

Enter a number (<100): 
Enter a number (<100): 
Enter a number (<100): 
Your number < 100 is: 25

(到目前為止,這是我的代碼:)

public class NumberPrompt {
   public static void main (String [] args) {
   Scanner scnr = new Scanner(System.in);
   int userInput = 0;

      do {
         System.out.println("Enter a number (<100):" );
         System.out.println("Enter a number (<100):" );
         System.out.println("Enter a number (<100):" );
         userInput = userInput + 25;
      } while (userInput > 100);
         System.out.print("");

      System.out.println("Your number < 100 is: " + userInput);
}

(我的輸出與上面的測試 1 結果完全匹配,但我意識到我根本沒有正確設置循環,因為當它進行“-9”的第二次測試時,輸出與我的第一次測試完全相同:)

Enter a number (<100):
Enter a number (<100):
Enter a number (<100):
Your number < 100 is: 25

(這是我接觸循環的第一周,我四處搜索了一些示例,但我沒有找到很多讓我想起這個循環的“Do”while 循環。如果有人有一些好的提示或指南可以指點我我將不勝感激。)

你不需要寫System.out.println("Enter a number (<100):" ); 三次。 每次用戶輸入的值小於 100 時,它都會自動顯示。並且您自己為 userInput 賦值,而不是從用戶那里獲取輸入。 您應該編寫下面給出的代碼。

 do {
     System.out.println("Enter a number (<100):" );
     userInput = scnr.nextInt();
  } while (userInput > 100);
     System.out.println("Your number < 100 is " + userInput);

1.閱讀價值

正如 Juan 的評論所建議的那樣,您不是在閱讀用戶輸入值。 基本上,這是通過nextInt()方法完成的。 而且, int是一種原始類型,必須進行初始化。 您選擇了值 0。那么為什么不選擇 101 以使其以錯誤的值開頭呢? 那么你肯定會觸發while循環:

public static void main(String... aArgs) {

    Scanner sc = new Scanner(System.in);
    // initialise at 101 to trigger the while loop
    int userInput = 101;

    // as we start at 101, it will enter the loop at least once
    while (userInput > 100) {
        System.out.println("Enter a number (<100):");
        userInput = sc.nextInt();
    }

    System.out.println("Your number < 100 is: " + userInput);
}

2.永遠不要相信用戶

2.1 異常捕獲

上面的代碼可能足以滿足您的任務。 但是,為了學習,我有一個基本原則:永遠不要相信用戶! . 如果在前面的示例中,如果用戶輸入azerty ,那么您的程序將向您拋出InputMismatchException 那是什么? 這個錯誤告訴你掃描器需要一個int而你用別的東西給了他:它拋出了這個。

一個簡單的比喻是:掃描儀只能吃蘋果。 你給他一個梨:他拿了一點然后吐了出來:“我只能吃蘋果”。 為了防止你的掃描儀嘔吐,你可以問他:“試試看,如果這不是蘋果,我們試試別的東西

在代碼中,它為您提供以下內容:

public static void main(String... aArgs) {

    Scanner sc = new Scanner(System.in);
    // initialise at 101 to trigger the while loop
    int userInput = 101;

    // as we start at 101, it will enter the loop at least once
    while (userInput > 100) {
        // tell the scanner to try something
        try {
            System.out.println("Enter a number (<100):");
            userInput = sc.nextInt();
        }
        // if the input is not a number, tell him do this this:
        catch (InputMismatchException e) {
            System.out.println("This is not a number!");
        }
    }

    System.out.println("Your number < 100 is: " + userInput);
}

如果你不熟悉try/catch子句,你可以閱讀這個

2.1 掃描儀進紙

上面的代碼是行不通的。 如何? 如果你輸入一些不是數字的東西,比如“aaaa”,你將有無限的

輸入一個數字 (<100):

這不是數字!

為什么? 因為掃描儀沒有把你的輸入扔出去。 基本上,他要么應該吃梨(但他會吐)要么把它扔到垃圾箱,但你從來沒有告訴他把它扔到垃圾箱! 掃描器需要在嘗試下一個輸入之前消耗輸入。

簡而言之:

  1. userInput從 101 開始,因此進入while循環
  2. 您輸入aaaa
  3. InputMismatchException被捕獲。 “這不是數字!” 被打印出來
  4. userInput 值沒有改變(仍然是 101),所以循環繼續
  5. 在這個階段,掃描器沒有消耗前一個輸入,所以下一個輸入仍然是aaaa
  6. 轉到 3. 並重新開始

如何解決這個問題? 通過告訴掃描器使用next()來消耗輸入:

public static void main(String... aArgs) {

    Scanner scnr = new Scanner(System.in);
    // initialise at 101 to trigger the while loop
    int userInput = 101;

    // as we start at 101, it will enter the loop at least once
    while (userInput > 100) {
        // tell the scanner to try something
        try {
            System.out.println("Enter a number (<100):");
            userInput = scnr.nextInt();
        }
        // if the input is not a number, tell him do this this:
        catch (InputMismatchException e) {
            // consume the incorrect input with scnr.next()
            // so that user can enter another input
            System.out.println(scnr.next() + " is not a number!");
        }
    }

    System.out.println("Your number < 100 is: " + userInput);
}

暫無
暫無

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

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