如果格式不正确,我深表歉意,我试着做对了。我需要关于什么是错误的、无序的、错误的编写的建议......还不能通过编译器,每个“修复”似乎都会产生更多的错误。
我试图在 main 中编写一个 while 循环,只要用户表示他们想玩,它就会运行
在 while 循环中,我调用了 3 个方法:
生成随机数以将相应的 Case # 拉出 switch 语句以使用 for 循环获取 3 项
根据比赛和投注计算奖金,然后显示
找出玩家是否想继续播放以保持 while 循环继续进行或关闭它,
我会很高兴得到任何建议,
问题粘贴在代码的顶部,以便我正在处理的内容更加清晰。谢谢!
这就是问题:
~Slot Machine:
A slot machine is a gambling device into which the user inserts money and then pulls a lever or presses a button.
The slot machine then displays a set of random images. If two or more of the images match, the user wins an amount of money,
which the slot machine dispenses back to the user.
Design a program that simulates a slot machine. When the program runs, it should do the following:
* Ask the user to enter the amount of money she wants to insert into the slot machine.
* Instead of displaying images, the program will randomly select a word from the following list: Cherries, Oranges, Plums, Bells, Melons, Bars.
The program will select and display a word from this list three times (like the three columns in a real slot machine).
* If none of the randomly selected words match, the program will inform the user that she has won $0.
* If two of the words match, the program will inform the user that she has won twice the amount wagered in the first step.
* If all three of the words match, the program will inform the user that she has won three times the amount entered.
* The program will ask whether the user wants to play again.
If so, these steps are repeated.
If not, the program displays the total amount of money entered into the slot machine and the total amount won.
*/
这是修改后的代码,编译器错误在它们所针对的行号旁边被注释掉
import java.util.Scanner;
import java.util.Random;
public class SlotMachineHWCH6
{
public static void Main(String[] args)
{
//declarations
Scanner keyboard = new Scanner(System.in); //for user input
//string slotMachine; //don't know if will be used
//double initialPlayerBet; //first bet
double playerBet = 0; //subsequent bets
//double totalPlayerBets; //total amount user spent in the slot machine
double totalWinnings = 0; //total amount won
double tripleWin = 0; //amount won when all 3 match
double doubleWin = 0; //amount won when 2 match
String spin, spin1, spin2, spin3;
int i; //counter variable
String no;
String yes;
boolean keepPlaying; //response to prompt to continue playing
System.out.println("Slot machine payout: 3 items match - win triple your bet. 2 items match = win double your bet. No match, no win.\n");
48 while (keepPlaying) /*SlotMachineHWCH6.java:48: error: variable keepPlaying might not have been initialized
while (keepPlaying)*/
{
System.out.println("Enter your bet");
playerBet = keyboard.nextDouble();
55 for (i = 1; i <= 3; i++)/*SlotMachineHWCH6.java:55: error: variable spin might not have been initialized
getSlotItems(spin);*/
^
{
getSlotItems(spin); /*error: variable spin might not have been initialized*/
getSlotItems(spin);
56 System.out.println(spin1 + "," + spin2 + "," + spin3); /*56: error: variable spin1 might not have been initialized
System.out.println(spin1 + "," + spin2 + "," + spin3); + same for spin2 and spin3*/
}
59 spinWinnings(spin1, spin2, spin3);/*59: error: variable spin1 might not have been initialized
spinWinnings(spin1, spin2, spin3); and there are the same messages for spin2, spin3 */
60 playAgain(yes, no); /* 60: error: variable no might not have been initialized*/
playAgain(yes, no); /*same error for 'yes'*/
^
}
}
//get the random 3 slot items out of 6 possible
public static String getSlotItems(String spin)
{
//declarations
Random rand = new Random(); //random number generator
//map a random number to the items in the list
switch(rand.nextInt(6))
{
case 0:
spin = "Cherries";
System.out.println("Cherries");
break;
case 1:
spin = "Oranges";
System.out.println("Oranges");
break;
case 2:
spin = "Plums";
System.out.println("Plums");
break;
case 3:
spin = "Bells";
System.out.println("Bells");
break;
case 4:
spin = "Melons";
System.out.println("Melons");
break;
case 5:
spin = "Bars";
System.out.println("Bars");
break;
default:
System.out.println("ERROR ERROR ERROR");
break;
}
return spin;
}
public static void spinWinnings(String spin1, String spin2, String spin3)
{
if (spin1 == spin2 && spin2 == spin3)
{
//double doubleWin;
125 double tripleWin; /*variable playerBet might not have been initialized
tripleWin = playerBet * TRIPLE; /*variable playerBet might not have been initialized
tripleWin = playerBet * TRIPLE;*/
^
^
double playerBet;
double newTotal;
final int TRIPLE = 3; // to calculate winnings for 3 matches
tripleWin = playerBet * TRIPLE;
newTotal = playerBet + tripleWin;
System.out.println("3 match, you won $ "+ tripleWin);
System.out.println("You have $ " + newTotal);
newTotal++;
}
else if (spin1 == spin2 || spin2 == spin3 || spin3 == spin1)
{
double doubleWin;
double playerBet;
double newTotal;
final int DOUBLE = 2; // to calculate winnings for 2 matches
140 doubleWin = playerBet * DOUBLE; /*140: error: variable playerBet might not have been initialized
doubleWin = playerBet * DOUBLE;*/
newTotal = playerBet + doubleWin;
System.out.println("2 match, you won $ "+ doubleWin);
System.out.println("You have $ " + newTotal);
newTotal++;
}
else
{
System.out.println("None match, you won absolutely nothing.");
}
}
public static boolean playAgain(String yes, String no)
{
Scanner keyboard = new Scanner(System.in);
String playSomeMore;
////String yes;
// String no;
System.out.println("Play again? Enter yes or no.");
playSomeMore = keyboard.nextLine();
if (playSomeMore.equals("yes"))
{
return true;
}
else
{
return false;
}
}
我不明白为什么我必须在 if 和 else 语句中声明变量......它们被认为是方法吗?
这看起来是否已经接近应有的运行?
我注释掉的一些东西一旦工作我就会删除,一些是我认为我需要但没有使用的变量,其中一些我正在四处移动以查看我是否将它们放在错误或正确的位置。