Привет и спасибо всем
INFO:
Эта программа предназначена для класса
Назначение - использовать массив для симуляции лотереи
Я использую массивы 1D для представления чисел в лотерейном билете
Методы, которые я использовал, были указаны, поэтому я не могу переместить fxn из одного метода в другой (например: я не мог определить, выиграл ли пользователь лотерею в основном методе, а не в назначенном методе)
ПРОБЛЕМА:
что я получаю - метод отображения показывает мой введенный пользователем массив, отсортированный от минимального к наибольшему
что я хочу - исходный массив, введенный пользователем
мой код:
import java.util.Scanner;
import java.util.Random;
public class Program2 {
/*notes
the numbers do not have to match in order
you must validate all user input
numeric output should be displayed with commas
the code should be written using efficient processing
use comments, naming conventions, and program structure
*/
//main method
public static void main(String[] args) {
Scanner kbr = new Scanner(System.in); //scanner object for user input
boolean auto = false;
int choice;
int[] yourTix;
int[] winningTix;
int tries = 0;
int[] freq = new int[20];
//display lottery info
System.out.println("Welcome to the Wacky Lottery Program!");
System.out.println("1 - Pick my own");
System.out.println("2 - Computer picks for me");
//validation loop
do {
choice = kbr.nextInt();
if (choice < 1 || choice > 2)
System.out.println("Invalid input. please enter either 1 or 2");
} while (choice < 1 || choice > 2);
//selecting automatic choice or not
if (choice == 2)
auto = false;
else
auto = true;
//generating your ticket
yourTix = yourTix(auto);
//this do/while loop keeps generating winning tickets until you win
do {
//get new winning ticket
winningTix = winningTix();
//check frequency of each number and add to the frequency accumulator array
for (int i = 0; i < winningTix.length; i++) {
//accumulate frequency @ index that is equal to winningTix[i]
int num = winningTix[i] - 1;
freq[num]++;
}
//accumulating the number of tries
tries++;
} while (win(yourTix, winningTix) == false);
//using the display method to print out the stats of this lottery
display(yourTix, winningTix, tries, freq);
}
//method that determines the user's ticket numbers
public static int[] yourTix(boolean auto) {
Random r = new Random();
Scanner kbr = new Scanner(System.in); //scanner object for user input
int[] tix = new int[5]; //creating a new array of integers with 5 integers
//this loop gets the user's input for each number on the ticket
for (int i = 0; i < tix.length; i++) {
if (auto == true) {
System.out.print("Please enter number " + (i + 1) + ": ");
do {
tix[i] = kbr.nextInt();
if (tix[i] < 1 || tix[i] > 20)
System.out.println("Invalid input. Please only enter 1-20");
} while (tix[i] < 1 || tix[i] > 20);
} else {
tix[i] = r.nextInt(19) + 1;
}
}
//return the ticket array
return tix;
}
//method to determine the winning numbers
public static int[] winningTix() {
Random r = new Random();
int[] winningTix = new int[5];
for (int i = 0; i < winningTix.length; i++)
winningTix[i] = r.nextInt(20) + 1;
return winningTix;
}
//method to determine if the user won
public static boolean win(int[] yt, int[] wt) {
boolean same = true;
int startScan, index, minIndex, minValue;
//sort user's array numerical order
for (startScan = 0; startScan < (yt.length - 1); startScan++) {
minIndex = startScan;
minValue = yt[startScan];
for (index = startScan + 1; index < yt.length; index++) {
if (yt[index] < minValue) {
minValue = yt[index];
minIndex = index;
}
}
yt[minIndex] = yt[startScan];
yt[startScan] = minValue;
}
//sort computer's array numerical order
for (startScan = 0; startScan < (wt.length - 1); startScan++) {
minIndex = startScan;
minValue = wt[startScan];
for (index = startScan + 1; index < wt.length; index++) {
if (wt[index] < minValue) {
minValue = wt[index];
minIndex = index;
}
}
wt[minIndex] = wt[startScan];
wt[startScan] = minValue;
}
index = 0; // Loop control variable
// determine whether the elements contain the same data
while (same && index < wt.length) {
if (yt[index] != wt[index])
same = false;
index++;
}
//return whether or not the winning array and the user's array matches
return same;
}
//method that displays and writes to file
public static void display(int[] winning, int[] user, int tries, int[] freq) {
//message indicating the win
System.out.println("You won!");
System.out.println();
//display winning lottery ticket numbers in their original order
System.out.println("Here is the winning ticket");
for (int i = 0; i < winning.length; i++)
System.out.println(winning[i]);
System.out.println();
//display user numbers in the original order
System.out.println("Here is your ticket");
for (int i = 0; i < user.length; i++)
System.out.println(user[i]);
System.out.println();
//the number of times it took to get a match with a winning ticket
System.out.printf("It took %,d tries to win", tries);
System.out.println();
//the number of times each value 1-20 was generated for a winning lottery ticket
for (int i = 0; i < freq.length; i++) {
System.out.printf("The number %d was generated %,d times", i + 1, freq[i]);
System.out.println();
}
//this should also be written to a file
//this should also be written to a file
//note: if tie, display all the numbers
}
}