[英]Values that cannot be bet by the user still can be used
For my CS assignment, I set up a range of values in my code where the user can only bet coins from 1-10.对于我的 CS 任务,我在我的代码中设置了一个值范围,用户只能下注 1-10 的硬币。 However even though I set this range of values up, whenever I bet a certain amount of coins such as 15 which is not in the range, it thinks it's a good number that it can be bet.
然而,即使我设置了这个数值范围,每当我下注一定数量的硬币时,例如 15 不在该范围内,它认为这是一个可以下注的好数字。 This is the piece of code where the range is located:
这是范围所在的代码段:
do {
System.out.print(" How much would you like to bet? ");
System.out.println("");
userBet = input.nextDouble();
userCoins = (userCoins - userBet);
numOfSpins++;
if (userBet > userCoins || userBet > 10) {
System.out.println("The number of coins you can bet can only be from 1-10!");
}
else if (userBet >= 1 && userBet <= 10) {
}
} while (userBet < 1 || userBet > 10);
And heres the rest of my code for better context:这是我的代码的 rest 以获得更好的上下文:
import java.util.Random;
import java.util.Scanner;
public class SlotMachine {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
Random randInt = new Random();
System.out.println("Welcome to The Royal Casino!");
System.out.println("");
System.out.println("Remember, getting:");
System.out.println("### means x2");
System.out.println("$$$ means x5");
System.out.println("&&& means x20");
System.out.println("@@@ means x100");
System.out.println("");
System.out.println("Now, you will be starting off with 100 coins. Good luck!");
double userCoins = 100.00; //Total number of coins you have
double userBet = 0; //The user's bet with how much coins he/she has
double numOfSpins = 0; //The number of spins the user does
double win = 0;
double loss = 0;
String quitKey = "q"; //The key the user enters to quit
do {
do {
System.out.print(" How much would you like to bet? ");
System.out.println("");
userBet = input.nextDouble();
userCoins = (userCoins - userBet);
numOfSpins++;
if (userBet > userCoins || userBet > 10) {
System.out.println("The number of coins you can bet can only be from 1-10!");
}
else if (userBet >= 1 && userBet <= 10) {
}
} while (userBet < 1 || userBet > 10);
int spin1 = randInt.nextInt(4) + 1;
int spin2 = randInt.nextInt(4) + 1;
int spin3 = randInt.nextInt(4) + 1;
String spinName1 = " ", spinName2 = " ", spinName3 = " ";
switch (spin1)
{
case 1:
spinName1 = "#"; break;
case 2:
spinName1 = "$"; break;
case 3:
spinName1 = "&"; break;
case 4:
spinName1 = "@"; break;
}
switch (spin2)
{
case 1:
spinName2 = "#"; break;
case 2:
spinName2 = "$"; break;
case 3:
spinName2 = "&"; break;
case 4:
spinName2 = "@"; break;
}
switch (spin3)
{
case 1:
spinName3 = "#"; break;
case 2:
spinName3 = "$"; break;
case 3:
spinName3 = "&"; break;
case 4:
spinName3 = "@"; break;
}
System.out.println("-------------------------------");
System.out.printf("%-12s%-10s%5s\n", spinName1, spinName2, spinName3);
System.out.print("-------------------------------\n");
if (spin1 == 1 && spin2 == 1 && spin3 == 1)
{
userCoins = userCoins + (userBet * 2);
win++;
System.out.printf("3 in a row! You have won: $%.2f",(userBet * 2));
System.out.println("You have spun " + numOfSpins);
System.out.printf(" You currently have: $%.2f", userCoins);
}
else if (spin1 == 2 && spin2 == 2 && spin3 == 2)
{
userCoins = userCoins + (userBet * 5);
win++;
System.out.printf("3 in a row! You have won: $%.2f",(userBet * 5));
System.out.println("You have spun " + numOfSpins);
System.out.printf(" You currently have: $%.2f", userCoins);
}
else if (spin1 == 3 && spin2 == 3 && spin3 == 3)
{
userCoins = userCoins + (userBet * 20);
win++;
System.out.printf("3 in a row! You have won: $%.2f",(userBet * 20));
System.out.println("You have spun " + numOfSpins);
System.out.printf(" You currently have: $%.2f", userCoins);
}
else if (spin1 == 4 && spin2 == 4 && spin3 == 4)
{
userCoins = userCoins + (userBet * 100);
win++;
System.out.printf("3 in a row! You have won: $%.2f",(userBet * 100));
System.out.println("You have spun " + numOfSpins);
System.out.printf(" You currently have: $%.2f", userCoins);
}
else
{
loss++;
System.out.println("You have won nothing. Try again!");
System.out.println("You have spun " + numOfSpins);
System.out.printf(" You currently have: $%.2f", userCoins);
}
} while (userCoins > 0);
System.out.println(" Oops! You have no more coins. Maybe next time you'll be more lucky!");
System.out.println("Your total balance is " + userCoins);
System.out.println("Your total spins were " + numOfSpins);
System.out.println("Your total wins are " + win + " and total losses are " + loss);
}
}
Thank you!谢谢!
At the start of that do-while loop, you have userCoins = (userCoins - userBet);
在那个 do-while 循环的开始,你有
userCoins = (userCoins - userBet);
, which means that even if the input is completely invalid, the bet value will still be removed every time. ,这意味着即使输入完全无效,每次下注值仍然会被移除。 This line should be after it's exited the first do-while loop.
这一行应该在它退出第一个 do-while 循环之后。 Once that's done, your program works fine as far as I can tell, and rejects invalid bets.
一旦完成,据我所知,您的程序运行良好,并且拒绝无效投注。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.