如何在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。

用于获取输入的用户扫描程序类。这是解决你的问题的好方法。

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

DataInputStreamInputStream)基本上是一个二进制构造。如果你想读取文本数据(例如从控制台),你应该使用阅读器。在源代码中有一个注释掉的BufferedReader。最好使用相同的而不是DataInputStream。,您可以按以下操作

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

或者,您可以使用DataInputStream.readLine(),但它已被弃用是有原因的。并建议使用CCD_ 6。

您可以选择其他选项,如扫描仪。

由于这个线程还没有接受的答案,即使它真的很旧,我仍然会回答;对于那些仍然想要解释的人。

对于这种情况,使用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

"字母";将返回扫描仪中某个空间之前的所有内容。要只查找第一个字母,请按""拆分字符串,然后取数组的第1个(第0个)。

最后,从这个帖子中摘录了一句关于扫描仪的好话

next()只能读取输入直到空格。它不能读两个用空格分隔的单词。此外,next()在读取输入后将光标放在同一行。

nextLine()读取输入,包括单词之间的空格(也就是说,它一直读取到行的末尾)。读取输入后,nextLine()将光标定位在下一行。

稍后,在查找多个单词时,请使用nextLine()而不是next()来获取完整的字符串

可能在上面的执行示例中,您将输入作为"asdfgaf",然后按enter键。因此它取A、S、D、F。。。。作为一次一个字符的输入。您应该以"y"、"n"或"e"(一次只能输入一个字符)的形式输入,然后按Enter键。

例如你想访问你的帐户吗?如果想,请键入"Y",或者如果你想创建一个新帐户,请按"N"退出,按"E":y

相关内容

最新更新