大家好,所以情况就是这样。我的乐透应用程序有效,但我只需要最后一件事情来实现,我有一个huuuuge问题。所以我有6个数字保存在“lotto.dat”文件中。 1,2,3,4,5,6。如果我不选择获取新数字,应用程序会生成6个新的随机数并进行比较。这工作。如何比较文件中的数字与随机数字?
但是,如果我不想要6个新的数字,将它们保存在一个ArrayList中,并将它们打印到“lotto.dat”中,那么该文件现在包含6个数字,并带有数组listList事物的括号cus。我有一种感觉,这可能是这个问题,因为当新号码被保存时,它说甚至没有匹配。
这是我的号码的方法:
Scanner scann = new Scanner(System.in);
File f = new File("lotto.dat");
PrintStream output = new PrintStream(f);
output.print(num1 + " " + num2 + " " + num3 + " " + num4 + " " + num5 + " " + num6);
System.out.println("Your lotto numbers: " + num1 + " " + num2 + " " + num3 + " " + num4 + " " + num5 + " " + num6);
System.out.println("Would you like to keep these numbers? y/n");
String yn = scann.nextLine();
if(!yn.equals("y")){
newNumbers();
}//While yn !y
在我newNumbers方法我填与被写在控制台中的6个newNumbs一个ArrayList。然后,我将ArrayList的StreamStream打印到“lotto.dat”文件中,该文件被覆盖。
现在,这是我的代码,我比较随机数(从ArrayList的号码):
for(int n : numbers) { // go through all the numbers in the list
String match = doCompare(n); // doCompare metode
if(match.equals("true")){
List<Integer> numbersMatch = new ArrayList<>();
numbersMatch.add(n);
Thread.sleep(1000);
System.out.println();
System.out.println("Match on the number: " + n);
count++;
}
}
这里是我的doCompare方法:
Scanner sc = new Scanner(new File("lotto.dat"));
List<Integer> mn = new ArrayList<>();
while(sc.hasNextInt()){
mn.add(sc.nextInt());
}
if(mn.contains(n)){
String tf = "true";
return tf;
}
else{
String tf = "false";
return tf;
}
我已经花了很多字面上几个小时试图解决问题,但我不能。为什么它没有比较数字?唯一真正改变的是,保存在lotto.dat中的新号码在外部文件中有“[]”。
对不起,我的咖啡不能正常工作,所以我需要一点澄清。你确切的问题是:“如果数字是新生成的,比较来自文件的数字不起作用”_我是对吗? – Keale 2014-10-31 03:31:00
你的'doCompare()'方法应该返回一个'boolean',而不是'String',它必须与'true'或'false'进行比较。你的问题仍然模糊。 – EJP 2014-10-31 05:03:11