2013-02-14 55 views
2

在下面的例子中,我试图接受来自用户的单个字符输入,但是当运行程序时,我得到了do..while循环多次执行。请看下面的程序结果。如何从Java中的用户获取单个字符输入?

如果有人能帮我解答,那该如何解决这个问题?

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' 
+1

也许你可以使用Readline()而不是readByte() – 2013-02-14 09:25:55

+0

你为什么* *不期望'while'循环执行多次?这就是循环的一个点,你的循环直到'c =='E''。由于你没有出现输入E,这个行为看起来和我所期望的完全一样。这将有助于解释你期望的替代行为。 – 2013-02-14 09:26:01

回答

2

使用DataInputStream类是不是可能是处理你的问题的最佳途径。

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

请尝试使用扫描仪。 下面是扫描仪的一个例子:

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

使用charAt有效地修剪输入只包含第一个字符,虽然在某些情况下,放置“ty”应该产生错误而不是返回“t”。特别是如果T和Y都是有效的选择,因为用户可能在事故中碰到了但是打算写Y. – 2014-11-14 11:24:12

-1

大概在你给输入为“asdfgaf”,然后按下输入上面执行的例子。因此它一次将A,S,D,F ......作为输入一个字符。 您应该输入'y'或'n'或'e'(一次一个字符),然后按Enter键。

如 你想访问您的帐户,如果需要,请键入“Y”或者,如果你想创建一个新帐户,按“托退出按‘E’: Ÿ

2

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

1

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

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

或者你可以使用DataInputStream.readLine(),但其弃用的一个原因。其建议还有使用BufferedReader.readLine()

你可以去其他选项,如Scanner

1

用于获取输入的用户扫描仪类。它是解决您的问题的好方法。

Scanner scan = new Scanner(System.in); 
System.out.println(scan.next().charAt(0)); 
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 

“信”将在扫描仪中空间之前返回的一切。要仅查找第一个字母,请将字符串拆分"",然后取第一个(0)数组。

最后,从this thread about scanners

下一个()采取了良好的报价可以读取输入只能做到的空间。它不能读取由空格分隔的两个单词。另外,next()在读取输入后将光标放在同一行中。

nextLine()读取包含单词之间空格的输入(即,它读取直到行\ n结尾)。一旦输入被读取,nextLine()将光标定位在下一行。

后来,寻找多个单词时,使用nextLine(),而不是下一个()来获得字符串

相关问题