为什么我的if / else不退出循环?

时间:2022-06-29 21:14:16

The problem starts at if statement ("Did you apply for Housing Allowance allowance for this month? yes/no"); After getting the answer from user the app still loops again.

问题始于if声明(“你本月申请住房津贴津贴吗?是/否”);从用户那里得到答案之后,应用程序仍会再次循环。

Why doesn't my if/else exit the loop? All my logic is okay except the if statement won't break at all I have tried to use break statement to jump the if/else and stop the code from infinite run but it also didn't work.

为什么我的if / else不退出循环?我的所有逻辑都没关系,除了if语句根本不会破坏我试图使用break语句来跳转if / else并从无限运行中停止代码但它也无效。

What am I missing?

我错过了什么?

   public static void main(String[] args) {

        double basic_salary, tax, KWSP, housing_allowance, holder;
        Scanner scan = new Scanner(System.in);
        String answer = null;

        System.out.println("Enter Basic Salary Of Employee: ");
        basic_salary = scan.nextDouble();

        do {

            if (basic_salary <= 6000 && basic_salary >= 1) {

                tax = 0.10 * basic_salary;
                System.out.println("The compulsory tax =  " + tax + " $");
                housing_allowance = 0.5 * basic_salary;
            } else if (basic_salary >= 6001 && basic_salary <= 10000) {

                tax = 0.20 * basic_salary;
                System.out.println("The compulsory tax =  " + tax + " $");
                housing_allowance = 0.5 * basic_salary;;
            } else if (basic_salary >= 10001 && basic_salary <= 14000) {

                tax = 0.25 * basic_salary;
                System.out.println("The compulsory tax =  " + tax + " $");
                housing_allowance = 0.3 * basic_salary;;
            } else {

                tax = 0.30 * basic_salary;
                System.out.println("The compulsory tax =  " + tax + " $");
                housing_allowance = 0.3 * basic_salary;;
            }


            System.out.println("Type the amount For KWSP contribution 11% or 13%  ONLY");
            KWSP = scan.nextDouble();
            holder = KWSP / 100;
            KWSP = holder * basic_salary;
            System.out.println("KWSP contribution= " + KWSP + " $");



            Scanner input = new Scanner(System.in);
            System.out.println("Did you apply for Housing Allowance allowance for this month? yes/no");
            answer = input.nextLine();

            if (answer.equalsIgnoreCase("yes")) {
                System.out.println("");
                System.out.println("");
                System.out.println("Housing Allowance: " + housing_allowance + " $");
                System.out.println("KWSP:contribution: " + KWSP + " $");
                System.out.println("Tax:contribution : " + housing_allowance + " $");
                System.out.println("Gross Salary Of Employee: " + (KWSP + housing_allowance + tax) + " $");
                System.out.println("");
                System.out.println("");


            } else if (answer.equalsIgnoreCase("no")) {
                System.out.println("");
                System.out.println("");
                System.out.println("KWSP:contribution: " + KWSP + " $");
                System.out.println("Tax:contribution : " + housing_allowance + " $");
                System.out.println("Gross Salary Of Employee: " + (KWSP + tax) + " /n");
                System.out.println("");
                System.out.println("");



            } else {
                break;
            }

        } while (basic_salary > 0); //while loop

        System.out.println("salary cannot be Zero or Negative");
    }
}

3 个解决方案

#1


1  

You're taking input the basic_salary and then running a do-while loop till basic_salary > 0 and not modifying basic_salary anywhere

您正在输入basic_salary,然后运行do-while循环,直到basic_salary> 0并且不在任何地方修改basic_salary

This condition will not turn false, until user inputs 0 or some negative value itself.

在用户输入0或某些负值本身之前,此条件不会变为false。

I am not sure what you want to achieve, what's your logic is. If you want to simply print the messages after making calculations then maybe you can use simple if-else condition instead of do-while loop.

我不确定你想要实现什么,你的逻辑是什么。如果您只想在计算后打印消息,那么您可以使用简单的if-else条件而不是do-while循环。

Maybe something like this :

也许是这样的:

if (basic_salary > 0) {
    //do your calculation and printing here
} else {
    //print basic_salary less than 0 error message
    System.out.println("salary cannot be Zero or Negative");
}

#2


0  

Looking at your while statement it seems that you want to break the loop when the salary is != 0.

看看你的while语句,似乎你想在工资为!= 0时打破循环。

So you could change your condition for something like this:

所以你可以改变这样的条件:

while !(basic_salary > 0); //while loop

And maybe write your System.out.println at the beginning of your loop.

也许可以在循环开始时编写System.out.println。

System.out.println("salary cannot be Zero or Negative");

#3


0  

I edited your code so it can work now. But don't just blindly use it, try to read it and understand why does it work. Check it and ask further questions, if any.

我编辑了你的代码,现在它可以工作了。但不要盲目地使用它,尝试阅读它并理解它为什么会起作用。检查并提出进一步的问题,如果有的话。

public static void main(String[] args) {

    double basic_salary, tax, KWSP, housing_allowance, holder;
    Scanner scan = new Scanner(System.in);
    String answer;

    do {
        System.out.println("Enter Basic Salary Of Employee: ");
        basic_salary = scan.nextDouble();
        if (basic_salary <= 0) {
            housing_allowance = 0;
            tax = 0;
            System.out.println("salary cannot be Zero or Negative");

        } else if (basic_salary <= 6000 && basic_salary >= 1) {

            tax = 0.10 * basic_salary;
            System.out.println("The compulsory tax =  " + tax + " $");
            housing_allowance = 0.5 * basic_salary;
        } else if (basic_salary >= 6001 && basic_salary <= 10000) {

            tax = 0.20 * basic_salary;
            System.out.println("The compulsory tax =  " + tax + " $");
            housing_allowance = 0.5 * basic_salary;
        } else if (basic_salary >= 10001 && basic_salary <= 14000) {

            tax = 0.25 * basic_salary;
            System.out.println("The compulsory tax =  " + tax + " $");
            housing_allowance = 0.3 * basic_salary;
        } else {

            tax = 0.30 * basic_salary;
            System.out.println("The compulsory tax =  " + tax + " $");
            housing_allowance = 0.3 * basic_salary;
        }
    }
    while (basic_salary <= 0);

    do {
        System.out.println("Type the amount For KWSP contribution 11% or 13%  ONLY");
        KWSP = scan.nextDouble();
    }
    while (!(KWSP == 11 || KWSP == 13));

    holder = KWSP / 100;
    KWSP = holder * basic_salary;
    System.out.println("KWSP contribution= " + KWSP + " $");

    do {
        System.out.println("Did you apply for Housing Allowance allowance for this month? yes/no");
        answer = scan.next();
    }
    while (!(answer.equals("yes") || answer.equals("no"))) ;

    if (answer.equalsIgnoreCase("yes")) {
        System.out.println("");
        System.out.println("");
        System.out.println("Housing Allowance: " + housing_allowance + " $");
        System.out.println("KWSP:contribution: " + KWSP + " $");
        System.out.println("Tax:contribution : " + housing_allowance + " $");
        System.out.println("Gross Salary Of Employee: " + (KWSP + housing_allowance + tax) + " $");
        System.out.println("");
        System.out.println("");
    } else if (answer.equalsIgnoreCase("no")) {
        System.out.println("");
        System.out.println("");
        System.out.println("KWSP:contribution: " + KWSP + " $");
        System.out.println("Tax:contribution : " + housing_allowance + " $");
        System.out.println("Gross Salary Of Employee: " + (KWSP + tax) + " /n");
        System.out.println("");
        System.out.println("");
    }
}

#1


1  

You're taking input the basic_salary and then running a do-while loop till basic_salary > 0 and not modifying basic_salary anywhere

您正在输入basic_salary,然后运行do-while循环,直到basic_salary> 0并且不在任何地方修改basic_salary

This condition will not turn false, until user inputs 0 or some negative value itself.

在用户输入0或某些负值本身之前,此条件不会变为false。

I am not sure what you want to achieve, what's your logic is. If you want to simply print the messages after making calculations then maybe you can use simple if-else condition instead of do-while loop.

我不确定你想要实现什么,你的逻辑是什么。如果您只想在计算后打印消息,那么您可以使用简单的if-else条件而不是do-while循环。

Maybe something like this :

也许是这样的:

if (basic_salary > 0) {
    //do your calculation and printing here
} else {
    //print basic_salary less than 0 error message
    System.out.println("salary cannot be Zero or Negative");
}

#2


0  

Looking at your while statement it seems that you want to break the loop when the salary is != 0.

看看你的while语句,似乎你想在工资为!= 0时打破循环。

So you could change your condition for something like this:

所以你可以改变这样的条件:

while !(basic_salary > 0); //while loop

And maybe write your System.out.println at the beginning of your loop.

也许可以在循环开始时编写System.out.println。

System.out.println("salary cannot be Zero or Negative");

#3


0  

I edited your code so it can work now. But don't just blindly use it, try to read it and understand why does it work. Check it and ask further questions, if any.

我编辑了你的代码,现在它可以工作了。但不要盲目地使用它,尝试阅读它并理解它为什么会起作用。检查并提出进一步的问题,如果有的话。

public static void main(String[] args) {

    double basic_salary, tax, KWSP, housing_allowance, holder;
    Scanner scan = new Scanner(System.in);
    String answer;

    do {
        System.out.println("Enter Basic Salary Of Employee: ");
        basic_salary = scan.nextDouble();
        if (basic_salary <= 0) {
            housing_allowance = 0;
            tax = 0;
            System.out.println("salary cannot be Zero or Negative");

        } else if (basic_salary <= 6000 && basic_salary >= 1) {

            tax = 0.10 * basic_salary;
            System.out.println("The compulsory tax =  " + tax + " $");
            housing_allowance = 0.5 * basic_salary;
        } else if (basic_salary >= 6001 && basic_salary <= 10000) {

            tax = 0.20 * basic_salary;
            System.out.println("The compulsory tax =  " + tax + " $");
            housing_allowance = 0.5 * basic_salary;
        } else if (basic_salary >= 10001 && basic_salary <= 14000) {

            tax = 0.25 * basic_salary;
            System.out.println("The compulsory tax =  " + tax + " $");
            housing_allowance = 0.3 * basic_salary;
        } else {

            tax = 0.30 * basic_salary;
            System.out.println("The compulsory tax =  " + tax + " $");
            housing_allowance = 0.3 * basic_salary;
        }
    }
    while (basic_salary <= 0);

    do {
        System.out.println("Type the amount For KWSP contribution 11% or 13%  ONLY");
        KWSP = scan.nextDouble();
    }
    while (!(KWSP == 11 || KWSP == 13));

    holder = KWSP / 100;
    KWSP = holder * basic_salary;
    System.out.println("KWSP contribution= " + KWSP + " $");

    do {
        System.out.println("Did you apply for Housing Allowance allowance for this month? yes/no");
        answer = scan.next();
    }
    while (!(answer.equals("yes") || answer.equals("no"))) ;

    if (answer.equalsIgnoreCase("yes")) {
        System.out.println("");
        System.out.println("");
        System.out.println("Housing Allowance: " + housing_allowance + " $");
        System.out.println("KWSP:contribution: " + KWSP + " $");
        System.out.println("Tax:contribution : " + housing_allowance + " $");
        System.out.println("Gross Salary Of Employee: " + (KWSP + housing_allowance + tax) + " $");
        System.out.println("");
        System.out.println("");
    } else if (answer.equalsIgnoreCase("no")) {
        System.out.println("");
        System.out.println("");
        System.out.println("KWSP:contribution: " + KWSP + " $");
        System.out.println("Tax:contribution : " + housing_allowance + " $");
        System.out.println("Gross Salary Of Employee: " + (KWSP + tax) + " /n");
        System.out.println("");
        System.out.println("");
    }
}