我的程序如下,
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不知道是什么地方出了问题,或者是这个不应该这么写,请高手指点一二
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不知道是什么地方出了问题,或者是这个不应该这么写,请高手指点一二
System.in.read()一次读一个字节,一共3个字节,所以输出3遍。
建议你好好学习一下基础,多看看书,不要一上来就问问题
case改成这个取第一个字母会不会实现目的?
西风(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;
}
}
}
}
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();
}
}