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

时间:2017-12-15 14:53:52

标签: java loops if-statement while-loop

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

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

我错过了什么?

   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 个答案:

答案 0 :(得分:1)

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

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

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

也许是这样的:

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

答案 1 :(得分:0)

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

所以你可以改变你的状况:

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

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

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

答案 2 :(得分:0)

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

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