我修复了大部分新问题,但现在我在线上得到一个空的字符文字和未关闭的字符文字错误:char a = input.next('');
以下是我的新代码,但我不完全确定如何修复此错误。
import java.util.*;
import java.io.BufferedReader;
public class JesseSabatinihw {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
char lottoGameSelection; // user inputs whether or not they want to play the Lottery Game
int game; // user input of game 3, 4, or 5 as integer
int times; // user input of times game is played as integer
int randNum; // random number generator creates an integer
int lotNum; //lottery number generated as an integer
int sum = 0; // initializing the sum of all integers as 0 to start with
System.out.println("Do you wish to make lottery game selections?");
char a = input.next('');
if(a == 'Y' || a =='y') {
System.out.println("Which lottery game do you want to play!");
System.out.println("Enter 3 for Pick 3");
System.out.println("Enter 4 for Pick 4");
System.out.println("Enter 5 for Pick 5");
game = input.nextInt();
System.out.print("How many games would you like to play? ");
times = input.nextInt();
System.out.println("Thank you! The numbers selected were: ");
// generates random lottery number specific to the chosen game 3, 4, or 5 numbers per games specified by user input
Set<Integer> numbers = new HashSet<>();
for(int i = 0; i < times; i++) {
lotNum = 0;
for(int j = 0; j < game; j++) {
do {
randNum = (new java.util.Random()).nextInt(10); // calls on the java.util.Random class to randomly select number for game
// prevents the generation of duplicate numbers per game
} while (numbers.contains(randNum));
numbers.add(randNum);
lotNum = (lotNum * 10) + randNum; // selects random integers 0 - 9
System.out.print(randNum); // prints randomly generated number
sum += randNum; // adds all random numbers generated
}
numbers.clear(); // refreshes the number generator preventing duplication of numbers and preventing the code to stop if all int 0-9 are used previously
System.out.println(); // prints each game on it's own line
}
System.out.println("Sum of the numbers of all games: " + sum); //prints the total of all randomly generated lottery numbers
} else {
if (a == 'N' || a == 'n'); {
System.exit(0);
}
}
}
}
答案 0 :(得分:0)
您可以使用Set来存储您已经拥有的数字,然后重新绘制,直到您得到一个不在Set中的数字。
web-interface
我不确定你希望数字在什么范围内是唯一的,所以也许你必须在外部循环内拉动集合的创建 - 或者只是在你想要重置它时调用number.clear()。
答案 1 :(得分:0)
所以我终于修复了它并让它按照我想要的方式工作 见下文:
import java.util.*;
import java.io.*;
public class JesseSabatinihw2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
char lottoGameSelection; // user inputs whether or not they want to play the Lottery Game
int game; // user input of game 3, 4, or 5 as integer
int times; // user input of times game is played as integer
int randNum; // random number generator creates an integer
int lotNum; //lottery number generated as an integer
int sum = 0; // initializing the sum of all integers as 0 to start with
System.out.println("Do you wish to make lottery game selections? Press Y or y for yes and N or n for no.");
char a = input.next().trim().charAt(0);
if(a == 'Y' || a == 'y') {
System.out.println("Which lottery game do you want to play!");
System.out.println("Enter 3 for Pick 3");
System.out.println("Enter 4 for Pick 4");
System.out.println("Enter 5 for Pick 5");
game = input.nextInt();
System.out.print("How many games would you like to play? ");
times = input.nextInt();
System.out.println("Thank you! The numbers selected were: ");
// generates random lottery number specific to the chosen game 3, 4, or 5 numbers per games specified by user input
Set<Integer> numbers = new HashSet<>();
for(int i = 0; i < times; i++) {
lotNum = 0;
for(int j = 0; j < game; j++) {
do {
randNum = (new java.util.Random()).nextInt(10); // calls on the java.util.Random class to randomly select number for game
// prevents the generation of duplicate numbers per game
} while (numbers.contains(randNum));
numbers.add(randNum);
lotNum = (lotNum * 10) + randNum; // selects random integers 0 - 9
System.out.print(randNum); // prints randomly generated number
sum += randNum; // adds all random numbers generated
}
numbers.clear(); // refreshes the number generator preventing duplication of numbers and preventing the code to stop if all int 0-9 are used previously
System.out.println(); // prints each game on it's own line
}
System.out.println("Sum of the numbers of all games: " + sum); //prints the total of all randomly generated lottery numbers
} else {
if (a == 'N' || a == 'n'); {
System.exit(0);
}
}
}
}