2013-03-27 106 views
-1

我遇到了CS课的作业问题。我应该制作一台老虎机,但我无法继续工作,我得到扫描仪nextLine方法跳过线,并保持那里有什么,但它不会让我输入任何东西,它只是结束程序。由于我的教授需要格式化,所以我也必须将这种方法分开,并将其设置为30行或更少。我也必须保持赢得奖金总额,但我无法弄清楚如何为大奖做。Java老虎机

/** 
* 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(); 
     } 
    } 
} 
+3

你为什么全部投票? Atlest让OP知道他如何改进这个问题。 – Smit 2013-03-27 18:55:05

回答

2

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

看到这个职位的详细信息:Reading Strings next() and nextLine() Java

+0

谢谢,解决了这个问题。 – Corey211 2013-03-27 19:13:55

0

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

改变您最初的续= “N” 到CONT =“ y“,并有while循环检查if(cont.equals(”y“))。

这是一个快速修复,有很多可以提高效率和跟上标准的变化,但我不会详细讨论。

至于30线以下的要求,我会开始只使用一个随机发生器在一个结构中生成所有3个水果。问问自己,为什么你必须为每种水果分别设置随机发生器?为什么每个水果有新的开关?一个随机vs3个随机发生器,会为你的水果产生相同的随机性。