当前位置: 首页 > 知识库问答 >
问题:

如何在Java中从用户获取单字符输入?

戈华茂
2023-03-14

在下面的例子中,我试图接受用户的单字符输入,但是当运行程序时,我得到do...而循环执行多次。请参阅下面程序的结果。

如果有人能帮我找到答案,如何解决这个问题?

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' 

共有3个答案

强金鑫
2023-03-14

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

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

或者您可以使用DataInputStream.readLine(),但不建议使用它是有原因的。它也建议使用BufferedReader.readLine()

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

欧阳嘉
2023-03-14

使用系统。在里面read()而不是DataInputStream。

赫连秦迟
2023-03-14

使用数据输入流可能不是处理您的情况的最佳方式。

DataInputStream缓冲您键入的输入,这就是为什么您会收到带有循环的不需要的长消息。

试着改用扫描仪。以下是扫描仪的一个示例:

Scanner objScanner = new Scanner(System.in);
char c = objScanner.next().charAt(0);
System.out.println(c);
 类似资料:
  • 我需要从用户给出的输入日期打印日历。然而,我不允许使用任何预定的日期类。 目前,我可以打印月份和年份,但是,我似乎不知道如何打印特定月份和年份的日期。我对Java非常陌生,因此非常感谢您的帮助! 以下是所需输出的示例: 以下是我到目前为止的输出: 以下是我迄今为止所尝试的:

  • 所以我的问题很简单。 如何从表单中获取用户输入并将其放入变量中? 我想用香草JS和没有库来做这件事。 多谢了。

  • 我是Java的新手,我正在尝试从一个多行的输入中获取一个字符串。 例如,一个字符串=“the quick brown fox jumps over the lazy dog.the quick brown fox jumps over the lazy dog.the quick fox jumps over the lazy dog.”输入如下: 敏捷的棕色狐狸跳过懒惰的狗。 敏捷的棕色狐狸跳过

  • 问题内容: 如何从用户进行pygame打印输入: 我试图让用户键入一些内容,然后pygame将其打印在屏幕上。 这是我当前的程序: 我想要这样,当用户点击Enter时,它将清空屏幕。 帮我! 问题答案: 这是一个示例脚本,可将输入切换到屏幕。它显示了如何在遍历pygame事件队列时修改字符串。每帧都将清除屏幕,并重建名称表面并使其变白。 这是要点版本

  • 问题内容: 我不知道如何从字符串中获取Unicode字符。例如,如果字符串是“你好”,我如何获得第一个字符“你”? 从另一个地方我得到一种方法: 确实有效。但是我仍然有一些问题: 还有另一种方法吗? 为什么在Go中不能从字符串中获取Unicode字符,却可以获取字节数据? 问题答案: 首先,您可能需要阅读https://blog.golang.org/strings 。它将回答您的部分问题。 Go

  • 问题内容: 当用户在Java中键入单个字符时,是否有一种简单的方法可从控制台读取它?可能吗?我尝试了这些方法,但是它们都在等待用户按Enter键: 我开始认为System.in在按下Enter之前不知道用户输入。 问题答案: 您要做的是将控制台置于“原始”模式(绕过行编辑且无需输入键),而不是“经烹煮”模式(需要输入键进行行编辑)。在UNIX系统上,“ stty”命令可以更改模式。 现在,关于Ja