Java老虎机

Java老虎机,java,Java,我在CS课上遇到了一个家庭作业问题。我应该做一台老虎机,但我不能让它继续工作,我知道扫描器下一行方法跳过了这一行,保留了那里的内容,但它不允许我输入任何内容,它只是结束了程序。我还必须把这个方法分成30行或更少,因为我的教授要求格式化。我还必须保持一个总奖金,但我不知道如何做到这一点的头奖 /** * ProgrammingAssignment2.java * * @author Corey Goff * @version March 2013 */ import java.uti

我在CS课上遇到了一个家庭作业问题。我应该做一台老虎机,但我不能让它继续工作,我知道扫描器下一行方法跳过了这一行,保留了那里的内容,但它不允许我输入任何内容,它只是结束了程序。我还必须把这个方法分成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();
       }
   }
}

调用keyboard.nextLine()后,必须调用keyboard.nextLine()将换行符转储到缓冲区中。nextInt()一直读取,直到找到不属于int的内容,并将int之后的所有内容保留在缓冲区中

有关更多信息,请参阅本文:

要使代码正常工作(只需稍作更改): 使用
键盘.next()而不是
键盘.nextLine()

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

这是一个快速解决方案,为了提高效率和跟上标准,可以进行许多更改,但我不会详细介绍


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

为什么您都投了反对票?至少让OP知道他如何改进这个问题。谢谢你,这就解决了这个问题。