2013-03-27 212 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個隨機發生器,會爲你的水果產生相同的隨機性。