怎样从屏幕输入一个字符?
import java.io.IOException;
class Bug { public static void main(String args[]) { throw java.io.IOException { ichar a ; a = System.in.read(); } switch(a) { case 'b': System.out.println("boy!"); case 'g': System.out.println("girl!"); default: System.out.println("not a human!"); } } } 上面的语句不行啊!!!
----------------解决方案--------------------------------------------------------
here is a program. Maybe usefull for you.
[CODE]
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class keyboard{
private static String getEingabe(){
String eingabe;
BufferedReader reader;
reader = new BufferedReader( new InputStreamReader( System.in ) );
try {
eingabe = reader.readLine();
return eingabe;
} catch ( IOException e ) { e.printStackTrace(); }
return null;
}
public static int getInteger(){
String eingabe = getEingabe();
int zahl = Integer.parseInt( eingabe );
return zahl;
}
public static double getDouble(){
String eingabe = getEingabe();
double zahl = Double.parseDouble( eingabe );
return zahl;
}
public static String getString(){
String eingabe = getEingabe();
return eingabe;
}
}
[/CODE]
----------------解决方案--------------------------------------------------------
啊,我只要从dos下输入一个字符,要这么麻烦吗?!
----------------解决方案--------------------------------------------------------
那个是缓冲的,有没有直接的象C++那样输入的?
有没有简单一点的方法?
----------------解决方案--------------------------------------------------------
你还挺麻烦的啊。还挑精拣肥的啊!
下面看看这段代码,java 的输入输出确实要比C++麻烦。
[CODE]
import java.io.*;
public class ReadCharacter
{
public static void main(String [] args)
{
char inChar;
System.out.println("Enter a character");
try
{
inChar = (char)System.in.read();
System.out.println("You entered: " + inChar);
}
catch(IOException e)
{
System.err.println("Error reading from user.");
}
}
}
[/CODE]
----------------解决方案--------------------------------------------------------
谢谢 kai
----------------解决方案--------------------------------------------------------