0

如果格式不正确,我深表歉意,我试着做对了。我需要关于什么是错误的、无序的、错误的编写的建议......还不能通过编译器,每个“修复”似乎都会产生更多的错误。

我试图在 main 中编写一个 while 循环,只要用户表示他们想玩,它就会运行

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

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

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

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

这看起来是否已经接近应有的运行?

我注释掉的一些东西一旦工作我就会删除,一些是我认为我需要但没有使用的变量,其中一些我正在四处移动以查看我是否将它们放在错误或正确的位置。

4

5 回答 5

1

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

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

这每次都会从头开始创建一个新的生成器,这完全破坏了生成器的统计特性。最好的办法是将这个随机数生成器作为类中的一个字段。

于 2014-03-11T11:43:17.253 回答
1

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

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

这称为“词法作用域”,不,if/else 块不是方法,但与方法一样,if/else、switch、for 循环等都使用它们自己的局部变量解析来处理。

于 2014-03-11T12:26:16.180 回答
0

错误来自这样一个事实,即在代码中使用变量时它们为空值。主循环不会启动,因为您有“boolean keepPlaying;”,它作为变量的命名很好,但它会将它分配给一个空值。所以,当你让java检查keepPlaying是否为真时,它不知道keepPlaying是什么。您应该在顶部将 keepPlaying 设置为 true (boolean keepPlaying = true;),这样才会开始 while 循环。然后,当您希望游戏结束时,只需将其设置为 false。幸运的是,您的错误很容易修复!:) 我知道当你想不通时的挫败感,尽管如此。

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

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

干得好,祝你好运!

于 2014-03-11T14:39:41.073 回答
0

方法、if语句、for循环都是的示例,几乎所有块的工作方式都与变量范围相同。

变量对其所在的块和该块内的块可见。

变量对于它所在的块之外的块是不可见的。

你甚至不需要一个方法,if等等for来定义一个块——你可以自己写一个块,仅仅因为你想要一个而创建一个范围。

{
    // 'var' is not visible here
    {
        int var = 5;
        // var is visible here
        {
             // var is also visible here
        }
    }
}

始终尝试在尽可能小的范围内声明变量。这意味着在代码中的任何特定位置,可见的变量更少,因此混淆程序员的机会也更少。

于 2014-03-11T11:57:18.300 回答
0

首先,在你的main方法中,检查是否继续玩的条件应该就像while (keepPlaying)你现在得到的是一个 assignment=而不是一个相等性检查==

此外,该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(),但重命名它也是一个好主意,因为您已经有一个具有该名称的变量。

于 2014-03-11T11:48:21.243 回答