我覺得我走的是同樣的介紹Java類,你(作爲程序看起來出奇的熟悉),所以我會盡量保持它只是基本的東西;然而,我從程序中複製了所有的原始代碼,所以你基本上做了所有的工作,我只是把它清理了一下。
這是一個完整的程序(隨機化一組數字(檢查重複),允許用戶選擇一套或隨機化自己的(也檢查重複),排序和比較兩套,通知用戶是否他們贏了還是不行,要求再次上場:
import java.util.*;
public class Lotto
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
Random rand = new Random();
boolean checkPlay = false, readInput = false;
final int MAX = 45;
while (readInput != true)
{
System.out.print("Would you like to buy a lotto ticket? [y/n] ");
String stBuyTicket = keyboard.nextLine().trim().toLowerCase();
if (stBuyTicket.isEmpty())
{
System.out.println("Invalid Input.");
}
else if (stBuyTicket.charAt(0) == 'y' || stBuyTicket.charAt(0) == 'n')
{
if (stBuyTicket.charAt(0) == 'y')
{
checkPlay = true;
}
else
{
checkPlay = false;
}
readInput = true;
}
else
{
System.out.println("Invalid Input.");
}
}
while (checkPlay == true)
{
int[] lotto = new int[6];
int lottoLength = lotto.length;
int[] userLotto = new int[6];
int userLottoLength = userLotto.length;
for (int i = 0; i < lottoLength; i++)
{
boolean checkLotto = false;
while (checkLotto != true)
{
int numCheck = (rand.nextInt(MAX) + 1);
boolean confirmSame = false;
for (int j = 0; j <= i; j++)
{
if (numCheck == lotto[j])
{
confirmSame = true;
}
}
if (confirmSame != true)
{
lotto[i] = numCheck;
checkLotto = true;
}
}
}
readInput = false;
while (readInput != true)
{
System.out.println("Would you like choose your own numbers or just randomize them?");
System.out.print("Choose your own numbers? [y/n] ");
String stBuyTicket = keyboard.nextLine().trim().toLowerCase();
if (stBuyTicket.isEmpty())
{
System.out.println("Invalid Input.");
}
else if (stBuyTicket.charAt(0) == 'y' || stBuyTicket.charAt(0) == 'n')
{
if (stBuyTicket.charAt(0) == 'y')
{
for (int i = 0; i < userLottoLength; i++)
{
boolean checkUserLotto = false;
while (checkUserLotto != true)
{
System.out.print("Which number would you like to choose for number " + (i + 1) + ": ");
String numUserInput = keyboard.nextLine().trim();
int numUserInputLength = numUserInput.length();
boolean checkInput = true;
if (numUserInputLength > 2 || numUserInputLength < 1)
{
System.out.println("Invalid Input. Try again.");
checkInput = false;
}
else
{
for (int j = 0; j < numUserInputLength; j++)
{
if (Character.isDigit(numUserInput.charAt(j)) != true)
{
System.out.println("Invalid Input. Try again.");
checkInput = false;
}
}
}
if (checkInput == true)
{
int userInput = Integer.parseInt(numUserInput);
if (userInput > MAX || userInput < 1)
{
System.out.println("Invalid Input. Try again.");
}
else
{
boolean confirmSame = false;
for (int j = 0; j <= i; j++)
{
if (userInput == userLotto[j])
{
System.out.println("You've already choosen this number. Choose again.");
confirmSame = true;
}
}
if (confirmSame != true)
{
userLotto[i] = userInput;
checkUserLotto = true;
}
}
}
}
}
}
else
{
for (int i = 0; i < userLottoLength; i++)
{
boolean checkLotto = false;
while (checkLotto != true)
{
int numCheck = (rand.nextInt(MAX) + 1);
boolean confirmSame = false;
for (int j = 0; j <= i; j++)
{
if (numCheck == userLotto[j])
{
confirmSame = true;
}
}
if (confirmSame != true)
{
userLotto[i] = numCheck;
checkLotto = true;
}
}
}
}
readInput = true;
System.out.print("Your lotto numbers are: " + userLotto[0]);
for (int i = 1; i < userLottoLength; i++)
{
System.out.print(", " + userLotto[i]);
}
System.out.print("!");
System.out.println();
System.out.print("And the winning lotto numbers are: " + lotto[0]);
for (int i = 1; i < lottoLength; i++)
{
System.out.print(", " + lotto[i]);
}
System.out.print("!");
System.out.println();
Arrays.sort(lotto);
Arrays.sort(userLotto);
if (Arrays.equals(userLotto, lotto) == true)
{
System.out.println("Your lotto numbers match the winning lotto numbers! \nYou win!");
}
else
{
System.out.println("Your lotto numbers do not match the winning lotto numbers. \nYou lose.");
}
}
else
{
System.out.println("Invalid Input.");
}
}
readInput = false;
while (readInput != true)
{
System.out.print("Would you like to buy another lotto ticket? [y/n] ");
String stBuyTicket = keyboard.nextLine().trim().toLowerCase();
if (stBuyTicket.isEmpty())
{
System.out.println("Invalid Input.");
}
else if (stBuyTicket.charAt(0) == 'y' || stBuyTicket.charAt(0) == 'n')
{
if (stBuyTicket.charAt(0) == 'y')
{
checkPlay = true;
}
else
{
checkPlay = false;
}
readInput = true;
}
else
{
System.out.println("Invalid Input.");
}
}
}
System.out.println("That's a good decision. Save your money");
System.exit(0);
}
}
以下是隨機化樂透集而不repitition部分:
for (int i = 0; i < lottoLength; i++) {
boolean checkLotto = false;
while (checkLotto != true) {
int numCheck = (rand.nextInt(MAX) + 1);
boolean confirmSame = false;
for (int j = 0; j <= i; j++) {
if (numCheck == lotto[j]) {
confirmSame = true;
}
}
if (confirmSame != true) {
lotto[i] = numCheck;
checkLotto = true;
}
}
}
它會產生一些與int numCheck = (rand.nextInt(MAX) + 1)
,並檢查數量對每一個輸入樂透號碼到目前爲止:
for (int j = 0; j <= i; j++) {
if (numCheck == lotto[j]) {
confirmSame = true;
}
}
如果numCheck
不等於任何輸入的號碼時,則numCheck
被寫入到當前的陣列點。
Tomasz,非常感謝,我現在看到它。無法保持它可能,但我必須堅持我的課程文本,我們沒有涵蓋集合,所以我不能使用它,似乎愚蠢,但多數民衆贊成在他們想要的方式,只有2個月,所以我們肯定會覆蓋這一切。 – user1958789