我刚刚开始开始计划,而我的教授让我们写了一个计票的计划。这个概念正在循环中,但我遇到了麻烦。该程序应该从用户获得字符输入,它将使用这些字符来计算投票。到目前为止,程序只能正确执行退出功能,如果我输入除指定字符以外的任何其他字符,我将获得无限循环。如果我输入' Y',' y',' N'或者' n'在开始时,即使我尝试退出,它也会继续接受输入。提前致谢!
public static void main(String[]args)
{
Scanner input = new Scanner(System.in);
int yesVotes = 0;
int noVotes = 0;
int quitVote = 0;
char vote = 'x';
System.out.println("Enter 'Y' to vote yes or Enter 'N' to vote no or Enter 'Q' to quit");
vote = input.next().charAt(0);
while (quitVote < 1)
{
if (vote == 'Y' || vote == 'y')
yesVotes++;
else if (vote == 'N' || vote == 'n')
noVotes++;
else if (vote == 'Q' || vote == 'q')
quitVote++;
else
System.out.println("Your input is invalid");
}
System.out.println("Total yes votes: " + yesVotes);
System.out.println("Total no votes: " + noVotes);
}
}
答案 0 :(得分:1)
进入while循环后,您不再接受输入值。
因此解决方案是在循环中移动vote = input.next().charAt(0);
。
你会得到预期的答案。
public static void main(String[]args)
{
Scanner input = new Scanner(System.in);
int yesVotes = 0;
int noVotes = 0;
int quitVote = 0;
char vote = 'x';
System.out.println("Enter 'Y' to vote yes or Enter 'N' to vote no or Enter 'Q' to quit");
while (quitVote < 1)
{
vote = input.next().charAt(0);
if (vote == 'Y' || vote == 'y')
yesVotes++;
else if (vote == 'N' || vote == 'n')
noVotes++;
else if (vote == 'Q' || vote == 'q')
quitVote++;
else
System.out.println("Your input is invalid");
}
System.out.println("Total yes votes: " + yesVotes);
System.out.println("Total no votes: " + noVotes);
}
}
答案 1 :(得分:1)
这是一种更好的方法。对代码进行了注释以突出显示已进行的更改。
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int yesVotes = 0;
int noVotes = 0;
//int quitVote = 0; // no need for this
char vote = 'x';
System.out
.println("Enter 'Y' to vote yes or Enter 'N' to vote no or Enter 'Q' to quit");
while (true) { // keeps on running until you quit
vote = input.next().charAt(0); // for taking user input (previously you were placing it outside the loop)
if (vote == 'Y' || vote == 'y')
yesVotes++;
else if (vote == 'N' || vote == 'n')
noVotes++;
else if (vote == 'Q' || vote == 'q')
break;
else
System.out.println("Your input is invalid...Please try again");
}
input.close(); // to close the input stream
System.out.println("Total yes votes: " + yesVotes);
System.out.println("Total no votes: " + noVotes);
}
示例输出:
Enter 'Y' to vote yes or Enter 'N' to vote no or Enter 'Q' to quit
s
Your input is invalid...Please try again
d
Your input is invalid...Please try again
y
Y
y
n
N
Q
Total yes votes: 3
Total no votes: 2
答案 2 :(得分:0)
问题是你没有在每次迭代中读取输入,所以就像这样做
while (quitVote < 1)
{
System.out.println("Enter 'Y' to vote yes or Enter 'N' to vote no or Enter 'Q' to quit");
vote = input.next().charAt(0);
if (vote == 'Y' || vote == 'y')
yesVotes++;
else if (vote == 'N' || vote == 'n')
noVotes++;
else if (vote == 'Q' || vote == 'q')
quitVote++;
else
System.out.println("Your input is invalid");
}
答案 3 :(得分:0)
在你的时间内使用新的字符读数,否则每次输入“Q”或“q”以外的字符时,它都会进入无限循环。
使用它:
public static void main(String[]args)
{
Scanner input = new Scanner(System.in);;
int yesVotes = 0;
int noVotes = 0;
int quitVote = 0;
char vote = 'x';
System.out.println("Enter 'Y' to vote yes or Enter 'N' to vote no or Enter 'Q' to quit");
// vote = input.next().charAt(0);
while (quitVote < 1){
vote = input.next().charAt(0);
if (vote == 'Y' || vote == 'y'){
yesVotes++;
}
else if (vote == 'N' || vote == 'n'){
noVotes++;
}
else if (vote == 'Q' || vote == 'q'){
quitVote++;
}
else
System.out.println("Your input is invalid");
}
System.out.println("Total yes votes: " + yesVotes);
System.out.println("Total no votes: " + noVotes);
}
}
这可能是你的解决方案。 :)
答案 4 :(得分:0)
试
while (quitVote < 1)
{
vote = input.next().charAt(0);
if (vote == 'Y' || vote == 'y')
yesVotes++;
else if (vote == 'N' || vote == 'n')
noVotes++;
else if (vote == 'Q' || vote == 'q')
quitVote++;
else
System.out.println("Your input is invalid");
}
答案 5 :(得分:0)
让我解释一下,另一个解决方案使用charAt(0)
我认为这是不对的,如果用户输入'nSKDJSALKD'它仍然计入No和'yYsd2sdf3'它仍然计为Yes
但是如果你尝试这个逻辑,如果输入“y”或“Y”,它只计算YES,如果你输入'n'或'N',则计算No,如果你输入'q'或'',也要退出投票Q',如果你输入其他字符或字符串,它将显示你的输入无效!。
作为程序员,我们需要防止这些人为错误。 :)
Scanner in = new Scanner(System.in);
int yesVote = 0;
int noVote = 0;
boolean bool = true;
while(bool){
System.out.println("Enter 'y' or 'Y' to vote Yes, 'n' or 'N' to vote No, and 'q' or 'Q' to QUIT voting.");
String input = in.next();
if(!input.equalsIgnoreCase("q") && !input.equalsIgnoreCase("n") && !input.equalsIgnoreCase("y")){
System.out.println("Invalid input!");
}
else{
if(input.equalsIgnoreCase("y")){
System.out.println("You vote Yes!");
yesVote++;
}
else if(input.equalsIgnoreCase("n")){
System.out.println("You vote No!");
noVote++;
}
else{
System.out.println("Thanks for Voting!! you may see the result below!");
bool = false;
}
}
}
System.out.println("Total of Yes: "+yesVote);
System.out.println("Total of No: "+noVote);
输出:
Enter 'y' or 'Y' to vote Yes, 'n' or 'N' to vote No, and 'q' or 'Q' to QUIT voting.
QqQq
Invalid input!
Enter 'y' or 'Y' to vote Yes, 'n' or 'N' to vote No, and 'q' or 'Q' to QUIT voting.
Y
You vote Yes!
Enter 'y' or 'Y' to vote Yes, 'n' or 'N' to vote No, and 'q' or 'Q' to QUIT voting.
N
You vote No!
Enter 'y' or 'Y' to vote Yes, 'n' or 'N' to vote No, and 'q' or 'Q' to QUIT voting.
n
You vote No!
Enter 'y' or 'Y' to vote Yes, 'n' or 'N' to vote No, and 'q' or 'Q' to QUIT voting.
y
You vote Yes!
Enter 'y' or 'Y' to vote Yes, 'n' or 'N' to vote No, and 'q' or 'Q' to QUIT voting.
q
Thanks for Voting!! you may see the result below!
Total of Yes: 2
Total of No: 2