目前卡在我的乐透计划中的一个错误,例如;如果我输入1,2,3,4,5并再次输入5,我收到警告我有5个副本,但如果我再次输入,我没有收到警告,我可以输入任何我想要的号码,它会打印出来。
package lottery;
import java.util.*;
public class Lottery {
public static final int NUMBERS = 6;//controlls the min and max numbers needed for the lotto
public static final int MIN_NUMBER = 1;
public static final int MAX_NUMBER = 42;
public static void main(String[] args) {
// get winning number and ticket sets
Set<Integer> winningNumbers = createWinningNumbers();
Set<Integer> ticket = getTicket();
System.out.println();
// Display and keeps only the winning numbers from the user's ticket
Set<Integer> intersection = new TreeSet<>(ticket);
intersection.retainAll(winningNumbers);
// print out the results by fetching input from the user and randomly generated numbers.
System.out.println("Your ticket numbers are " + ticket);
System.out.println("The winning numbers are " +
winningNumbers);
System.out.println();
System.out.println("You had " + intersection.size() +
" matching numbers.");
if (intersection.size() > 0) {
double prize = 100 * Math.pow(2, intersection.size());
System.out.println("The matched numbers are " +
intersection);
System.out.println("Your prize is $" + prize);
}
}
// generates a set of the winning lotto numbers
public static Set<Integer> createWinningNumbers() {
Set<Integer> winningNumbers = new TreeSet<>();
Random r = new Random();
while (winningNumbers.size() < NUMBERS) {
int number = r.nextInt(MAX_NUMBER) + 1;
winningNumbers.add(number);
}
return winningNumbers;
}
// reads the player's lottery ticket from the console
public static Set<Integer> getTicket() {
Set<Integer> ticket = new TreeSet<>();
Scanner console = new Scanner(System.in);
Scanner input = new Scanner(System.in);
System.out.print("Type in your " + NUMBERS + //prints out asking for you to type in your numbers
" unique lotto numbers: ");
int inputSize = 0;
while (input.hasNextInt() && inputSize <NUMBERS) {
int getTicket = input.nextInt();
if (getTicket< MIN_NUMBER || getTicket > MAX_NUMBER) {//prints out if number is not within the lotto range
System.out.println("Your number must be in " + MIN_NUMBER + " - " + MAX_NUMBER + " range.");
}
else {
if (ticket.add(getTicket)) {//prints out if number successfully added
System.out.println("Number added successfully");
}
else {// prints out if number is not successfully added.
System.out.println("Duplicate number detected");
}
inputSize++;
}
}
while (ticket.size() < NUMBERS) {
int number = console.nextInt();
ticket.add(number);
}
return ticket;
}//close main() function
}//close Lottery class
答案 0 :(得分:1)
更改getTicket()
// reads the player's lottery ticket from the console
public static Set<Integer> getTicket()
{
Set<Integer> ticket = new TreeSet<>();
Scanner input = new Scanner(System.in);
System.out.println("Please enter " + NUMBERS + " unique lotto numbers!");
int validInputs = 0;
while (input.hasNextInt() && validInputs < NUMBERS)
{
System.out.printf("Enter number %d: ", validInputs);
int getTicket = input.nextInt();
if (getTicket< MIN_NUMBER || getTicket > MAX_NUMBER)
{
//prints out if number is not within the lotto range
System.out.println("Your number must be in " +
MIN_NUMBER + " - " + MAX_NUMBER + " range.");
}
else
{
if (ticket.add(getTicket))
{
//prints out if number successfully added
System.out.println("Number added successfully");
validInputs++; // Increment valid inputs here
}
else
{
// prints out if number is not successfully added.
System.out.println("Duplicate number detected");
}
}
}
}
为什么你在一个功能中使用两个扫描仪?
Scanner console = new Scanner(System.in);
Scanner input = new Scanner(System.in);
答案 1 :(得分:0)
您的代码不需要第二个while循环
while (ticket.size() < NUMBERS) {
int number = console.nextInt();
ticket.add(number);
}
因为它不包含上面实现的逻辑。
只需删除它,只有在将有效数字添加到集合时才会增加inputSize(例如,当您打印出“成功消息”时)。然后第一个循环将运行,直到用户输入6个有效(唯一)数字