Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法将我的数字数组传递到另一个类_Java_Arrays - Fatal编程技术网

Java 无法将我的数字数组传递到另一个类

Java 无法将我的数字数组传递到另一个类,java,arrays,Java,Arrays,我试图比较不同类中的两个不同数组,一个是用户输入的数字,另一个是随机的。但是我不能让用户填写数字的那个进入我的构造函数类来比较它们。 代码如下: import java.util.Scanner; class LotteryTester{ public static void main(String[] args) { Scanner input = new Scanner(System.in); int[] Picks = new int [

我试图比较不同类中的两个不同数组,一个是用户输入的数字,另一个是随机的。但是我不能让用户填写数字的那个进入我的构造函数类来比较它们。 代码如下:

import java.util.Scanner;

class LotteryTester{

    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        int[] Picks = new int [5];
        System.out.println("Introduce your numbers here: ");

        for(int i = 0; i < Picks.length; i++)
        {
            Picks[i] = input.nextInt();
        }

        for (int i = 0; i < Picks.length; i++)
        {
            System.out.println(Picks); 
        }
    }
}
import java.util.Scanner;
类彩票测试器{
公共静态void main(字符串[]args)
{
扫描仪输入=新扫描仪(System.in);
int[]选取=新的int[5];
System.out.println(“在这里介绍您的号码:”);
for(int i=0;i
下面是找不到第一个数组的类:

import java.util.Random;
    public class Lottery
    {
        Random random = new Random();
        int number;
        int count;
    public Lottery( )
    {
        int lotteryNums[] = new int[5];
        for (int i=0; i < lotteryNums.length; i++)
        {
            number = random.nextInt(10);
            lotteryNums[i] = number;
        }
    }

    public static int CompareNums(Picks[] numbers)
    {
        for (int i=0; i <Picks.length; i++)
        {
            if (Picks[i] == lotteryNums[i])
            {
                System.out.println("The number on " + (i+1)+ " matches");
                count++;
            }
            else 
            {
                   System.out.println("the number on " + (i+1)+ " matches");
                }
           }
           return count;
      }
}
import java.util.Random;
公共类彩票
{
随机=新随机();
整数;
整数计数;
公共彩票()
{
int lotteryNums[]=新int[5];
for(int i=0;i对于(int i=0;i您永远不会将lotteryNums赋值给构造函数之外的变量。每当方法结束时,在其中声明的所有变量都不再可访问

您的类应该如下所示:

public class Lottery
{
   Random random = new Random();
   int number;
   int count;
   int lotteryNums[];
    public Lottery()
    {
        lotteryNums[] = new int[5]; //Set the variable in the class so we can use it later
        for (int i=0; i < lotteryNums.length; i++)
        {
            number = random.nextInt(10);
            lotteryNums[i] = number;
        }
    }

    public int CompareNums(Picks[] numbers) //Removed static since numbers are made in a constructor. All non-static class variables wouldn't be accessible otherwise.
    {
        for (int i=0; i <Picks.length; i++)
        {
            if (Picks[i] == lotteryNums[i])
            {
                System.out.println("The number on " + (i+1)+ " matches");
                count++;
            }
            else 
            {
               System.out.println("the number on " + (i+1)+ " matches");
            }
       }
        return count;
    }
}
然后,向用户询问他们的号码。 最后,对照中奖号码检查号码,看看用户是否中奖

if (lottery.CompareNums(lotteryNums) == 5) System.out.println("You Won!");

实际上,不需要额外的类就可以做到这一点,ArrayList会更好

试试这个,我留下评论来告诉你我做了什么

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;


public class CompareArrayLists {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       ArrayList<Integer> userNums = new ArrayList();
       ArrayList<Integer> randNums = new ArrayList();
       Random random = new Random();

       Scanner cin = new Scanner(System.in);
       int num = 0;
       for (int i = 0; i < 5; i++) //let a user enter 5 numbers and generate 5 at the same time
       {
           System.out.print("Please enter your " + (i+1) + " number >");
           num = cin.nextInt();
           userNums.add(num);
           num = random.nextInt(10);
           randNums.add(num);
       }
       num = 0;
       for (int j : userNums)//test each item in userNums to see if matches in randNums
       {
           if (!(randNums.contains(j)))
               num++;//increment nums if something doesn't match
       }
       if (num > 0)//not a match if nums increments, you could also print nums to see how many are correct
          System.out.println("Not a match");
       else
          System.out.println("a match");



    }
import java.util.ArrayList;
导入java.util.Random;
导入java.util.Scanner;
公共类比较列表{
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args){
ArrayList userNums=新的ArrayList();
ArrayList randNums=新的ArrayList();
随机=新随机();
扫描仪cin=新扫描仪(System.in);
int num=0;
for(int i=0;i<5;i++)//让用户输入5个数字,同时生成5个数字
{
系统输出打印(“请输入您的“+(i+1)+”编号>”;
num=cin.nextInt();
userNums.add(num);
num=random.nextInt(10);
randNums.add(num);
}
num=0;
for(int j:userNums)//测试userNums中的每个项,看看是否与randNums中的匹配
{
如果(!(randNums.contains(j)))
num++;//如果不匹配,则增加nums
}
if(num>0)//如果nums递增,则不匹配,还可以打印nums以查看有多少是正确的
System.out.println(“不匹配”);
其他的
System.out.println(“匹配”);
}

你在哪里打电话
?我实际上不知道我应该调用该对象,我假设使用的是它引用它的相同对象名。你必须调用该方法才能运行它。你的主方法当前没有调用它。Axel,你应该开始遵循。这将有助于使你的代码更具可读性。这将使你的代码更易于调试,也更容易使用r阅读!谢谢,我可以在同一个类中完成,几乎没有任何问题,但我正在尝试学习如何在不同的类中完成,以便了解方法调用的要点,但我似乎不知道如何调用方法
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;


public class CompareArrayLists {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       ArrayList<Integer> userNums = new ArrayList();
       ArrayList<Integer> randNums = new ArrayList();
       Random random = new Random();

       Scanner cin = new Scanner(System.in);
       int num = 0;
       for (int i = 0; i < 5; i++) //let a user enter 5 numbers and generate 5 at the same time
       {
           System.out.print("Please enter your " + (i+1) + " number >");
           num = cin.nextInt();
           userNums.add(num);
           num = random.nextInt(10);
           randNums.add(num);
       }
       num = 0;
       for (int j : userNums)//test each item in userNums to see if matches in randNums
       {
           if (!(randNums.contains(j)))
               num++;//increment nums if something doesn't match
       }
       if (num > 0)//not a match if nums increments, you could also print nums to see how many are correct
          System.out.println("Not a match");
       else
          System.out.println("a match");



    }