所以最近我发布了一个问题,询问我的高等和低等游戏,它到处都是,只是没有工作,我在这个游戏中采取了另一种方法,并且它正在工作,这次只是一个小问题,它比解释更容易展示,这是我的代码:
String noReplay = "\nThank you for playing.";
String highLow = "(H)igher or (L)ower: ";
String loseHigh = "Card is higher, you lose. Would you like to play again? Y/N: ";
String loseLow = "Card is lower, you lose. Would you like to play again? Y/N: ";
String playAgain;
boolean replay = false;
Random rand = new Random();
Scanner scan = new Scanner(System.in);
do {
int card1 = rand.nextInt(13) + 1;
int card2 = rand.nextInt(13) + 1;
System.out.println("Card is: " + card1);
System.out.printf(highLow);
String guess = scan.nextLine().toUpperCase();
if(card2 > card1 && guess.equalsIgnoreCase("H")) {
System.out.println("Card is: " + card2);
System.out.printf(highLow);
replay = true;
guess = scan.nextLine().toUpperCase();
}
else if(card2 > card1 && guess.equalsIgnoreCase("L")) {
System.out.println("Card is: " + card2);
System.out.printf(loseHigh);
playAgain = scan.nextLine().toUpperCase();
if(playAgain.equalsIgnoreCase("Y")) {
replay = true;
}
else if(playAgain.equalsIgnoreCase("N")) {
replay = false;
System.out.println(noReplay);
}
}
else if(card2 < card1 && guess.equalsIgnoreCase("H")) {
System.out.println("Card is: " + card2);
System.out.printf(loseLow);
playAgain = scan.nextLine().toUpperCase();
if(playAgain.equalsIgnoreCase("Y")) {
replay = true;
}
if(playAgain.equalsIgnoreCase("N")) {
replay = false;
System.out.println(noReplay);
}
}
else if(card2 < card1 && guess.equalsIgnoreCase("L")) {
System.out.println("Card is: " + card2);
System.out.printf(highLow);
replay = true;
guess = scan.nextLine().toUpperCase();
}
} while(replay != false);
我的项目的目的是创建一个简单的游戏,当用户输入更高或更低的上面的卡号时,程序将回答正确的答案,或者从该阶段重播游戏,或者询问用户是否想在失败时再次玩游戏。
但是从我的输出中可以正常工作:
Card is: 9
(H)igher or (L)ower: h
Card is: 11
(H)igher or (L)ower: l
*Card is: 6*
*(H)igher or (L)ower: l*
*Card is: 7*
Card is higher, you lose. Would you like to play again? Y/N: y
*Card is: 13*
*(H)igher or (L)ower: h*
*Card is: 10*
(H)igher or (L)ower:
我们可以在星号上看到,它没有做它应该做的事情。 当我说更高时,如果数字较低,它应该问我是否想再次玩,因为我弄错了。在这种情况下,它不是吗?
这就是我的问题^
另一件事情即时通讯,我想请求输入检查器的帮助,我只希望用户能够输入Y,N,H,L(我已经分类了大写或小写的问题出)。
感谢您阅读!
答案 0 :(得分:1)
这应该有意义并有助于减少游戏的复杂程度。看看我对card1与card2的比较,你希望你的猜测是基于card2与card1的比较,而不是相反。如果这是有道理的。希望这可以帮助!
String highLow = "(H)igher or (L)ower: ";
String loseHigh = "Card is higher, you lose.";
String loseLow = "Card is lower, you lose.";
String loseMessageDetails = "";
boolean gameLost = false;
boolean askAgain = true;
Scanner scan = new Scanner(System.in);
String guess = "";
Random rand = new Random();
int card1;
int card2;
while(askAgain)
{
if(gameLost)
{
System.out.println(loseMessageDetails + "\nWould you like to play again? Y/N");
guess = "";
while(!guess.equals("Y") && !guess.equals("N"))
{
guess = scan.nextLine().toUpperCase();
}
if(guess.equals("Y"))
{
gameLost = false;
askAgain = true;
}
else
{
askAgain = false;
}
}
if(!gameLost)
{
card1 = rand.nextInt(13) + 1;
card2 = rand.nextInt(13) + 1;
System.out.println("Card is: " + card1);
guess = "";
//loop until correct type of input
while(!guess.equals("H") && !guess.equals("L"))
{
System.out.printf(highLow);
guess = scan.nextLine().toUpperCase();
}
//now we either correctly guessed or incorrectly guessed
//here is a correct guess
if((card1 < card2 && guess.equalsIgnoreCase("H")) ||
(card1 > card2 && guess.equalsIgnoreCase("L")))
{
askAgain = true;
}
else
{
loseMessageDetails = guess.equalsIgnoreCase("L") ? loseHigh : loseLow;
gameLost = true;
}
}
}
这也将确保只有有效输入并继续询问输入,直到用户输入正确的字母。