【发布时间】:2016-04-06 01:33:39
【问题描述】:
编辑:由于我的程序仍然无法运行,我发布了整个方法以防万一出现其他问题。
如果用户在提示时输入“n”,我想退出该程序:
char again = 'y';
while (again == 'y' || again == 'Y')
{
String ans = IBIO.inputString ("Unscramble: OBORSLW (Hint: Shellder latches onto its tail.) ");
int tries = 0;
while (!ans.toLowerCase ().equals ("slowbro"))
{
System.out.println ("Incorrect. Wrong answer. Try again.");
tries++;
ans = IBIO.inputString ("\nUnscramble: OBORSLW (Hint: Shellder latches onto its tail.) ");
if (tries > 3)
{
System.out.println ("The correct answer was SLOWBRO.");
again = IBIO.inputChar ("Play again? (y/n) ");
break;
}
}
System.out.println ("Correct.");
System.out.println ("\nQuestion #2 - ");
String ans2 = IBIO.inputString ("\nUnscramble: RVLEGERA (Hint: It rolls down slopes without slowing down.) ");
int tries2 = 0;
while (!ans2.toLowerCase ().equals ("graveler"))
{
System.out.println ("Incorrect. Wrong answer. Try again.");
tries2++;
ans2 = IBIO.inputString ("\nUnscramble: RVLEGERA (Hint: It rolls down slopes without slowing down.) ");
if (tries2 > 3)
{
System.out.println ("The correct answer was GRAVELER.");
again = IBIO.inputChar ("Play again? (y/n) ");
if (again != 'y' || again != 'Y')
break;
}
}
System.out.println ("Correct.");
System.out.println ("\nQuestion #3 -");
String ans3 = IBIO.inputString ("\nUnscramble: TYSGLA (Hint: It's almost invisible and is gaseous.) ");
int tries3 = 0;
while (!ans3.toLowerCase ().equals ("gastly"))
{
System.out.println ("Incorrect. Wrong answer. Try again.");
tries3++;
ans3 = IBIO.inputString ("\nUnscramble: TYSGLA (Hint: It's almost invisible and is gaseous.) ");
if (tries3 > 3)
{
System.out.println ("The correct answer was GASTLY.");
again = IBIO.inputChar ("Play again? (y/n) ");
if (again != 'y' || again != 'Y')
break;
}
}
printSlow ("Correct.");
printSlow ("\nWell Done, " +name+ "! You have passed the test! I'm so happy for you!!");
break;
}
}
每当我故意多次输入错误值时,我都会得到正确答案,如果我愿意,可以再试一次。这部分有效。但是,如果我不想继续,程序会自行继续并继续下一个问题。如何停止整个程序?
【问题讨论】:
-
System.exit(0);退出程序。 -
等待,但我仍然希望玩家在按下“y”后继续播放
-
if (again != 'y') System.exit(0); -
等待,但有时即使我按 y 它也不会循环@Gendarme
-
好吧,我想这与
IBIO.inputChar ("Play again? (y/n) ");有关,我不知道那个方法,所以我不知道里面发生了什么。
标签: java loops while-loop exit