java: input.next 不再接受用户输入
Posted
技术标签:
【中文标题】java: input.next 不再接受用户输入【英文标题】:java: input.next no longer accepting user input 【发布时间】:2016-02-03 05:09:54 【问题描述】:我修复了大部分新问题,但现在我在线收到一个空字符文字和未闭合字符文字错误: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);
【问题讨论】:
不要将文本作为图像发布。坚持以文本格式发布输出/代码/数据。 【参考方案1】:您可以使用 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()。
【讨论】:
我会继续测试一下,但感谢您的回复。我不知道您是否能够看到我的图像,这是一个示例输出,但基本上,每一行都会生成选择的游戏,因此例如,如果您选择游戏 3 并玩两次,输出将如下所示:123 234我要防止的是每行重复。例如:223 445 等....希望您提供的解决方案可以防止这种情况发生,我非常感谢您的回复! 所以在编辑我的代码并添加您建议的内容后,我没有重复,但是由于没有重复,它会在使用所有 int 0-9 后停止代码并且不打印总和,例如:游戏 3 5 游戏:023 146 578 9 这将是输出,它会停在那里,所以一方面我没有得到重复,这很棒,但另一方面,如果输入请求输出,它不再做我需要的事情生成超过 0-9 ..... 如果有意义的话 好吧,您将随机数限制为 0-9,并且您不希望重复,因此除非您扩大 Random 对象为您提供的数字范围,否则您无法生成更多。跨度> 哦,等等,我想我明白你想说什么了。我认为你必须在每场比赛后清空 Set,所以在内部 for 循环之后调用 numbers.clear() 。我更新了我的代码。 太棒了,我添加了 numbers.clear();现在它可以完美运行了!非常感谢!【参考方案2】:所以我终于修好了它并让它完全按照我想要的方式工作 见下文:
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);
【讨论】:
以上是关于java: input.next 不再接受用户输入的主要内容,如果未能解决你的问题,请参考以下文章