我昨天晚上問,但沒有得到任何答案最有可能由於我的問題格式不良所以我在這裏重新發布它更多可理解的背景;交換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();
}
}
}
我爲初學者添加了骰子滾動類。我還添加了一個循環,但我希望它是一個數組中的循環,而不是重複的打印。 – Banba