Java 如果用户输入的字符不是';r';或';c';?
比如说,用户会输入“e”,这样它会显示一个错误对话框,但如果是这样的话,我如何将其循环回来呢Java 如果用户输入的字符不是';r';或';c';?,java,Java,比如说,用户会输入“e”,这样它会显示一个错误对话框,但如果是这样的话,我如何将其循环回来呢 public static char readTypeOfConsumer(){ Scanner kbd = new Scanner(System.in); char typeCode='x'; do { System.out.println("Enter the type consumer< you may type r for resident
public static char readTypeOfConsumer(){
Scanner kbd = new Scanner(System.in);
char typeCode='x';
do {
System.out.println("Enter the type consumer< you may type r for
residential or c for commercial>:");
typeCode = kbd.next().charAt(0);
if (typeCode == 'r' || typeCode == 'R') {
System.out.println("type: Residential");
} else if (typeCode == 'c' || typeCode == 'C') {
System.out.println("type: Commercial");
}
} while (typeCode == 'r' || typeCode == 'R' || typeCode == 'c'|| typeCode == 'C');
return typeCode;
}
publicstaticcharreadtypeofconsumer(){
扫描仪kbd=新扫描仪(System.in);
char typeCode='x';
做{
System.out.println(“输入类型consumer<您可以为
住宅或商业用c>:”;
typeCode=kbd.next().charAt(0);
如果(类型代码=='r'| |类型代码=='r'){
System.out.println(“类型:住宅”);
}else if(typeCode=='c'| | typeCode=='c'){
System.out.println(“类型:商业”);
}
}而(typeCode='r'| | typeCode=='r'| | typeCode=='c'| | typeCode=='c');
返回类型码;
}
不幸的是,这种方法对我不起作用这将起作用:
public static char readTypeOfConsumer() {
Scanner kbd = new Scanner(System.in);
boolean typeCodeInvalid = true;
char typeCode;
do {
typeCodeInvalid = false;
System.out.println(
"Enter the type consumer< you may type r for residential or c for commercial>:");
typeCode = kbd.next().toLowerCase().charAt(0);
if (typeCode == 'r') {
System.out.println("type: Residential");
} else if (typeCode == 'c') {
System.out.println("type: Commercial");
} else {
typeCodeInvalid = true;
}
} while (typeCodeInvalid);
return typeCode;
}
publicstaticcharreadtypeofconsumer(){
扫描仪kbd=新扫描仪(System.in);
布尔类型代码无效=真;
字符类型码;
做{
typeCodeInvalid=false;
System.out.println(
“输入消费者类型<您可以为住宅输入r或为商业输入c>:”;
typeCode=kbd.next().toLowerCase().charAt(0);
如果(类型代码=='r'){
System.out.println(“类型:住宅”);
}else if(类型代码==“c”){
System.out.println(“类型:商业”);
}否则{
typeCodeInvalid=true;
}
}while(typecode无效);
返回类型码;
}
我添加了一个
boolean
,它的值为false
,当输入被接受时,可能有更好的方法来实现这一点,但我只是脱离了您提供的代码。它将一直循环,直到输入r
或c
我还添加了
toLowerCase()
,因此您不必检查它是c
还是c
,只需c
,请尝试一下:
public static char readTypeOfConsumer(){
char typeCode='x';
do {
System.out.println("Enter the type consumer< you may type r for residential or c for commercial>:");
Scanner kbd = new Scanner(System.in);
typeCode = kbd.next().charAt(0);
if (typeCode == 'r' || typeCode == 'R') {
System.out.println("type: Residential");
} else if (typeCode == 'c' || typeCode == 'C') {
System.out.println("type: Commercial");
}
} while (typeCode == 'r' || typeCode == 'R' || typeCode == 'c'|| typeCode == 'C');
return typeCode;
publicstaticcharreadtypeofconsumer(){
char typeCode='x';
做{
System.out.println(“输入类型consumer<您可以为住宅输入r,为商业输入c>:”;
扫描仪kbd=新扫描仪(System.in);
typeCode=kbd.next().charAt(0);
如果(类型代码=='r'| |类型代码=='r'){
System.out.println(“类型:住宅”);
}else if(typeCode=='c'| | typeCode=='c'){
System.out.println(“类型:商业”);
}
}而(typeCode='r'| | typeCode=='r'| | typeCode=='c'| | typeCode=='c');
返回类型码;
}
}您的确切问题是什么?在第一次迭代时,do语句中的类型代码会更新为输入的用户数据的第一个字符。我的问题是它不会循环,我基本上只需要找出它是如何循环的。如前所述,do-while循环对我不起作用,因此我需要另一种方法来研究它是如何循环的,为什么不创建两个或语句,一个是你在while中提到的整个语句,另一个不等于r和c。这是如何使它起作用的?您只移动了
扫描仪kbd
,但未在循环时调整。