Java老虎机程序 - 现在要纠正什么?

时间:2014-03-11 11:38:21

标签: java jgrasp

如果格式不正确,我道歉,我试着做对了。我需要有关错误,乱序,错误编写的建议......无法通过编译器,每次“修复”似乎都会产生更多错误。

我试图在main中编写一个while循环,只要用户指示他们想要播放

在while循环中,我调用了3个方法:

  1. 生成随机数,将相应的Case#拉出switch语句,使用for循环获取3个项目

  2. 根据比赛和投注计算奖金然后显示

  3. 查明玩家是否想继续玩游戏以保持while循环播出或关闭播种,

  4. 我会很高兴有任何建议,

    问题被粘贴在代码的顶部,这样我正在处理的内容会更清晰。 谢谢!

    这是问题所在:

    ~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语句中声明变量......它们被认为是方法吗?

    这看起来是否接近正常运行?

    我注意到的一些事情我会删除一旦它正在工作,有些是变量我认为我需要但没有使用,其中一些我正在四处走动,看看我是否有错误或正确的位置。

4 个答案:

答案 0 :(得分:1)

除了所有的语法和循环错误,通过一点谷歌搜索,阅读和耐心,你最终会修复(特别注意while (keepPlaying = true)由于赋值而产生无限循环),唯一的严重的问题是你使用随机数生成器:

  Random rand = new Random();  //random number generator

这将从头开始创建一个新的生成器时间,这完全破坏了生成器的统计属性。最好的办法是将这个随机数生成器作为您班级中的一个字段。

答案 1 :(得分:1)

由于其他人已经在作业中发布了问题,我将回答这个问题:

&#34;我不明白为什么我必须在if和else语句中声明变量......它们被认为是方法吗?&#34;

这称为&#34; lexical scoping&#34;并且,不,if / else块不是方法,但是像方法,if / else,switch,for循环等等都使用它们自己的局部变量分辨率处理。

答案 2 :(得分:0)

首先,在你的main方法中,检查是否继续播放的条件应该只是while (keepPlaying),因为你现在获得的是=而不是平等检查==

此外,keepPlaying方法应更改为以下内容:

public static boolean keepPlaying()
   {
      Scanner keyboard = new Scanner(System.in);
      String playAgain;

      System.out.println("Play again? Enter yes or no.");
      playAgain = keyboard.nextLine();

      return (playAgain.equals("yes"));
   }

并且您希望使用keepPlaying = keepPlaying()来调用它,但重命名它也是一个好主意,因为您已经拥有一个具有该名称的变量。

答案 3 :(得分:0)

错误来自以下事实:当代码中使用变量时,变量处于空值。主循环不会启动,因为你有“boolean keepPlaying;”,这很好地作为变量的命名,但它将它赋值为空值。所以,当你要求java检查keepPlaying是否为true时,它不知道keepPlaying是什么。你应该在顶部设置keepPlaying为true(boolean keepPlaying = true;),因此while循环将开始。然后,当您希望游戏结束时,只需将其设置为false即可。幸运的是,您的错误很容易修复! :)但是,当你想不出什么东西时,我知道沮丧的感觉。

就其他错误而言,这是同一个问题。因此,对于那些,只需将它们设置为虚拟值,例如ints为0,字符串为“”。现在可以设置它们,因为它们会在游戏开始时被更改。

我还想指出,将游戏置于主要方法之外可能是一个好主意。初始化类构造函数中的所有变量,然后在main方法中创建一个新对象(slotMachine game = new slotMachine())。然后你可以有一个start()方法(在main方法中调用它:slotMachine.start()),它将keepPlaying设置为true并调用run方法。我说这是因为它远离静态变量。现在,您声明的每个变量都是静态的,因为它位于静态void main下。这只是一个偏好,但我讨厌必须解决静态变量,因此我通常有一个名为Main的类,其主要的void构成了Game类,它具有运行时。

干得好,祝你好运!