簡體   English   中英

do-while循環問題

[英]Issues with do-while loop

我只是Java編碼的初學者,我只是寫一個簡單的程序:給用戶一個菜單,如果用戶輸入正確的數字,他必須在1-4之間輸入數字,如果輸入了錯誤的數字,則必須完成任務,再次詢問用戶用於輸入。 下面是我的程序

class menu {
  public static void main(String [] args) throws java.io.IOException {
        int choice;
        do
        {
            System.out.println("HELP MENU: ");
            System.out.println("IF STATEMENT: 1 ");
            System.out.println("WHILE: 2 ");
            System.out.println("DO WHILE: 3 ");
            System.out.println("SWITCH: 4 ");
            choice = System.in.read();
            System.out.println(choice);
        }

        while( choice < 1 || choice > 4);
        System.out.println("\n");
        System.out.println(choice);

        switch (choice)
        {
        case 1:
            System.out.println("if statement is selected");
            break;
        case 2:
            System.out.println("while statement is selected");
            break;

        case 3:
            System.out.println("do while statement is selected");
            break;

        case 4:
            System.out.println("switch statement is selected");
            break;
        }
    }
}

輸出: +++++++

E:\study\javacode>java menu 
HELP MENU:
IF STATEMENT: 1
WHILE: 2
DO WHILE: 3
SWITCH: 4
4
52
HELP MENU:
IF STATEMENT: 1
WHILE: 2
DO WHILE: 3
SWITCH: 4
13
HELP MENU:
IF STATEMENT: 1
WHILE: 2
DO WHILE: 3
SWITCH: 4
10
HELP MENU:
IF STATEMENT: 1
WHILE: 2
DO WHILE: 3
SWITCH: 4

用戶通過鍵盤輸入的內容都會不斷重復執行do-while循環。我通過打印輸入值確定了原因,發現代碼輸入值有誤。請幫助解決此問題

要讀取數字或String ,我建議您使用Scanner對象。 main()的開頭創建並實例化它:

Scanner in = new Scanner(System.in);

並在do-while調用nextInt()方法:

do {
    System.out.println("HELP MENU: ");
    System.out.println("IF STATEMENT: 1 ");
    System.out.println("WHILE: 2 ");
    System.out.println("DO WHILE: 3 ");
    System.out.println("SWITCH: 4 ");
    choice = in.nextInt();
    System.out.println(choice);
} while (choice < 1 || choice > 4);

筆記:

  • System.in.read()實際上返回您輸入的字符的int值。 例如,如果輸入1 ,則該方法將返回49 ,它等於(int)'1'
  • 如前所述,您可以使用nextLine()方法使用Scanner讀取String

編輯:

只是為了看到可以使用System.in.read() (必須閱讀文檔以了解其作用)來讀取整數,請嘗試這樣做(在單獨的文件中,以免意外修改代碼):

int i = System.in.read();
System.out.println(Integer.parseInt(Character.toString(((char) i))));

問題是您使用的是InputStream.read(),它從流中讀取一個字節,而不是字符,例如,如果您鍵入“ 1”,則read()將返回0x31。

read()的Javadoc:

/**
 * Reads the next byte of data from the input stream. The value byte is
 * returned as an <code>int</code> in the range <code>0</code> to
 * <code>255</code>. If no byte is available because the end of the stream
 * has been reached, the value <code>-1</code> is returned. This method
 * blocks until input data is available, the end of the stream is detected,
 * or an exception is thrown.
 *

System.in.read()並不像您認為的那樣工作。 它讀取一個字符(不是int)並返回一個字節值,而不是整數。

當用戶輸入“ 1”時, read()返回49,它是字符“ 1”的整數字節值。 (50是“ 2”,51是“ 3”,依此類推)。

@Christian的掃描儀建議非常好。 我認為您應該這樣做。

或者,您可以將switch語句更改為使用49/50/51 / etc,但這有點丑陋。

暫無
暫無

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

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