gpt4 book ai didi

java - 从文本文件读取乐透

转载 作者:行者123 更新时间:2023-12-01 09:38:36 25 4
gpt4 key购买 nike

我的控制台应该从文本文件中读取一组数字。文件中的数字必须与 7 个“幸运数字”进行比较。程序必须找出有多少人获得了 3 场比赛、4 场比赛、5 场比赛、6 场比赛和 7 场比赛的“幸运数字”,并且必须在最后打印结果。

我不知道如何比较文件中的数字并生成“幸运数字”。

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

public class Lotto {
public static void main (String[] args) throws FileNotFoundException {

Scanner lottoFile = new Scanner(new File("c:\\Exercises\\New Folder\\Lotto.txt"));
ArrayList<Double> lotto = new ArrayList<Double>();

while(lottoFile.hasNextLine()){
String line = lottoFile.nextLine();

Scanner scanner = new Scanner(line);
scanner.useDelimiter(",");
while(scanner.hasNextDouble()){
lotto.add(scanner.nextDouble());
}
scanner.close();
}

lottoFile.close();

System.out.println(lotto);
}
}

最佳答案

使用 HashSet .

第1步.将幸运数字放入HashSet中,这样可以轻松检查某个数字是否是幸运数字。

第 2 步。对于输入中的每个人,使用 HashSet.contains 计算他们有多少个号码位于幸运号码集中。 .

关于java - 从文本文件读取乐透,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38624647/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com