我有这个java代码:
public class Ages {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int value = 0;
String name;
int age;
System.out.print("Hey, what's your name? ");
name = keyboard.next();
System.out.println();
System.out.print("Ok, " + name + ", how old are you? ");
age = keyboard.nextInt();
System.out.println();
do{
if(age < 16){
System.out.println("You can't visit in the museum, " + name + ".");
if(age < 18);
System.out.println("You can't visit in the park, " + name + ".");
}
if (age < 25){
System.out.println("You can't visit if you have a car, " + name + ".");
}
if (age >= 25){
System.out.println("You can do anything, " +
name + ".");
}
while(age > 300);
System.out.println("It's not correct..Try Again, " + name +
".");
}
}
我需要那个用户写错了答案,他会再次得到问题&#34;你多大了?&#34;,并且在他再次尝试之后...... 我goona做什么? 谢谢您的帮助!! :)
答案 0 :(得分:3)
1)你应该使用这个条件:if (age >= 25 && age <= 300){
代替
if (age >= 25){
。
如果true
超过300并且您不想,则条件为age
:
while(age > 300);
2)如果值不正确,您还应允许用户输入新输入。
age = keyboard.nextInt();
应该在循环中。
3)你应该允许循环完成。
在while语句中,您可以指定需要来自用户的新输入的条件:
while (age > 300)
这给出了代码(未测试):
int age;
do{
age = keyboard.nextInt();
if(age < 16){
System.out.println("You can't visit in the museum, " + name + ".");
}
else if(age < 18){
System.out.println("You can't visit in the park, " + name + ".");
}
else if (age < 25){
System.out.println("You can't visit if you have a car, " + name + ".");
}
if (age >= 25 && age <= 300){
System.out.println("You can do anything, " +
name + ".");
}
else{
System.out.println("It's not correct..Try Again, " + name + ".");
}
}
while (age > 300);
答案 1 :(得分:3)
一个 while块不正确你必须使用:
System.out.println("It's not correct..Try Again, " + name + ".");
do {
if (age < 16) {
System.out.println("You can't visit in the museum, " + name + ".");
if (age < 18);
System.out.println("You can't visit in the park, " + name + ".");
}
if (age < 25) {
System.out.println("You can't visit if you have a car, " + name + ".");
}
if (age >= 25) {
System.out.println("You can do anything, "
+ name + ".");
}
//while(age > 300);<<----------wrong position
System.out.println("It's not correct..Try Again, " + name + ".");
} while (age > 300);//<<----------correct position
<强>两个强>
虽然条件接缝不正确,但您必须检查最小和最大年龄,而不仅仅是最大,因此您可以使用:
while (age < minAge || age > maxAge );
因此,如果年龄小于最小值,那么最大重复次数
三这个如果不会执行:
if (age < 18);//<<----------------note the ; it mean your if is end
System.out.println("You can't visit in the park, " + name + ".");
四
相反,您可以阅读循环中的年龄:
System.out.print("Hey, what's your name? ");
name = keyboard.next();
System.out.println();
do {
System.out.print("Ok, " + name + ", how old are you? ");
age = keyboard.nextInt();
System.out.println();
....
}while(..);
希望这可以让您了解您的问题。
答案 2 :(得分:1)
请试试这个:
public class Ages {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int value = 0;
int age;
System.out.print("Hey, what's your name? ");
String name = keyboard.next();
System.out.println();
while(true) {
System.out.print("Ok, " + name + ", how old are you? ");
age = keyboard.nextInt();
System.out.println();
if (age <= 300) {
break;
}
System.out.println("It's not correct..Try Again, " + name +
".");
}
if(age < 16) {
System.out.println("You can't visit in the museum, " + name + ".");
}
if(age < 18){
System.out.println("You can't visit in the park, " + name + ".");
}
if (age < 25){
System.out.println("You can't visit if you have a car, " + name + ".");
}
if (age >= 25){
System.out.println("You can do anything, " +
name + ".");
}
}
}