【发布时间】:2016-05-25 15:57:06
【问题描述】:
import java.util.Scanner;
public class PlayAgain {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
boolean playing = true;
char replayCheck;
do { //start do-while
System.out.print("Play again? (y/n): ");
boolean validInput = false;
while (validInput = false){ //start while
replayCheck = input.next().charAt(0);
switch (replayCheck) { //start switch
case 'y':
case 'Y':
validInput = true;
playing = true;
break;
case 'n':
case 'N':
validInput = true;
playing = false;
break;
default:
System.out.println("Invalid input! please enter (y/n)");
validInput = false;
break;
} //end switch
} //end while
} while (playing = true); //end do-while
System.out.println("Thanks for playing!");
} //end main
} //end class
如果用户输入n/N,程序会再次播放,任何其他输入也是如此。逻辑似乎很好,但我在replayCheck = input.next().charAt(0); 的行上得到“从未使用分配的值”,所以我怀疑问题就在那里。
我有点菜鸟。欢迎提出任何建议!
【问题讨论】:
-
你的while测试错了:你做作而不是测试...替换为:playing == true
标签: java