我的程序如下,
import java.io.IOException;
class HelloWorldConsole{
public static void main(String[] args) throws IOException 
{
boolean bool_quit;
char  char_read;
bool_quit = true;
while (bool_quit==true){
System.out.println("Select one of the following:");
System.out.println("[D]eutsch");
System.out.println("[E]nglish");
System.out.println("[F]rancais");
System.out.println("[Q]uit");
char_read = (char)System.in.read();
switch(char_read) {
case 'd':
System.out.println("Guten Tag!");
break;
case 'e':
System.out.println("Hello");
break;
case 'f':
System.out.println("Aurevoir");
break;
case 'q':
bool_quit = false;
break;
case 'D':
System.out.println("Guten Tag!");
break;
case 'E':
System.out.println("Hello");
break;
case 'F':
System.out.println("Aurevoir");
break;
case 'Q':
bool_quit = false;
break;
default :
break;
}
}
}
}然后运行之后的结果如下:Select one of the following:
[D]eutsch
[E]nglish
[F]rancais
[Q]uit
d
Guten Tag!
Select one of the following:
[D]eutsch
[E]nglish
[F]rancais
[Q]uit
Select one of the following:
[D]eutsch
[E]nglish
[F]rancais
[Q]uit
Select one of the following:
[D]eutsch
[E]nglish
[F]rancais
[Q]uit不知道是什么地方出了问题,或者是这个不应该这么写,请高手指点一二

解决方案 »

  1.   

    “输入字符d然后按回车”,实际上是输入了3个字节,'d'、 '\r'、 '\n',
    System.in.read()一次读一个字节,一共3个字节,所以输出3遍。
      

  2.   

    你写程序的思路有问题,bool_quit都没有赋值,虽然Java会自动赋值。
    建议你好好学习一下基础,多看看书,不要一上来就问问题
      

  3.   

    str.substring();
    case改成这个取第一个字母会不会实现目的?
      

  4.   

    我说的是错误的!!以下,西风提供:
    西风(3697*****) 10:21:33
    import java.io.*;
    import java.io.IOException;
    class HelloWorldConsole{
    public static void main(String[] args) throws IOException 
    {
    boolean bool_quit;
    // int inchar;
    char char_read;
    bool_quit = true;
    BufferedReader br=null;
    while (bool_quit==true){
    System.out.println("Select one of the following:");
    System.out.println("[D]eutsch");
    System.out.println("[E]nglish");
    System.out.println("[F]rancais");
    System.out.println("[Q]uit");
     br=new BufferedReader(new InputStreamReader(System.in));
     char_read= br.readLine().charAt(0);

    switch(char_read) {
    case 'd':
    System.out.println("Guten Tag!");
    break;
    case 'e':
    System.out.println("Hello");
    break;
    case 'f':
    System.out.println("Aurevoir");
    break;
    case 'q':
    bool_quit = false;
    break;
    case 'D':
    System.out.println("Guten Tag!");
    break;
    case 'E':
    System.out.println("Hello");
    break;
    case 'F':
    System.out.println("Aurevoir");
    break;
    case 'Q':
    bool_quit = false;
    break;
    default :
    break;
    }
    }
    }
    }
      

  5.   

    import java.io.*;class HelloWorldConsole{
    public static void main(String[] args) throws IOException 
    {
    boolean bool_quit;
    char  char_read;
    bool_quit = true;
    String fromUser="";
    BufferedReader stdIn = null;

    while (bool_quit==true){
    System.out.println("Select one of the following:");
    System.out.println("[D]eutsch");
    System.out.println("[E]nglish");
    System.out.println("[F]rancais");
    System.out.println("[Q]uit"); stdIn = new BufferedReader(new InputStreamReader(System.in));
    if ((fromUser = stdIn.readLine()) != null)
            {
             //System.out.println("fromUser="+fromUser);
             char_read = fromUser.charAt(0);
             //System.out.println("char_read="+char_read);
    switch(char_read) {
    case 'd':
    System.out.println("Guten Tag!");
    break;
    case 'e':
    System.out.println("Hello");
    break;
    case 'f':
    System.out.println("Aurevoir");
    break;
    case 'q':
    bool_quit = false;
    break;
    case 'D':
    System.out.println("Guten Tag!");
    break;
    case 'E':
    System.out.println("Hello");
    break;
    case 'F':
    System.out.println("Aurevoir");
    break;
    case 'Q':
    bool_quit = false;
    break;

    default :
    System.out.println("No this Command!");
    break;
    }
    }

    }
    stdIn.close();
    }
    }