我还不熟悉Java,所以我在解决这个老虎机程序时遇到了一些问题。在您第一次运行程序并计算奖励(例如,您下注20美元并赢得40美元,因此您的新金额为120美元)之后,它应该循环回来并提示"如何你想打多少赌?"再次,然后用新的总和来完成游戏(所以你要花费120美元的一部分而不是游戏开始时给你的100美元),然后继续这样做,直到你的钱用完为止。但是它现在的工作方式,游戏完全重新开始,因此用户每次都会以$ 100开始。有什么提示吗?
import java.util.Random;
import java.util.Scanner;
import java.io.*;
public class SlotMachineSimulation
{
public static void main(String [] args)
{
Scanner user_input = new Scanner (System.in);
Random randInt = new Random();
System.out.print("Welcome to the Slot Machine Simulator!\n" + "\nActions:" + "\n1. Start a new game" + "\n2. View scores" + "\n3. Exit\n");
System.out.print("\nPlease select an action: ");
int userAction = user_input.nextInt();
if (userAction == 1)
{
System.out.print("\nBefore the game begins, please enter your name: ");
String userName = user_input.next();
System.out.print("\nGame start! You will begin with $100.00." + "\nEnter a negative value to quit the game. " + "Good luck, " + userName + "!\n");
double startingSum = 100.00;
double userTotal = 0.0;
System.out.printf("\nYou currently have: $%.2f\n", startingSum);
do
{
System.out.print("\nHow much would you like to bet? ");
double userBet1 = user_input.nextDouble();
userTotal = (startingSum - userBet1);
//Beginning of random number generation
int val1 = randInt.nextInt(6) + 1;
int val2 = randInt.nextInt(6) + 1;
int val3 = randInt.nextInt(6) + 1;
String valName1 = " ", valName2 = " ", valName3 = " "; //For assignment of slot machine names to randomly generated values
switch (val1)
{
case 1:
valName1 = "Cherries";
break;
case 2:
valName1 = "Oranges";
break;
case 3:
valName1 = "Plums";
break;
case 4:
valName1 = "Bells";
break;
case 5:
valName1 = "Melons";
break;
case 6:
valName1 = "Bars";
break;
}
switch (val2)
{
case 1:
valName2 = "Cherries";
break;
case 2:
valName2 = "Oranges";
break;
case 3:
valName2 = "Plums";
break;
case 4:
valName2 = "Bells";
break;
case 5:
valName2 = "Melons";
break;
case 6:
valName2 = "Bars";
break;
}
switch (val3)
{
case 1:
valName3 = "Cherries";
break;
case 2:
valName3 = "Oranges";
break;
case 3:
valName3 = "Plums";
break;
case 4:
valName3 = "Bells";
break;
case 5:
valName3 = "Melons";
break;
case 6:
valName3 = "Bars";
break;
}
System.out.println("\n-------------------------------");
System.out.printf("%-12s%-10s%5s\n", valName1, valName2, valName3);
System.out.print("-------------------------------\n");
//Beginning of reward calculation
if (val1 == val2 || val2 == val3 || val1 == val3)
{
System.out.println("\nNumber of matches: 1");
double doubleReward = (userBet1 * 2);
double postBetSum = (userTotal + doubleReward);
System.out.printf("You have won: $%.2f", doubleReward);
System.out.printf("\nYou currently have: $%.2f", postBetSum);
}
else if (val1 == val2 && val2 == val3)
{
System.out.println("\nNumber of matches: 3");
double tripleReward = (userBet1 * 3);
double postBetSum = (userTotal + tripleReward);
System.out.printf("\nYou have won: $%.2f",tripleReward);
System.out.printf("\nYou currently have: $%.2f", postBetSum);
}
else
{
System.out.println("\nNumber of matches: 0");
System.out.println("You have won: $0.00");
System.out.printf("You currently have: $%.2f", userTotal);
}
} while (userTotal > 0.00);
} //end of #1 option
}
}
答案 0 :(得分:0)
更改此行并应该正常工作
double userTotal = 100.0;
userTotal -= userBet1;
甚至使用
String valName1 = genName(), valName2 = genName(), valName3 = genName();
和方法
public static String genName()
{
int ran = new Random().nextInt(6) + 1;
switch (ran)
{
case 1:
return "Cherries";
case 2:
return "Oranges";
case 3:
return "Plums";
case 4:
return "Bells";
case 5:
return "Melons";
case 6:
return "Bars";
default:
return "Cherries";
}
}
清除代码=)
答案 1 :(得分:0)
您在每次迭代中从开始总和中减去 userbet,并使用 userTotatl 检查条件。您需要按如下方式分配:
userTotal = startingSum (Before the loop);
在循环内
userTotal-= userbet;
答案 2 :(得分:-1)
代码中的问题:
解决方案:
所有这一切,加上你可以把你的开关构造放在一个只返回一个字符串而不是写三次的方法中。
现在试试这个:
class SlotMachineSimulation
{
public static String getValName(int val){
String valName=" ";
switch (val)
{
case 1:
valName = "Cherries";
break;
case 2:
valName = "Oranges";
break;
case 3:
valName = "Plums";
break;
case 4:
valName = "Bells";
break;
case 5:
valName = "Melons";
break;
case 6:
valName = "Bars";
break;
}
return valName;
}
public static void main(String [] args)
{
Scanner user_input = new Scanner (System.in);
Random randInt = new Random();
System.out.print("Welcome to the Slot Machine Simulator!\n" + "\nActions:" + "\n1. Start a new game" + "\n2. View scores" + "\n3. Exit\n");
System.out.print("\nPlease select an action: ");
int userAction = user_input.nextInt();
if (userAction == 1)
{
System.out.print("\nBefore the game begins, please enter your name: ");
String userName = user_input.next();
System.out.print("\nGame start! You will begin with $100.00." + "\nEnter a negative value to quit the game. " + "Good luck, " + userName + "!\n");
double userTotal = 0.0;
System.out.printf("\nYou currently have: $%.2f\n", 100.00);
int count=0;
do
{
System.out.print("\nHow much would you like to bet? ");
double userBet1 = user_input.nextDouble();
count++;
if(count==1){
userTotal=100.0-userBet1;
}else{
userTotal=userTotal-userBet1;
}
//Beginning of random number generation
int val1 = randInt.nextInt(6) + 1;
int val2 = randInt.nextInt(6) + 1;
int val3 = randInt.nextInt(6) + 1;
String valName1,valName2,valName3; //For assignment of slot machine names to randomly generated values
valName1=getValName(val1);
valName2=getValName(val2);
valName3=getValName(val3);
System.out.println("\n-------------------------------");
System.out.printf("%-12s%-10s%5s\n", valName1, valName2, valName3);
System.out.print("-------------------------------\n");
//Beginning of reward calculation
if (val1 == val2 && val2 == val3 && val3==val1)//check this condition first otherwise even after matching these values user never get more than double
{
System.out.println("\nNumber of matches: 1");
double doubleReward = (userBet1 * 2);
userTotal+=doubleReward;
System.out.printf("You have won: $%.2f", doubleReward);
System.out.printf("\nYou currently have: $%.2f", userTotal);
}
else if (val1 == val2 || val2 == val3 || val1 == val3)
{
System.out.println("\nNumber of matches: 3");
double tripleReward = (userBet1 * 3);
userTotal+=tripleReward;
System.out.printf("\nYou have won: $%.2f",tripleReward);
System.out.printf("\nYou currently have: $%.2f", userTotal);
}
else
{
System.out.println("\nNumber of matches: 0");
System.out.println("You have won: $0.00");
System.out.printf("You currently have: $%.2f", userTotal);
}
} while (userTotal > 0.00);
} //end of #1 option
user_input.close();
}
}
答案 3 :(得分:-2)
很简单,在循环开始时,您将总计分配为:
userTotal = (startingSum - userBet1);
这转换为
userTotal = ($100 - userBet1);
很明显,每轮结束后用户都会重置为100美元。
请改为尝试:
double userTotal = startingSum;
...
userTotal -= userBet1;
此代码还有其他几个问题(例如三个相同的case语句体,应该用循环压缩成一个),老实说,考虑在codereview.stackexchange.com上发布你的程序可能是值得的,作为您可以获得的反馈,可以为您提供更多学习Java和预防错误的方法,例如本程序中的错误。