import java.util.Scanner;
import javax.swing.JOptionPane;
public class Coin {
public static void main(String[] args)
{
boolean validFinalInput = false;
boolean validBetInput = false;
boolean validGuessInput = false; //Determines if the guess is a valid input
boolean validBet = false; //Determines if the bet is valid
boolean goAgain = true;
double num; //Unparsed Side Determiner
double balance = 100;
String Bet = null; //Bet Input
double bet = 0; //Parsed Bet
String Guess = null; //H or T
String Side = null; //Determines Side
String GoAgain = null; //Y/N Retry input
Scanner in = new Scanner(System.in);
while (goAgain == true){ //While true, the player will keep playing.
num = Math.round(Math.random()); //Declares Number
if (num == 1){
Side = "H";
} else if (num == 0){
Side = "T";
}
while (validGuessInput == false){ //Determines if the input is valid
System.out.println("Guess: (H/T)");
Guess = in.next();
if (Guess.equals("H") || Guess.equals("T")){
validGuessInput = true;
} else {
JOptionPane.showMessageDialog(null, "Invalid Guess!");
validGuessInput = false;
}
}
while (validBet == false){
while (validBetInput == false){
try {
System.out.println("Bet? (Your balance is $" + balance);
Bet = in.next();
bet = Double.parseDouble(Bet);
if (bet > 0){
validBetInput = true;
}
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Invalid Bet!");
}
}
if (bet <= balance && bet >= 0){
validBet = true;
} else {
JOptionPane.showMessageDialog(null, "Invalid Bet!");
validBet = false;
}
}
if (Guess.equals(Side)){
balance = balance + (bet * 2);
System.out.println("Correct! The side was: " + Side);
System.out.println("Your balance is now: $" + balance);
} else {
balance = balance - bet;
System.out.println("Incorrect! The side was: " + Side);
System.out.println("Your balance is now: $" + balance);
}
if (balance == 0){
System.out.println("You ran out of money. Ending Game.");
break;
}
System.out.println("Go again? (Y/N)");
GoAgain = in.next();
while (validFinalInput == false);
System.out.println("Go again? (Y/N)");
GoAgain = in.next();
validGuessInput = false;
validBet = false;
validBetInput = false;
if (GoAgain.equals("Y")){
goAgain = true;
validFinalInput = true;
} else if (GoAgain.equals("N")){
goAgain = false;
validFinalInput = true;
System.out.println("Thanks for playing! You ended with: $" + balance);
} else {
System.out.println("Invalid Input!");
}
}
}
}
所以我为“头或尾”风格的游戏编写了这段代码。它最初工作,但后来我决定添加一些功能,它被破坏了。基本上,它会结束,但会停留在最后一个输入(Y / N)上。
有人能告诉我我做错了吗?
很抱歉没有评论。
我也知道我的变量并不完全符合惯例,对此也很抱歉。
感谢。
答案 0 :(得分:5)
在此处删除分号;
while (validFinalInput == false)
的 强>
正确缩进代码。
检查你的逻辑;您为两个条件设置了validFinalInput = true(true和false)。
if(GoAgain.equals(“Y”)){ ......... validFinalInput = true; }
else if(GoAgain.equals(“N”)){ ....... validFinalInput = true; }
答案 1 :(得分:1)
goAgain的值永远不会以while (goAgain == true){
开头的循环发生变化,因为它在}
之前以while (validBet == false){
结尾。最后的括号是错误的。
答案 2 :(得分:0)
你有这个while语句
while (validFinalInput == false);
这是一个无限循环!由于分号,它永远不会超过这部分代码。如果你像这样重写它应该工作:
while (validFinalInput == false)
{
System.out.println("Go again? (Y/N)");
GoAgain = in.next();
validGuessInput = false;
validBet = false;
validBetInput = false;
if (GoAgain.equals("Y"))
{
goAgain = true;
validFinalInput = true;
}
else if (GoAgain.equals("N"))
{
goAgain = false;
validFinalInput = true;
System.out.println("Thanks for playing! You ended with: $" + balance);
}
else
{
System.out.println("Invalid Input!");
}
}