如何从if语句返回while循环?

时间:2014-11-17 15:24:36

标签: java while-loop user-input

这是我一直在做的事情。我尝试使用布尔值循环这个while方法。 (我的老师不称职,所以我一直在学习教科书。)

else {  System.out.println("Do you want to restart? Y/N"); 
            string answer = scn.next();
            return;
            if (scn.hasNext() && !no)) {
                System.out.println("end");

            } else{   
                continue;
                }

                /*if (repeat) {
                continue;
                } else {
                System.out.println("End");
                break;
                }*/

            }

这是嵌套在像这样的while循环....

import java.util.Scanner; import java.lang.String;

public class booleanvariables {
public static void main (String[] args){
    Scanner scn = new Scanner(System.in);

    int score1, score2;

    String answer, e;

    boolean bothHigh, atLeastOneHigh, atLeastOneModerate, noLow, tooLow, repeat;

    while (true) {
        System.out.print("Enter the first test score:\t");
        score1 = scn.nextInt();
        System.out.print("Enter the second test score:\t");
        score2 = scn.nextInt();
        answer = null;
        e = "n";
        bothHigh            = (score1 >= 90 && score2 >= 90);
        atLeastOneHigh      = (score1 >= 90 || score2 >= 90);
        atLeastOneModerate  = (score1 >= 70 || score2 >= 70);
        noLow               = !(score1 < 50 || score2 <  50);
        tooLow              = (score1 <= 50 || score2 <= 50);
        repeat              = (answer == "yes" || answer == "y"); //|| answer == Y || answer ==     Yes);
        if (tooLow)
            System.out.println("Inputs are too low");

        if (bothHigh)
            System.out.println("Qualified to be a manager");

        if (atLeastOneHigh)
            System.out.println("Qualified to be a supervisor");

        if (atLeastOneModerate && noLow)
            System.out.println("Qualified to be a clerk");
            /** NESTED WRONG I'M AWARE
             */
        else {  System.out.println("Do you want to restart? Y/N"); 
            string answer = scn.next();
            return;
            if (scn.hasNext() && !no)) {
                System.out.println("end");

            } else{   
                continue;
                }

                /*if (repeat) {
                continue;
                } else {
                System.out.println("End");
                break;
                }*/

            }
        }
    }
}

3 个答案:

答案 0 :(得分:1)

这比你想象的要简单得多。

就这样做:

boolean stop = false;
while(!stop) {
    //do whatever you want here

    System.out.println("Do you want to quit?(yes or no");
    String input = scan.nextLine();
    if(input.equals("no")) {
        stop = true;
    }
}

这样,如果您输入&#34; no&#34;,它会将布尔值设置为true,然后将为while循环!stop创建条件,等于false

答案 1 :(得分:0)

answer == "yes"

您正在检查两个对象是否相同。您应该使用等于方法answer.equals("yes") || answer.equals("y")

答案 2 :(得分:0)

经过测试和工作 我重写了一些分支。 (我使用BlueJ作为编译器,它认为这是一个没有input = scn.nextLine();

的错误
do {
//same booleans i've been using
if (!stop) {
            System.out.print("Do you want to quit? (yes or no):\t");
            //String input;
            input = scn.nextLine();
        }
        //String input;
        input = scn.next();


        if(input.equals("yes")) {
            stop = true;
            System.out.println("Goodbye");
            return;

        }
    } while (!stop);

我真的不知道为什么蓝色J在if语句中初始化输入时不喜欢它