0
我在编写循环的简单任务时遇到了问题。我需要为循环做如下:for循环输出
1)提示用户,询问什么是50 + 10 =
2)如果用户输入错误答案的警告信息会弹出说,他们有2个更多的尝试
3)一旦用户耗尽了他们所有的尝试不同的信息会弹出说你有没有更多的尝试
这是我已经能够拿出:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int attempt = 1;
int answer = 60;
for(attempt = 1; attempt < 0; --attempt)
System.out.print(" 50 + 10 = ");
answer = input.nextInt();
input.nextLine();
if(answer != 60)
{
System.out.printf("Invalid! Try Again! %d attempt(s) left! ", attempt);
System.out.print("\n50 + 10 = ");
answer = input.nextInt();
}
if(attempt == 0)
{
System.out.print("Sorry! You have no more attempts left!");
}
System.exit(0);
}
如果我改变从1中的控制变量的值说2它会打印50 + 10 = 50 + 10 =
当我运行该程序,它会与0尝试输出左而不是2次的尝试,然后1,然后抱歉消息。
该做的工作,但我有关于控制变量的问题。当我将其更改为2或3时,为什么它会为我提示的号码复制我放入的任何号码? 因此,例如在我的代码中,2会使50 + 10 = 50 + 10 = – Abweichung 2012-03-08 06:38:21
您的意思是在你的代码中? – 2012-03-08 06:40:25
因为你的for循环永远不会打开.....没有大括号({})来包含循环将执行的语句....因此它只执行第一个语句...... – 2012-03-08 06:42:10