如何比较文件中的现有数字和随机数?

时间:2014-10-28 09:31:50

标签: java random while-loop compare

我正在进行一项练习,我必须制作一个乐透游戏。

我已生成6个随机数(rand1rand2rand3rand4rand5rand6),现在我必须将它们与我现有的数字进行比较,这些数字保存在文件f中。我想一次比较每个随机数和我保存在文件中的数字。

这是我到目前为止所得到的:

public class lottoEx{

   public static void main(String[] args)throws Exception{

      userNumbers(4, 5, 7, 9, 11, 19);

      drawNumbers();


   }

   public static void userNumbers(int num1, int num2, int num3, int num4, int num5, int num6)throws Exception{ //USER NUMBERS

      File f = new File("lotto.dat");

      PrintStream output = new PrintStream(f);

      output.println(num1 + " " + num2 + " " +  num3 + " " + num4 + " " + num5 + " " + num6);

      Scanner read = new Scanner(f);

      System.out.print("Your numbers: ");

      while(read.hasNext()){

         System.out.print(read.nextInt() + " ");

      }

   }

   public static void drawNumbers()throws Exception{ //RANDOM NUMBERS

      Random rand = new Random();

      int rand1 = rand.nextInt(19)+1;
      int rand2 = rand.nextInt(19)+1;
      int rand3 = rand.nextInt(19)+1;
      int rand4 = rand.nextInt(19)+1;
      int rand5 = rand.nextInt(19)+1;
      int rand6 = rand.nextInt(19)+1;

      Thread.sleep(2000);

      System.out.println();
      System.out.println();

      System.out.print("The lotto numbers are: ");

      Thread.sleep(2000);

      System.out.print(rand1 + " " + rand2 + " " + rand3 + " " + rand4 + " " + rand5 + " " + rand6);

         doCompare(rand1, rand2, rand3, rand4, rand5, rand6);

         winCheck();

   }

   public static void doCompare(int rand1, int rand2, int rand3, int rand4, int rand5, int rand6)throws Exception{

      Scanner scan = new Scanner("lotto.dat");

      Set<Integer> set = new HashSet<>();

      while(scan.hasNextInt()){
         set.add(scan.nextInt());    
      }
      System.out.println(set);

      if(set.contains(rand1)){

         System.out.println("Match on rand1");

      }

   }

   public static void winCheck()throws Exception{

      //code goes here

   }

}

看起来好像有些错误,因为它只是将随机数与我文件中的第一个数字进行比较?我现在有点卡住了。希望有人能帮助我! : - )

2 个答案:

答案 0 :(得分:1)

一种简单而有效的方法是将文件中的所有数字存储在Set中,然后查找每个随机数的集合。这样你只需要读一次文件。

Set<Integer> set = new HashSet<>();

while(scan.hasNextInt()) {
    set.add(scan.nextInt());
}

if (set.contains(rand1)) {
    System.out.println("MATCH on rand1: " + rand1);
}

// repeat for rand2, rand3..etc

答案 1 :(得分:0)

尝试使用java.util.Set存储生成的数字并使用Scanner构造函数,该构造函数将File作为参数。

public static void main(String[] args) throws FileNotFoundException {
    final Set<Integer> lottoResults = new HashSet<>();
    final Random rnd = new Random(System.currentTimeMillis());

    // generate numbers
    while (lottoResults.size() < 6) {
        lottoResults.add(rnd.nextInt(49) + 1);
    }

    final Scanner in = new Scanner(new File("path\\to\\yourLottoFile.txt"));
    for (String s : in.nextLine().split(",")) {
        final int lottoGuess = Integer.parseInt(s);
        // maybe some error handling here (invalid / wrong input)?

        System.out.printf("The guess: %2d is %s\n", lottoGuess, (lottoResults.contains(lottoGuess) ? "correct!!!" : "incorrect"));
    }
}

示例输出如下所示:

The guess:  4 is incorrect
The guess: 10 is incorrect
The guess:  7 is incorrect
The guess:  5 is incorrect
The guess: 11 is correct!!!
The guess: 19 is incorrect