Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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,这就是我得到的。我可以看到这两个数组,但当比较时,我得到了一个错误的答案。我在编译和运行时得到了很多false作为输出,但不是36个结果(布尔值),而是180个布尔值结果 import java.io.*; import java.util.*; public class Lotto { /** * @param args the command line arguments */ public static void main(String[] args) throws FileN

这就是我得到的。我可以看到这两个数组,但当比较时,我得到了一个错误的答案。我在编译和运行时得到了很多false作为输出,但不是36个结果(布尔值),而是180个布尔值结果

import java.io.*;
import java.util.*;



public class Lotto {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws FileNotFoundException
{
    userNumbers();
    drawNumbers();  
}
public static void userNumbers()throws FileNotFoundException
{
     Scanner in = new Scanner(new FileReader("lotto.dat")); //In this file there is 6 integer

 while(in.hasNext())
 {
     if(in.hasNextInt())
     {
        //in.next();
        System.out.println(in.nextLine());
     }
     else
     System.out.print(in.next()+ " ");
 }

}
public static void drawNumbers() throws FileNotFoundException
{
     int N = 6;// 6 Random Numbers
     int[] lottoNumber = new int[N];
     int[] userNumber = new int[N];
     //This generate random numbers of size N
     for (int i = 0; i < N; i++) 
     {
        Random n = new Random(); 
        lottoNumber[i] = n.nextInt(20)+1;
        System.out.print(lottoNumber[i]+" ");           
     }

     Scanner dat = new Scanner(new FileReader("lotto.dat"));
        int i=0;
        while(dat.hasNextInt())
        {
           userNumber[i++]=dat.nextInt();

           System.out.println("");
           doCompare(lottoNumber, userNumber); 
        }


}
  public static void doCompare(int[] a, int[] b) {
   for(int i = 0; i < a.length; i++)
  {
for(int j = 0; j < b.length; j++)
{
    if(a[i] == b[j])
    {
        System.out.print("True");//true
    }
    else
    {
        System.out.print("false");``
    }
}
}}}
import java.io.*;
导入java.util.*;
公共类彩票{
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args)引发FileNotFoundException
{
用户编号();
数字();
}
public static void userNumbers()引发FileNotFoundException
{
Scanner in=new Scanner(new FileReader(“lotto.dat”);//此文件中有6个整数
while(在.hasNext()中)
{
if(在.hasNextInt()中)
{
//in.next();
System.out.println(in.nextLine());
}
其他的
System.out.print(in.next()+);
}
}
public static void drawNumbers()引发FileNotFoundException
{
int N=6;//6个随机数
int[]lottoNumber=新的int[N];
int[]userNumber=新的int[N];
//这将生成大小为N的随机数
对于(int i=0;i
您有:

while(dat.hasNextInt())
{
   userNumber[i++]=dat.nextInt();
   System.out.println("");
   doCompare(lottoNumber, userNumber); 
}
每次从文件中读取数字时,都会运行比较。听起来您想要做的是从文件中读取所有内容,然后运行比较-因此您应该这样做。你可能的意思是:

while(dat.hasNextInt())
{
   userNumber[i++]=dat.nextInt();
   System.out.println(""); // <- This probably isn't useful, either.
}
doCompare(lottoNumber, userNumber); 
while(dat.hasNextInt())
{
userNumber[i++]=dat.nextInt();
System.out.println(“”;//您有:

while(dat.hasNextInt())
{
   userNumber[i++]=dat.nextInt();
   System.out.println("");
   doCompare(lottoNumber, userNumber); 
}
每次从文件中读取数字时,您都在运行比较。听起来您想要做的是从文件中读取所有内容,然后运行比较-因此您应该这样做。您可能的意思是:

while(dat.hasNextInt())
{
   userNumber[i++]=dat.nextInt();
   System.out.println(""); // <- This probably isn't useful, either.
}
doCompare(lottoNumber, userNumber); 
while(dat.hasNextInt())
{
userNumber[i++]=dat.nextInt();

System.out.println(“”;//对于我来说,它打印216个布尔值,因为应该打印36个的函数执行了6次,对于lotto.dat中的每个整数,您只需更改:

while(dat.hasNextInt())
{
   userNumber[i++]=dat.nextInt();
   System.out.println("");
   doCompare(lottoNumber, userNumber); 
}


对我来说,它打印216个布尔值,因为应该打印36的函数执行6次,对于
lotto.dat
中的每个整数,您只需更改:

while(dat.hasNextInt())
{
   userNumber[i++]=dat.nextInt();
   System.out.println("");
   doCompare(lottoNumber, userNumber); 
}


由于每次从文件中读取下一个数字时都在运行doCompare函数,因此此例程将运行6*36次;由于只得到180个结果,因此文件中似乎没有6个数字,但很可能只有5个


将doCompare()移动到
while(dat.hasNextInt())之外的调用
-循环,您应该会没事的!

因为您每次从文件中读取下一个数字时都在运行doCompare函数,所以您将运行此例程6*36次;因为您只得到180个结果,看起来您的文件中没有6个数字,但很可能只有5个


将doCompare()-调用移到
while(dat.hasNextInt())
-循环之外,您应该会没事的!

在使用文件中的所有数字填充
userNumber
之前,您正在进行比较。您只需将语句
doCompare(lottoNumber,userNumber)
移到while循环之外:

while (dat.hasNextInt()) {
       userNumber[i++] = dat.nextInt();

  System.out.println("");
  //doCompare(lottoNumber, userNumber);
}

doCompare(lottoNumber, userNumber);

在用文件中的所有数字填充
userNumber
之前,您正在进行比较。只需将语句
doCompare(lottoNumber,userNumber)
移到while循环之外:

while (dat.hasNextInt()) {
       userNumber[i++] = dat.nextInt();

  System.out.println("");
  //doCompare(lottoNumber, userNumber);
}

doCompare(lottoNumber, userNumber);

你能把数组内容附加到问题上吗…你能把数组内容附加到问题上吗…非常感谢!错过那个小细节让我觉得很愚蠢。很高兴有人看到你的代码。再次感谢!非常感谢!错过那个小细节让我觉得很愚蠢。很高兴有人看到你的代码.再次感谢!