Для моего задания CS я установил диапазон значений в своем коде, где пользователь может ставить только монеты от 1 до 10. Однако даже если я установил этот диапазон значений, всякий раз, когда я ставлю определенное количество монет, например 15, которого нет в диапазоне, он думает, что это хорошее число, на которое можно поставить. Это фрагмент кода, в котором расположен диапазон:
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);
И вот остаток моего кода для лучшего контекста:
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);
}
}
Спасибо!