繁体   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