Java 为什么';我的if/else是否退出循环?
问题始于if声明(“您是否申请了本月的住房补贴?是/否”); 从用户那里得到答案后,应用程序仍然会再次循环 为什么我的if/else不退出循环? 我所有的逻辑都是正确的,除了if语句不会中断 我曾尝试使用break语句跳转if/else并停止无限运行代码,但它也不起作用 我错过了什么Java 为什么';我的if/else是否退出循环?,java,loops,if-statement,while-loop,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, hol
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");
}
}
publicstaticvoidmain(字符串[]args){
双倍基本工资、税收、KWSP、住房津贴、持有人;
扫描仪扫描=新扫描仪(System.in);
字符串应答=null;
System.out.println(“输入员工基本工资:”);
基本工资=scan.nextDouble();
做{
如果(基本工资=1){
税费=0.10*基本工资;
System.out.println(“强制税=“+tax+”$”);
住房津贴=0.5*基本工资;
}else if(basic_-salary>=6001&&basic_-salary=10001&&basic_-salary 0);//while循环
System.out.println(“工资不能为零或负”);
}
}
您正在输入基本工资
,然后运行do-while循环,直到基本工资>0
,并且不在任何地方修改基本工资
在用户输入0或某个负值之前,此条件不会变为false
我不确定你想要实现什么,你的逻辑是什么。
如果您想在计算后简单地打印消息,那么您可以使用简单的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");
}
看看你的while声明,似乎你想在工资上涨时打破循环!=0 所以你可以改变你的状况,比如:
while !(basic_salary > 0); //while loop
也许在循环的开头写下你的System.out.println
System.out.println("salary cannot be Zero or Negative");
我编辑了你的代码,现在就可以使用了。但不要只是盲目地使用它,试着阅读它并理解它为什么会起作用。 检查并进一步询问问题(如果有)
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("");
}
}
publicstaticvoidmain(字符串[]args){
双倍基本工资、税收、KWSP、住房津贴、持有人;
扫描仪扫描=新扫描仪(System.in);
字符串回答;
做{
System.out.println(“输入员工基本工资:”);
基本工资=scan.nextDouble();
如果(basic_-salary=6001&&basic_-salary=10001&&basic_-salary),因为用户必须输入“是”或“否”以外的内容要让代码达到中断
?这是一个你可以自己用调试器解决的问题,实际上比在这里提问更容易。不要在每次迭代中创建扫描仪处理系统。在main方法开始时创建一次,并在需要时使用它。但是在进入循环之前首先验证的是基本工资,如何我可以改变这个提示吗:如果您编写更小的函数并将它们组合在一起以实现所需的功能,您可能会发现调试更容易。您希望代码在哪一点或哪种情况下退出循环?