如题
java中相当与c 中的getchar 的方法是什么?
import java.io.*;public class Main {
    public static void main(String[] args) throws IOException {
        DataInputStream in = new DataInputStream(System.in);
        char ch = in.readChar();
        System.out.println(ch);
    }
   
}上面的代码为什么输入英文 读出来是中文,     如何解决

解决方案 »

  1.   

    (char)System.console().reader().read();
      

  2.   

    BufferedReader consoleReader = new BufferedReader(
    new InputStreamReader(System.in));
    String line = consoleReader.readLine();
    char cmd = line.length() == 0 ? 0 : line.charAt(0);
      

  3.   

    把 “char ch = in.readChar();”
    改为“char ch =(char) in.read();”即可
      

  4.   

    呵呵,直接在运行时通过main的args传入不可以吗???
      

  5.   

    1.readLine() 不行
      比如我直接打一个回车。对于c的getChar()也是可以算是获取一个输入字符吧
      但是你这里读到一个空字符串。似乎不是很合适
    2.用(char)System.in.read()
      仅局限于ascii字符。如果你尝试输入一个非ascii字符,就不是这么回事了
      

  6.   

    从控制台读入吗.?用Scanner比较容易
      

  7.   

    使用基于字符输入流才是真正的答案
    1.Reader reader = System.console().reader();
    2.Reader reader = new InputStreamReader(System.in);获取reader后,执行 reader.read(),并转为char值即可得到输入的字符值。不论是ascii码还是中文,都正常运行(char)reader.read();ps:方法1,在IDE里运行可能会有问题.
      

  8.   

    Scanner 是可以用的。读进来的是字符串,比如说保存在 str。str.charAt(0); 就是第一个字符。括号里的数字就是 index。把字符串就当数组看好了。还有一个解决方案就直接用 char c = (char)new BufferedReader(new InputStreamReader(System.in)).read();
    就可以读取你输入的第一个字符。然后有了字符你就随便处理好了。比如可以用 switch 语句:
    switch (c) {
            case 'A':
                    // do something
            case 'B':
                    // do something
    }---------------------------------------
    你初学者,我就把代码再写一遍吧:import java.io.*;public class Demo {
            public static void main (String args[]) {
                    char c = 0;
                    try {
                            c = (char)new BufferedReader(new InputStreamReader(System.in)).read();
                    } catch (IOException ioe) {
                            System.exit(0);
                    }
                    switch (c) {
                            case 'A':
                                    System.out.println("It is A.");
                                    break;
                            case 'B':
                                    System.out.println("It is B.");
                                    break;
                    }
            }
    }
      

  9.   

    还不是很满意
    比如楼上的,  我输入AB  只能输出A    那个B就给活生生的吞了。
      

  10.   

    InputStream.read()
    读入一个字节!!
      

  11.   

    getchar有一个int型的返回值.当程序调用getchar时.程序就等着用户按键.用户输入的字符被存放在键盘缓冲区中.直到用户按回车为止(回车字符也放在缓冲区中).当用户键入回车之后,getchar才开始从stdio流中每次读入一个字符.getchar函数的返回值是用户输入的第一个字符的ASCII码,如用户在按回车之前输入了不止一个字符,其他字符会保留在键盘缓存区中,等待后续getchar调用读取.也就是说,后续的getchar调用不会等待用户按键,而直接读取缓冲区中的字符,直到缓冲区中的字符读完为后,才等待用户按键. 相似功能可以用BufferedReader.readLine()得到一个字符串,再用String.charAt(i)得到一个一个字符来实现.注意的是readLine()得到的一整行是到换行符但不包括换行符的字符串
    -----------------
    你的代码为什么输入英文 读出来是中文,可以看DataInputStream.readChar()的实现就明白了.
    char readChar(){
    int ch1 = in.read();
    int ch2 = in.read();
    if ((ch1 | ch2) < 0)
         throw new EOFException();
    return (char)((ch1 << 8) + (ch2 << 0));
    }
      

  12.   

    麻烦楼上的详细解释下
    char readChar(){
    int ch1 = in.read();
    int ch2 = in.read();
    if ((ch1 | ch2) < 0)
      throw new EOFException();
    return (char)((ch1 << 8) + (ch2 << 0));
    }
    小弟很菜,谢谢