我正在创建一个赌博计划。我完成了投注过程,但是我想让投注者在投注结束后继续投注,但我如何重新启动代码并允许用户更换投注呢?
代码:
import java.util.Scanner;
import java.util.Random;
public class Casino {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int min = 1;
int max = 10;
int colmax = 2;
double balance = 2500.0;
double prize = 0;
double bet1 = 0;
double bet2 = 0;
System.out.println(" -==-==-==-==-==-==-==-==-==-==-" );
System.out.println("-==-==-=={ Welcome to the Marist Casino! }==-==-==-");
System.out.println(" -==-==-==-==-==-==-==-==-==-==-" );
System.out.println("(1) Red Fox Roullete");
System.out.println("(2) Blackjack");
System.out.println("(3) Crash");
System.out.print("Enter the number for the game you'd like to play!: ");
int game = input.nextInt();
if(game == 1) {
System.out.println("---------------------------------------------");
System.out.println("Welcome to Red Fox Roullete!");
System.out.print("Please choose Black or Red....and a number from 1-10: ");
String color = input.next();
int number = input.nextInt();
System.out.print("How much would you like to bet on "+color+ "?: ");
bet1 = input.nextInt();
System.out.print("How much would you like to bet on "+number+"?: ");
bet2 = input.nextInt();
System.out.println("------------------Results--------------------");
System.out.println("You just bet "+bet1+" on "+color+" and "+bet2+" on "+number);
System.out.println("Spinning............");
Random rouletteNum = new Random();
int rNum = min + rouletteNum.nextInt(max);
int rCol = min + rouletteNum.nextInt(colmax);
if (rCol == 1) {
System.out.println("The machine landed on Black "+rNum);
}
else {
System.out.println("The machine landed on Red "+rNum);
}
if(rNum == number) {
System.out.println("Congrats, you guessed the right number!");
balance += bet2 * 5;
}
else {
System.out.println("Sorry!You didnt guess the right number! You've lost "+bet2);
balance -= bet2;
}
if(rCol == 1 && color == "black") {
System.out.print("Congrats, you guessed the right color!");
balance += bet1 * 2;
}
else {
System.out.println("Sorry!You didnt guess the right color!You've lost "+bet1);
balance -= bet1;
}
System.out.print("New balance is :"+balance);
}
}
}
总而言之,余额会打印在最后一行,我想问一下"Do you want to place another bet?"
,但我不确定我是如何回到允许它们的代码的要做到这一点。有什么想法/建议吗?
答案 0 :(得分:2)
您可以创建一个处理下注活动的方法,并在显示余额后调用该方法。
public static void bet() {
System.out.print("How much would you like to bet on "+color+ "?: ");
bet1 = input.nextInt();
System.out.print("How much would you like to bet on "+number+"?: ");
bet2 = input.nextInt();
System.out.println("------------------Results--------------------");
System.out.println("You just bet "+bet1+" on "+color+" and "+bet2+" on
"+number);
System.out.println("Spinning............");
}
答案 1 :(得分:0)
您可以添加一个具有正确中断的不定式循环,如:
npm i npm@5 -g
答案 2 :(得分:0)
您只需使用do ... while
语句即可。
你可以做的例子:
Scanner input = new Scanner(System.in);
boolean anotherBet;
do {
... // place all your code here
...
...
// Check if the user wants to bet again! after first game is over.
System.out.print("Do you want to place another bet? (true/false): ");
anotherBet = input.nextBoolean();
} while(anotherBet);
希望这有帮助