简体   繁体   English

循环if语句麻烦

[英]Looping if statement trouble

I've just started beginning program and my professor is having us write a program that counts votes. 我刚刚开始开始编程,我的教授正在让我们编写一个计算选票的程序。 The concept is looping but I'm having trouble with it. 这个概念正在循环,但是我遇到了麻烦。 The program is supposed to get an input of characters from the user and it will use those characters to count the vote. 该程序应该从用户那里输入字符,它将使用这些字符来计数投票。 So far the program can only execute the quit function properly and if I enter any other character other than the specified ones I'll get an endless loop. 到目前为止,该程序只能正确执行quit函数,如果我输入了除指定字符以外的其他任何字符,我将得到一个无限循环。 If I enter 'Y', 'y', 'N', or 'n' in the beginning it'll continue to take inputs even if I try to quit. 如果我在开头输入“ Y”,“ y”,“ N”或“ n”,即使我尝试退出也将继续接受输入。 Thanks in advance! 提前致谢!

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

} }

Once you go in while loop you are not accepting input value again. 一旦进入while循环,您将不再接受输入值。

So solution is move vote = input.next().charAt(0); 因此解决方案是移动vote = input.next().charAt(0); inside while loop. 在while循环内。

You will get expected answer. 您将得到预期的答案。

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

Here is a better way of doing it. 这是一种更好的方法。 The code is commented to highlight the changes that have been made. 该代码被注释以突出显示已进行的更改。

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

Sample Output: 样本输出:

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

Problem is you are not reading input in every iteration so do it like this 问题是您不是在每次迭代中都读取输入,所以要这样做

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

use new character reading within your while , else every time you enter a character other than 'Q' or 'q' it falls into infinite loop . 在一段时间内使用新的字符读取,否则每次输入“ Q”或“ q”以外的字符时,都会陷入无限循环。

use this : 用这个 :

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

this might be your solution . 这可能是您的解决方案。 :) :)

try 尝试

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

let me explain this, the other solution use charAt(0) i think it is not right, what if the users enter 'nSKDJSALKD' it still count for No and 'yYsd2sdf3' it still count for Yes but if you try this logic, it will only count the YES if you input 'y' or 'Y' and count the No if you input 'n' or 'N' and also quit the voting if you input 'q' or 'Q', and if you input other character or String it will display that your input is invalid!. 让我解释一下,另一种解决方案使用charAt(0)我认为这是不对的,如果用户输入“ nSKDJSALKD”,它仍然算作“否”和“ yYsd2sdf3”,它仍然算作“是”,但是如果您尝试这种逻辑,那如果您输入“ y”或“ Y”,则只会计数为“是”;如果您输入“ n”或“ N”,则只会计数为“否”;如果您输入“ q”或“ Q”,并且输入其他值,也会退出投票字符或字符串,它将显示您的输入无效!。

As a programmer we need to prevent those human errors. 作为程序员,我们需要防止那些人为错误。 :) :)

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

output: 输出:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM