我一直在试图编写这个程序几个小时,所以请裸露在我身边。我甚至分别分解了这些循环,但是一旦我把所有东西放在一起,它就无法工作,我需要它。循环语句逻辑
所以,让我打破什么,我需要每一个循环做,然后我得到什么输出:
1)第一个循环,我需要使用一个DO /时。在这个循环中,用户被提示输入一个单词。如果用户键入错误的单词,则会收到错误消息:
无效!再试一次! 2次尝试离开!
无效!再试一次! 1次尝试离开!
对不起!你没有更多的尝试了!
这个输出工作,只要错一个字每一个进入时间,但如果1后输入正确的单词失败的尝试它仍然适用错误消息
2)在我的第二个循环( for循环),用户被要求输入“3 * 8 =”如果输入了错误的数字3次,或者输入了24,那么循环的这部分工作正常。
问题出在24进入后的循环中。输出如下:
谢谢你等等等等。如果您是赢家,我们会致电5555555555。 3 * 8 =其中3 * 8不应显示。我意识到我可以休息一下;在此声明之后,但说明明确指出我不能使用break命令。
正确的输出应为:谢谢你等等等等。如果您是赢家,我们会致电5555555555。
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int attempt = 2;
int answer = 24;
long phoneNumber = 0;
String firstName = "";
String lastName = "";
String wordOfTheDay = "";
System.out.printf("Enter the word of the day: ");
wordOfTheDay = input.nextLine();
if(wordOfTheDay.equals("tired"))
{
for(attempt = 2; attempt >= 0; --attempt)
{
System.out.print(" 3 * 8 = ");
answer = input.nextInt();
input.nextLine();
if(answer == 24)
{
System.out.printf("Please enter your first name, last name, and phone number (no dashes or spaces)\n" +"in a drawing for an all-expenses-paid vacation in the Bahamas: ");
firstName = input.next();
lastName = input.next();
phoneNumber = input.nextLong();
System.out.printf(
"Thank you %s %s. We'll call you at %d if you're a winner.",
firstName,
lastName,
+ phoneNumber);
}
else if(answer != 24)
{
if(attempt!=0)
{
System.out.printf("Invalid! Try Again! %d attempt(s) left!\n ", attempt);
continue;
}
else
{
System.out.print("Sorry! You have no more attempts left!");
}
}
}
}
else
{
do
{
System.out.printf("Invalid! Try Again! %d attempt(s) left!\n ", attempt);
--attempt;
System.out.printf("Enter the word of the day: ");
wordOfTheDay = input.nextLine();
} while (attempt >= 1);
if(attempt == 0)
{
System.out.print("Sorry! You have no more attempts left!");
}
}
}
我希望我说得够清楚。
回顾一下,我需要解决我的问题/同时不让我在失败的尝试后输入正确的单词。
此外,我需要摆脱3 * 8 =显示后,用户输入正确的输入。
谢谢!
唯一的检查,你正在为当天的正确词汇在两个循环之外,所以在你第一次检查它时,不管你输入什么,你都不会再检查它。 – DaveJohnston 2012-03-08 09:39:54
我应该重新将它作为家庭作业吗? – Juvanis 2012-03-08 09:50:58
你可以尝试改善你的格式吗?例如你的IDE会为你做这个。 – 2012-03-08 09:55:33