简体   繁体   中英

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. If I enter 'Y', 'y', 'N', or 'n' in the beginning it'll continue to take inputs even if I try to quit. 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.

So solution is move vote = input.next().charAt(0); inside while loop.

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 .

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!.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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