簡體   English   中英

如何在Java中從用戶那里獲取單字符輸入?

[英]How to take single character input from user in Java?

在下面的例子中,我正在嘗試接受來自用戶的單個字符輸入,但是在運行程序時,我得到do..while循環執行多次。 請參閱下面的程序結果。

如果有人可以幫我解決問題,如何解決這個問題呢?

import java.io.*;



public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {



            char c;
           // BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            DataInputStream in =new DataInputStream(System.in);

            // Asking the user what to do with the application
           do{ 

            System.out.println("Would you like to access your account, if yes,type 'Y' or if you want to create a new account press 'N'to exit press 'E' ");          

            byte b = in.readByte();
            c = (char) b;
            c = Character.toUpperCase(c);

            if (c=='Y'){
                System.out.println(c);
               }
            else if (c=='N') {
              System.out.println(c);
            }
            else if (c=='E'){
               System.out.println(c); 
            }
            else{
                System.out.println("Incorrect Entry, try again: "+c); 
            }

           }while (c!='E');

    }

}

產量

init:
deps-jar:
compile:
run:
Would you like to access your account, if yes,type 'Y' or if you want to create a new account press 'N'to exit press 'E' 
asdfgaf
Incorrect Entry, try again: A
Would you like to access your account, if yes,type 'Y' or if you want to create a new account press 'N'to exit press 'E' 
Incorrect Entry, try again: S
Would you like to access your account, if yes,type 'Y' or if you want to create a new account press 'N'to exit press 'E' 
Incorrect Entry, try again: D
Would you like to access your account, if yes,type 'Y' or if you want to create a new account press 'N'to exit press 'E' 
Incorrect Entry, try again: F
Would you like to access your account, if yes,type 'Y' or if you want to create a new account press 'N'to exit press 'E' 
Incorrect Entry, try again: G
Would you like to access your account, if yes,type 'Y' or if you want to create a new account press 'N'to exit press 'E' 
Incorrect Entry, try again: A
Would you like to access your account, if yes,type 'Y' or if you want to create a new account press 'N'to exit press 'E' 
Incorrect Entry, try again: F
Would you like to access your account, if yes,type 'Y' or if you want to create a new account press 'N'to exit press 'E' 
Incorrect Entry, try again: 

Would you like to access your account, if yes,type 'Y' or if you want to create a new account press 'N'to exit press 'E' 

使用DataInputStream可能不是處理您情況的最佳方法。

DataInputStream會緩沖您鍵入的輸入,這就是您使用循環獲取不需要的冗長消息的原因。

請嘗試使用掃描儀。 這是掃描儀的一個例子:

Scanner objScanner = new Scanner(System.in);
char c = objScanner.next().charAt(0);
System.out.println(c);

使用System.in.read()而不是DataInputStream。

DataInputStreamInputStream )基本上是二進制構造。 如果要讀取文本數據(例如從控制台),則應使用Reader。 在源代碼中有一個注釋掉的BufferedReader 最好使用相同的而不是DataInputStream 你可以這樣做,

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String input = in.readLine();

或者您可以使用DataInputStream.readLine() ,但由於某種原因不推薦使用它。 它建議還使用BufferedReader.readLine()

您可以選擇其他選項,如掃描儀

用於獲取輸入的User Scanner類。 解決問題的好方法。

Scanner scan = new Scanner(System.in);
System.out.println(scan.next().charAt(0));

既然這個帖子還沒有接受過的答案,我仍然會回答,盡管它真的很老; 對於仍然想要解釋的人。

使用Scanner是最好的選擇。 掃描儀易於使用,並停止線程直到完成。 您可以使用它,或者您可以創建一個新線程來保持當前線程的運行。 掃描儀的基本用法:

Scanner s = new Scanner(System.in) //Makes a new scanner with System.in
String letter = s.next(); //Use next to find the character typed
//Insert code using letter variable

“letter”將在掃描儀中的空格之前返回所有內容。 要僅找到第一個字母,請將字符串拆分為""然后取第一個(第0個)數組。

最后,從這個線程中獲取了一個關於掃描儀的好引用

next()只能讀取輸入直到空格。 它無法讀取由空格分隔的兩個單詞。 此外,next()在讀取輸入后將光標置於同一行。

nextLine()讀取包含單詞之間空格的輸入(即,它讀取直到行的結尾\\ n)。 讀取輸入后,nextLine()將光標定位在下一行。

稍后,當查找多個單詞時,使用nextLine()而不是next()來獲取完整的 String

可能在上面的執行示例中,您輸入了“asdfgaf”,然后按下回車鍵。 因此,它將A,S,D,F ....作為輸入一次輸入一個字符。 你應該輸入'y'或'n'或'e'(一次一個字符),然后按Enter鍵。

例如,您是否要訪問您的帳戶,如果是,請鍵入“Y”或如果您要創建新帳戶,請按“否”退出“E”:y

暫無
暫無

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

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