Java: input.next больше не принимает ввод пользователя
Я исправил большинство новых проблем, но теперь я получаю пустую символьную литеральную и открытую символьную ошибку в строке: 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);
}
}
}
}
2 ответа
Решение
Вы можете использовать Набор для хранения уже набранных номеров, а затем перерисовывать, пока не получите номер, которого нет в наборе.
System.out.println("Thank you! The numbers selected were: ");
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);
} while (numbers.contains(randNum));
numbers.add(randNum);
lotNum = (lotNum * 10) + randNum;
System.out.print(randNum);
sum += randNum;
}
numbers.clear();
System.out.println();
}
Я не уверен, в какой области вы хотите, чтобы числа были уникальными, поэтому, возможно, вам придется потянуть создание набора во внешнем цикле - или просто вызвать number.clear(), когда вы хотите сбросить его.
Так что я, наконец, исправил это и заставил работать так, как я хотел, см. ниже:
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);
}
}
}
}