繁体   English   中英

循环if语句麻烦

[英]Looping if statement trouble

我刚刚开始开始编程,我的教授正在让我们编写一个计算选票的程序。 这个概念正在循环,但是我遇到了麻烦。 该程序应该从用户那里输入字符,它将使用这些字符来计数投票。 到目前为止,该程序只能正确执行quit函数,如果我输入了除指定字符以外的其他任何字符,我将得到一个无限循环。 如果我在开头输入“ 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);
    }

}

一旦进入while循环,您将不再接受输入值。

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

您将得到预期的答案。

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

这是一种更好的方法。 该代码被注释以突出显示已进行的更改。

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

问题是您不是在每次迭代中都读取输入,所以要这样做

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

在一段时间内使用新的字符读取,否则每次输入“ 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);
    }
}

这可能是您的解决方案。 :)

尝试

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

让我解释一下,另一种解决方案使用charAt(0)我认为这是不对的,如果用户输入“ nSKDJSALKD”,它仍然算作“否”和“ yYsd2sdf3”,它仍然算作“是”,但是如果您尝试这种逻辑,那如果您输入“ y”或“ Y”,则只会计数为“是”;如果您输入“ n”或“ N”,则只会计数为“否”;如果您输入“ 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

暂无
暂无

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

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