我必须在我的第一门计算机科学课上做一个“即时彩票”程序。我的教授整个学期都读了这本书的逐字记录,所以现在我确实有些失落。我知道如何执行大部分操作,但是在弄清楚数组排序以及如何比较用户输入和随机数输出方面遇到困难。我的教授拒绝回答有关家庭作业的问题,并禁止使用除数组,循环和math.random之外的任何内容,因此,没有任何集合或任何更复杂的东西可以提供帮助。我看过其他可以编译的程序,但是都带有集合。
我有用于用户输入彩票号码并生成随机数输出的代码。我最有可能还会弄清楚如何使用if / else打印付款。我只需要知道如何获得该程序来比较这些数字,从而确定用户是否是“赢家”。
import java.util.Scanner;
public class TheLottery {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in); //user input of their lottery numbers
System.out.print("Enter number 1: ");
int num1 = keyboard.nextInt();
System.out.print("Enter number 2: ");
int num2 = keyboard.nextInt();
System.out.print("Enter number 3: ");
int num3 = keyboard.nextInt();
System.out.print("Enter number 4: ");
int num4 = keyboard.nextInt();
System.out.print("Enter number 5: ");
int num5 = keyboard.nextInt();
System.out.print("Enter number 6: ");
int num6 = keyboard.nextInt();
}
int[] lottery = new int[6];
int randomNum;
{
for (int i = 0; i < 6; i++) {
randomNum = (int) (Math.random() * 50); // Random number created here.
for (int x = 0; x < i; x++) {
if (lottery[x] == randomNum) // Here, code checks if same random number generated before.
{
randomNum = (int) (Math.random() * 50);// If random number is same, another number generated.
x = -1; // restart the loop
}
}
lottery[i] = randomNum;
}
for (int i = 0; i < lottery.length; i++)
System.out.print(lottery[i] + " "); //print random numbers
}
}
最终程序应让用户输入6个数字,该程序会比较数字以进行匹配,弄清楚用户是否是“赢家”,显示奖品,以及增加的东西是显示他们花了多少钱(每个“票” '是$ 1),而他们赢了多少。到目前为止,所有输出都是扫描仪和随机数
答案 0 :(得分:1)
您似乎获得了六个数字,然后没有使用它们。您的彩票数组会自动初始化为零。我认为您正在尝试将一个数组与一个随机数组的输入进行比较,因此您需要一个循环才能将输入的值放入其中。完成之后,初始化您的随机数组,然后只需比较数组即可。
public static void main(System[] args) {
Scanner in = new Scanner(System.in);
int[] lottery = new int[6];
System.out.println("Enter " + lottery.length + " numbers: ");
for (int i = 0; i < lottery.length; i++) {
lottery[i] = in.nextInt();
}
答案 1 :(得分:0)
具体问题与如何进行比较并确定“赢家”有关。尚不清楚“胜利者”的定义是什么。
根据我的评论,如@szoore的回答所示,我将使用数组来收集输入。我会使用一种收集方法(因为可以更改以使用其他方法进行选择,这使测试更加容易。)
/**
* Obtain the specified number of entries from the user
*/
public static int[] getUserSelections(final int numSelections)
{
Scanner in = new Scanner(System.in);
// read N entries from the user
int[] nums = new int[numSelections];
// NOTE: no error processing in this loop; should be refined
// bad numbers (e.g., negative, too large), duplicate entries, etc.
// need to be removed
for (int i = 0; i < numSelections; ++i) {
System.out.print("Enter number " + (i + 1) + ": ");
nums[i] = in.nextInt();
}
return nums;
}
OP对于彩票号码具有基本的生成方式,但是我还是要使用一种方法。通过使用一种方法,它对重复检查进行了一些细化,并且还允许稍后使用相同的重复检查方法来检查匹配项:
public static int[] getLotteryNumbers(final int numSelections)
{
// the largest number to be selected; all numbers between
// 1 and maxNum (inclusive) will have equal(-ish) probability
// of being generated
final int maxNum = 50;
int[] lottery = new int[numSelections];
Random rnd = new Random();
// make N random selections, and ensure we don't have duplicates
for (int i = 0; i < numSelections; ++i) {
boolean generate = true;
while (generate) {
int sel = rnd.nextInt(maxNum) + 1;
generate = numberInArray(sel, lottery);
if (! generate) {
lottery[i] = sel;
}
}
}
return lottery;
}
/**
* Returns true if the specific queryNum is found in the pastSelections
* Could be slightly optimized by passing how many selections have
* already been made
*/
public static boolean numberInArray(int queryNum, int[] pastSelections)
{
// look at each element and see if already there; exit via return
// if so
for (int i = 0; i < pastSelections.length; ++i) {
if (pastSelections[i] == queryNum) {
return true;
}
}
return false;
}
然后使用方法'numberInArray'可以很容易地检查有多少个数字匹配:
// see how many match
int matches = 0;
// see if the user entry exists in the lottery; if so, we
// have a match
for (int i = 0; i < userEntries.length; ++i) {
if (numberInArray(userEntries[i], lottery)) {
++matches;
}
}
System.out.printf("Found %2d matches%n", matches);
使用if/else
或(可能更好)基于匹配数的switch
直接确定支出等。
此外,条目和彩票选择可能应该进行排序。尚不清楚是否可以使用内置排序。适当地编写sort
方法:
/**
* Sorts the array; implement sorting as needed
*/
public static void sort(int[] arr)
{
Arrays.sort(arr);
}
/*
* outputs the array if one cannot use Arrays.toString(arr)
*/
public static void outputArray(int[] arr)
{
for (int i = 0; i < arr.length; ++i) {
System.out.printf("%2d ", arr[i]);
}
System.out.println();
}
示例main
:
public static void main(String[] args)
{
// how many options for the lottery; here it is 6
final int numEntries = 6;
// this method obtains from user
int[] userEntries;
userEntries = getUserSelections(numEntries);
sort(userEntries);
// display User selections
outputArray(userEntries);
int[] lottery = getLotteryNumbers(numEntries);
sort(lottery);
// display lottery numbers
outputArray(lottery);
// see how many match
int matches = 0;
// see if the user entry exists in the lottery; if so, we
// have a match
for (int i = 0; i < userEntries.length; ++i) {
if (numberInArray(userEntries[i], lottery)) {
++matches;
}
}
System.out.printf("Found %2d matches%n", matches);
//
// TODO: calculate winnings based upon the number of matches
//
}