Java老虎机

时间:2013-03-27 18:51:44

标签: java

我在CS课上遇到了家庭作业问题。我应该制作一台老虎机,我不能继续工作,我得到扫描仪nextLine方法跳过线并保留那里但它不会让我输入任何东西它只是结束了程序。由于我的教授需要格式化,我还必须将此方法拆分并使其为30行或更少。我还必须保留奖金的总计,但我无法弄清楚如何为累积奖金做到这一点。

/**
 * ProgrammingAssignment2.java
 * 
 * @author Corey Goff 
 * @version March 2013
 */
import java.util.Random;
import java.util.Scanner;
/**
* This class simulates a slot machine.
*/
public class SlotMachine
{
   /**
    * This is the main method.
    * 
    * @param args
    */
   public static void main(String[] args)
   {
       Scanner keyboard = new Scanner(System.in);
       Random random = new Random();
       String cont = "n";
       char answer;
       int coin = 0;
       int totalEntered = 0;
       int a;
       int b;
       int c;
       int n;
       int amountWon = 0;
       int dubs = coin * 2;
       int trips = coin * 4;

       while (cont.equals("n"))
       {
           a = random.nextInt(6);
           b = random.nextInt(6);
           c = random.nextInt(6);
           n = random.nextInt(991) +10;
           totalEntered += coin;
           System.out.println("How much would you like to bet? ");
           coin = keyboard.nextInt();

           switch (a) 
           {
               case 0:
                   System.out.println("Cherry");
                   break;
               case 1:
                   System.out.println("Orange");
                   break;
               case 2:
                   System.out.println("Plum");
                   break;
               case 3:
                   System.out.println("Bell");
                   break;
               case 4:
                   System.out.println("Melon");
                   break;
               default:
                   System.out.println("Bar");
           }

           switch (b) 
           {
               case 0:
                   System.out.println("Cherry");
                   break;
               case 1:
                   System.out.println("Orange");
                   break;
               case 2:
                   System.out.println("Plum");
                   break;
               case 3:
                   System.out.println("Bell");
                   break;
               case 4:
                   System.out.println("Melon");
                   break;
               default:
                   System.out.println("Bar");
           }

           switch (c) 
           {
               case 0:
                   System.out.println("Cherry");
                   break;
               case 1:
                   System.out.println("Orange");
                   break;
               case 2:
                   System.out.println("Plum");
                   break;
               case 3:
                   System.out.println("Bell");
                   break;
               case 4:
                   System.out.println("Melon");
                   break;
               default:
                   System.out.println("Bar");
           }

           if (a != b && a != c && b != c)
           {
               System.out.println("You have won $0");
           }
           else if (a == b || a == c || b == c)
           {
               System.out.println("Congratulations, you have won $" + dubs);
                  amountWon += dubs;
           }
           else if (a == b && a == c && a != 0)
           {
               System.out.println("Congratulations, you have won $" + trips);
                  amountWon += trips;
           }
           else if (a == 0 && b == 0 && c == 0)
           {
               System.out.println("Congratulations! You have won the jackpot of $" 
                   + (coin * n));

           }

           System.out.println("Continue? y/n ");
           cont = keyboard.nextLine();
       }
   }
}

2 个答案:

答案 0 :(得分:2)

调用keyboard.nextInt()后,必须调用keyboard.nextLine()将换行符转储到缓冲区中。 nextInt()读取,直到找到不能成为int的一部分的东西,并在int位于缓冲区之后留下所有内容。

有关详情,请参阅此帖子:Reading Strings next() and nextLine() Java

答案 1 :(得分:0)

让代码工作(几乎没有变化): 使用keyboard.next();代替keyboard.nextLine();

将您的初始cont =“n”更改为cont =“y”,并使用while循环检查是否(cont.equals(“y”))。

这是一个快速解决方案,可以为效率和跟上标准做出许多改变,但我不会详细说明。

至于30行以下的要求,我首先只使用一个随机生成器在一个结构中生成所有3个水果。问问自己,为什么每个水果必须有单独的随机发生器?为什么每个水果换一个新的?一个随机与3个随机生成器,将为您的水果产生相同的随机性。

相关问题