java: input.next 不再接受用户输入

java: input.next no longer accepting user input

我修复了大部分新问题,但现在我在线上收到空字符文字和未闭合字符文字错误: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);
            }
        }
    }
}

你可以用一个Set来存储你已经得到的数字,然后重新绘制,直到你得到一个不在Set中的数字。

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);
            }
        }
    }
}