2016-11-11 36 views
0

我昨天晚上問,但沒有得到任何答案最有可能由於我的問題格式不良所以我在這裏重新發布它更多可理解的背景;交換Java樂透機器號碼輸入骰子滾動,而不是像賭博機

我爲我的Java課程製作了Java樂透程序,然後我正在玩從我的Java書中製作的骰子程序,並且我想我可以通過替換數字來製作賭博機器用骰子輸入代替。我將如何去交換擲骰子的輸入數字?如果問題沒有意義,請告訴我。對不起,在短時間內發佈兩次。

import java.util.Random; 

import java.util.Scanner; 

class Lottery { 

private final int lotteryNumbers[]; 

public Lottery() { 

    Random rand = new Random(System.currentTimeMillis()); 
    lotteryNumbers = new int[2]; 
    for (int i = 0; i < lotteryNumbers.length; i++) { 
     lotteryNumbers[i] = Math.abs(rand.nextInt()) % 90; 
    } 
} 

public class Dice { 
    int die1; 
    int die2; 

    Scanner keyboard = new Scanner(System.in); 

    Random rand = new Random(); 
} 

public int compareNumbers(int[] usersNumbers) { 

    int match = 0; 
    if (usersNumbers.length == lotteryNumbers.length) { 
     for (int i = 0; i < lotteryNumbers.length; i++) { 
      if (usersNumbers[i] == lotteryNumbers[i]) { 
       match++; 
      } 
     } 
    } 
    return match; 
    } 

public int[] getLotteryNumbers() { 
    return lotteryNumbers; 
} 
    } 

    public class LottoMachine11 { 

public static void main(String[] args) { 

    String again = "y"; 

    Lottery lottery = new Lottery(); 
    int lotteryNumbersCount = lottery.getLotteryNumbers().length; 

    while (again.equalsIgnoreCase("y")) { 

     System.out.println("Lottery application\n"); 
     System.out.println("There are " + lotteryNumbersCount 
       + " numbers in range of 0 through 9. " 
       + "Try to guess all of them\n"); 

     Scanner keyboard = new Scanner(System.in); 
     Scanner kb = new Scanner(System.in); 
     int numbers[] = new int[lotteryNumbersCount]; 

     for (int i = 0; i < numbers.length; i++) { 
      System.out.print(String.format("Enter number %d: ", i + 1)); 
      numbers[i] = kb.nextInt(); 
     } 

     int match = lottery.compareNumbers(numbers); 

     if (match == lotteryNumbersCount) { 

      System.out.println("\nYou got all the numbers correct. You won the grand prize!"); 

     } else { 

      System.out.println("Uh oh, you only got " + match + " number(s)."); 
      System.out.print("Play again? (y = yes)? "); 
      again = keyboard.nextLine(); 
     } 
    } 
} 
+0

我爲初學者添加了骰子滾動類。我還添加了一個循環,但我希望它是一個數組中的循環,而不是重複的打印。 – Banba

回答

0

嗯,你會首先要1-6之間產生隨機骰子值,所以你骰子類應該像這樣的事情:

Dice { 
    int die1; 
    int die2; 

    Random rand = new Random(); 

    //Generates random value between 1 and 6 for dice1 and dice2 
    dice1 = rand.nextInt(6) + 1; 
    dice2 = rand.nextInt(6) + 1; 
} 

其次是你的getter方法以訪問dice1和dice2的值

然後要求用戶猜測骰子值並將用戶輸入與骰子捲進行比較。我不會爲你編碼整個事情,但我希望這會推動你朝着正確的方向發展!

+0

謝謝科迪。它確實有幫助,你的解釋是有道理的。我一定會努力一點。 – Banba