无法让我的代码重复问题以使游戏继续



我必须为我要学习的类编写一个代码。这是一款基于下注2种颜色和1-36数字的游戏。用户已经获得了一个设定数量的芯片,即100。我已经编写了大部分代码,但是,我无法让我的代码重复这个过程。我目前正在使用Do While循环。但它就是不起作用。

这是我的代码:

import java.util.Scanner;
import java.lang.Math;
public class Program_8 {
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
int chipsNow = 100;
int userChoice;
int chipsBetted = 0;


//welcome message
welcome();  

do {


int spinNum = (int)(Math.random() * 36) + 0;
userChoice = getMenuChoice(userInput);

//get user choice
if (userChoice == 1) {
int getNum = getNumber(userInput);
int getBet = getBet(userInput, chipsNow);
String determineColor = determineColor(spinNum);

System.out.println("nSpinning Wheel....");
System.out.println("Spin Number: " + spinNum);
System.out.println("Spin Color: " + determineColor);

if (getNum == spinNum) {

chipsNow += getBet;
chipsBetted += getBet;
System.out.println("Congrats, you won!");
System.out.println("nYou now have: " + chipsNow + " chips");
;
}

else {
chipsNow -= getBet;

System.out.println("nSorry, you lost!");
System.out.println("You now have: " + chipsNow + " chips");

}
}

if (userChoice == 2) {
String getColor = getColor(userInput);
int getBet = getBet(userInput, chipsNow);
String determineColor = determineColor(spinNum);

System.out.println("Spinning Wheel....");
System.out.println("Spin Number: " + spinNum);
System.out.println("Spin Color: " + determineColor);

if (getColor.equals(determineColor)) {

chipsNow += getBet;
chipsBetted += getBet;
System.out.println("nCongrats, you won!");
System.out.println("You now have: " + chipsNow + " chips");

}

else {

chipsNow -= getBet;
System.out.println("nSorry, you lost!");
System.out.println("You now have: " + chipsNow + " chips");

}
}

}while (userChoice != 3 && chipsNow > 0);



}

//welcome message
public static void welcome() {

int chipsNow = 100;

System.out.println("############################");
System.out.println("#    Welcome To Roulette   #");
System.out.println("############################");
System.out.println("# Number Bets Payout: 35:1 #");
System.out.println("# Color Bets Payout: 1:1   #");
System.out.println("############################n");
System.out.println("Chips owned: " + chipsNow + "n");

System.out.println("1. Pick a number to bet on");
System.out.println("2. Pick a color to bet on");
System.out.println("3. Cash Outn");

}
//get menu choice
public static int getMenuChoice(Scanner userInput) {
int getMenuChoice;

System.out.println("nChoose an option [1-3]: ");
getMenuChoice = userInput.nextInt();


return getMenuChoice;
}

public static int getNumber(Scanner userInput) {
int getNumber;

do {

System.out.println("Enter a number to bet on [0-36]: ");
getNumber = userInput.nextInt();


}while (getNumber < 0 || getNumber > 36);   


return getNumber;
}

public static String getColor(Scanner userInput) {
String getColor = "";

do{
System.out.println("Enter a color to bet on [Red or Black]: ");
getColor = userInput.next();

}while (!(getColor.equals("Red") || getColor.equals("Black")));

return getColor;
}

public static int getBet(Scanner userInput, int chipsNow) {
int getBet;

do{
System.out.println("Enter the number of chips to bet [1 - " + chipsNow + "]: ");
getBet = userInput.nextInt();

}while (getBet < 1 || getBet > chipsNow);


return getBet;
}

public static String determineColor(int spinNum) {

if (spinNum % 2 == 0) {
if (spinNum == 0) {
return "Green";
}
//even
else {
return "Red";
}
}

return "Black";


}

public static void report(int chipsNow) {

System.out.println("nThanks for Playing!");
System.out.println("You Won a total of: " + chipsNow + " chips today");



}   
}

所以只要看看代码我就会:

  1. 在do while之外声明int userChoice = 0;。这是while条件工作所必需的。

  2. welcome();userChoice = getMenuChoice(userInput);应该进入do,因此它将重复欢迎消息并要求用户选择执行时所做的一切

  3. while(userChoice != 3 && chipsNow > 0)这样简化while循环。这只是为了让它更易读。

  4. 最后删除if(getNum == spinNum) { } else { }中的break;break将强制退出while循环,而不管while条件是否满足。所以基本上,如果你赢了或输了一场比赛,你的循环就会退出,我认为这不是你想要的。如果chipsNow < 0userChoice == 3

最新更新