1

我的 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();
       }
   }
}
4

2 回答 2

2

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

有关更多信息,请参阅这篇文章:读取字符串 next() 和 nextLine() Java

于 2013-03-27T18:59:44.157 回答
0

让你的代码工作(几乎没有改变):使用keyboard.next();而不是keyboard.nextLine();

将您的初始 cont = "n" 更改为 cont = "y",并让 while 循环检查 if (cont.equals("y"))。

这是一个快速修复,可以进行许多更改以提高效率并跟上标准,但我不会详细介绍。

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

于 2013-03-27T19:04:14.837 回答