简体   繁体   English

试图从另一个类的随机数打印出数组

[英]trying to print out array from random numbers from another class

I have basically finished this piece of code, but when I print out the numbers from the array in the lottery class, I get a bunch of seeming gibberish. 我基本上已经完成了这段代码,但是当我从彩票类中的数组中打印出数字时,我得到了一堆看上去很乱的东西。 How can I fix the problem? 我该如何解决该问题?

import java.util.Scanner;
public class Hw5pr2
{
    public static void main(String[] args)
    {
    Scanner kb = new Scanner(System.in);
    int[] rand = new int[5];
    System.out.println("please enter 5 number");
    for (int a = 0; a<rand.length; a++)
    {
    rand[a] = kb.nextInt();
    }
    Lottery k = new Lottery();
    System.out.print("your number are: ");
    for (int a = 0; a < rand.length; a++)
    {
        System.out.print(rand[a]+",");
    }
    System.out.print("The Winning numbers are: ");
    for (int a = 0; a < rand.length; a++)
    {
        System.out.print(k.getArray()+",");
    }
    System.out.println("you have " + k.RanInput(rand) + " matching number!!");
    }
}



import java.util.Random;
public class Lottery
{
private int[] lotteryNumbers = new int[5];
    public Lottery()
    {
    Random rand = new Random();
    for (int a = 0; a<lotteryNumbers.length; a++)
        {
        lotteryNumbers[a] = rand.nextInt(9)+1;
        }
    }
    public int RanInput(int[] Inran)
    {
    int b = 0;
    for (int a = 0; a<lotteryNumbers.length; a++)
        {
        if (lotteryNumbers[a] == Inran[a])
            {
            b++;
            }
        }
    return b;
    }
    public int[] getArray()
    {
    return lotteryNumbers;
    }
}
for (int a = 0; a < rand.length; a++)
{
    System.out.print(k.getArray()+","); //k.getArray() which returns you an entire array
}

You are trying to print the entire array. 您正在尝试打印整个阵列。 That's why it may show you the reference instead. 因此,它可能会向您显示参考。 You can store the array in a temp array and print the contents of that array. 您可以将数组存储在临时数组中并打印该数组的内容。


You may try doing something like this: 您可以尝试执行以下操作:

int[] winningNum = k.getArray();
for (int a = 0; a < winningNum.length; a++)
    System.out.print(winningNum[a] + ",");

try something like 尝试类似

System.out.print("The Winning numbers are: "); for (int a = 0; a < rand.length; a++) { System.out.print(k.getArray()[a]+","); }

note here 注意这里

 for (int a = 0; a<lotteryNumbers.length; a++)
 {
    if (lotteryNumbers[a] == Inran[a])
    {
        b++;
    }
 }
 return b;

this is always return lotteryNumbers.length-1; 这总是返回lotteryNumbers.length-1; beacause always two arrays are same 因为总是两个数组相同

When you print out the array, to the best of my knowledge, it calls the toString method. 据我所知,当您打印出数组时,它将调用toString方法。 This returns a hash of the object, not the values it contains. 这将返回对象的哈希值,而不是其包含的值。 You can call the static method toString from the Arrays class also to print the string values of the array. 您也可以从Arrays类调用静态方法toString来打印数组的字符串值。

        System.out.print(Arrays.toString(k.getArray())+",");

or use a for loop 或使用for循环

for (int a = 0; a < k.getArray().length; a++)
    {
        System.out.print(k.getArray()[a]+",");
    }   

A few things to note: 注意事项:

As others have mentioned, if you want to print the elements of an array, a simple way to do it is with a for loop. 正如其他人提到的,如果要打印数组的元素,一种简单的方法是使用for循环。 Your code won't work the way you want it to unless you access each element of k's lotteryNumbers array separately: 除非您分别访问k的lotteryNumbers数组的每个元素,否则您的代码将无法按您希望的方式工作:

System.out.print("The winning numbers are: ");
for (int a = 0; a < rand.length; a++)
    {
        System.out.print(k.getArray()[a]); //make sure to include the [a]
        if(a != rand.length - 1)   //added to help make prints more readable
            System.out.print(", ");
    }

Secondly, your RanInput function won't behave the way you want it to, assuming you wanted to see how many of the user's numbers match the lottery numbers. 其次,假设您想查看有多少用户号码与彩票号码匹配,RanInput函数将不会按照您希望的方式运行。 Currently, it just checks if the nth number inputted matches the nth lotto number chosen, which is unlikely to be true. 当前,它仅检查输入的第n个数字是否与所选的第n个乐透数字匹配,这不太可能是真的。 Instead, you should do something like this: 相反,您应该执行以下操作:

public int RanInput(int[] Inran)
{
    int count = 0;
    for (int i = 0; i<lotteryNumbers.length; i++)
    {
        for(int j = 0; j < lotteryNumbers.length; j++) {
            if (lotteryNumbers[j] == Inran[i])
                {
                count++;
                }
        }
    }
    return count;
}

It compares each of the inputted numbers with each of the lotto numbers. 它将每个输入的数字与每个乐透数字进行比较。 There are more efficient ways to do this than the code above, but I think it should be good enough for now. 有比上面的代码更有效的方法来执行此操作,但是我认为它现在应该足够好了。

I don't think the code you use to generate the lottery numbers account for duplicates (eg the numbers might be 1, 2, 2, 1, 3) so you should fix that. 我认为您用于生成彩票号码的代码不包含重复项(例如,数字可能是1、2、2、1、3),因此您应该解决该问题。

Finally, here's some error checking you should implement if you want to be thorough: 最后,这是一些错误检查,如果您想做得更周全,应该实施这些检查:

  • make sure the user inputs exactly 5 numbers 确保用户正确输入5个数字
  • all positive integers 所有正整数
  • are below some upper bound (say, 100) 低于某个上限(例如100)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM