Java彩票程序,无法比较输出

Java彩票程序,无法比较输出,java,arrays,loops,Java,Arrays,Loops,在我的第一节计算机科学课上,我必须做一个“即时彩票”计划。整个学期我的教授都在逐字逐句地读这本书,所以说实话,现在我有点不知所措了。我知道怎么做大部分工作,但我只是在计算数组排序以及如何比较用户输入和随机数输出时遇到了麻烦。我的教授拒绝回答关于带回家作业的问题,并且禁止使用除数组、循环和math.random之外的任何东西——因此没有集合或任何更复杂的东西可以帮助我。我见过其他编译的程序,但都是集合 我有用于用户输入彩票号码和生成随机号码输出的代码。我也很可能知道如何使用if/else打印支出。

在我的第一节计算机科学课上,我必须做一个“即时彩票”计划。整个学期我的教授都在逐字逐句地读这本书,所以说实话,现在我有点不知所措了。我知道怎么做大部分工作,但我只是在计算数组排序以及如何比较用户输入和随机数输出时遇到了麻烦。我的教授拒绝回答关于带回家作业的问题,并且禁止使用除数组、循环和math.random之外的任何东西——因此没有集合或任何更复杂的东西可以帮助我。我见过其他编译的程序,但都是集合

我有用于用户输入彩票号码和生成随机号码输出的代码。我也很可能知道如何使用if/else打印支出。我只需要知道如何让程序来比较这些数字,以确定用户是否是“赢家”

import java.util.Scanner;
公共类厕所{
公共静态void main(字符串[]args){
扫描器键盘=新扫描器(System.in);//用户输入他们的彩票号码
System.out.print(“输入数字1:”);
int num1=keyboard.nextInt();
系统输出打印(“输入数字2:”);
int num2=keyboard.nextInt();
系统输出打印(“输入数字3:”);
int num3=keyboard.nextInt();
系统输出打印(“输入数字4:”);
int num4=keyboard.nextInt();
系统输出打印(“输入数字5:”);
int num5=keyboard.nextInt();
系统输出打印(“输入数字6:”);
int num6=keyboard.nextInt();
}
int[]彩票=新int[6];
int随机数;
{
对于(int i=0;i<6;i++){
randomNum=(int)(Math.random()*50);//此处创建的随机数。
对于(int x=0;x

最后一个程序应该让用户输入6个数字,该程序比较比赛的数字,判断用户是否是“赢家”,显示奖品,另外一件事是显示他们花了多少钱(每张“票”是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();
         }
publicstaticvoidmain(系统[]参数){
扫描仪输入=新扫描仪(系统输入);
int[]彩票=新int[6];
System.out.println(“输入”+lotking.length+“数字:”);
for(int i=0;i<1.length;i++){
彩票[i]=in.nextInt();
}

具体的问题与如何进行比较和计算“赢家”有关。不清楚“赢家”的定义是什么

根据我的评论,如@szoore的回答所示,我将使用数组来收集输入。我将使用方法来收集(因为可以更改为使用不同的方法进行选择,这使得测试更容易)

示例
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
    //
}
publicstaticvoidmain(字符串[]args)
{
//彩票有多少种选择,这里是6
最终整数=6;
//此方法从用户获得
int[]用户条目;
userEntries=getUserSelections(numEntries);
排序(用户条目);
//显示用户选择
输出阵列(用户条目);
int[]彩票=获取彩票号码(numEntries);
分类(彩票);
//显示彩票号码
输出阵列(彩票);
//看有多少场比赛
int匹配=0;
//查看彩票中是否存在用户条目;如果存在,则
//比赛
对于(int i=0;i
当你说“还有更复杂的吗?”,这是否意味着您应该自己执行排序?另外,我会将从用户获得的数字读入数组,而不是6个不同的变量——这将使以后的比较更加容易。这非常有用,谢谢!支付必须是if/else,因为他也没有说switch语句。我感谢您的支持救命啊!
/**
 * 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;
}
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;
}
    // 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);
/**
 * 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();
}
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
    //
}