我正在尝试让下面的嵌套语句起作用,但是在让它们执行超过第一个语句时遇到了问题。我尝试嵌套语句,但只是第一个if执行。任何关于格式化的反馈都非常感谢,我知道可能有更有效的方法来实现这一点,但我必须使用嵌套语句来执行代码。
package incometax;
import java.util.Scanner;
import java.text.DecimalFormat;
public class IncomeTax {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
DecimalFormat df = new DecimalFormat ("#,###,000.00");
String singleStatus = "single";
String marriedStatus = "married";
String maritalStatus = "";
double annualIncome = 0;
double taxAmount = 0;
System.out.println("Please enter your martial status: ");
maritalStatus = scnr.next();
if (maritalStatus.compareTo(singleStatus) == 0){
System.out.println("Please enter your annual income: ");
annualIncome = scnr.nextDouble();
if (annualIncome <= 30000){
taxAmount = (annualIncome * .15);
System.out.println("Based on annual income of "+ "$ " +
df.format(annualIncome) + " your tax is " + "$ " +
df.format(taxAmount));
if (annualIncome > 30000){
taxAmount = (annualIncome * .25);
System.out.println("Based on annual income of "+ "$ " +
df.format(annualIncome) +
" your tax is " + "$ " + df.format(taxAmount));
}
}
else {
if (maritalStatus.compareTo(marriedStatus) == 0){
if(annualIncome <= 30000){
taxAmount = (annualIncome * .12);
System.out.println("Based on annual income of "+ "$ "
+ df.format(annualIncome) +
" your tax is " + "$ " + df.format(taxAmount));
if(annualIncome > 30000){
taxAmount = (annualIncome * .20);
System.out.println("Based on annual income of "+
"$ " + df.format(annualIncome) +
" your tax is " + "$ " + df.format(taxAmount));
}
}
}
}
}
}
}
答案 0 :(得分:0)
if
时你将进入第一个annualIncome <= 30000
。但是嵌套if中的条件是annualIncome > 30000
。这总是错误的。请尝试if else
而不是嵌套if
if (annualIncome <= 30000) {
taxAmount = (annualIncome * .15);
System.out.println("Based on annual income of "+ "$ " +
df.format(annualIncome) + " your tax is " + "$ " +
df.format(taxAmount));
}
else {
taxAmount = (annualIncome * .25);
System.out.println("Based on annual income of "+ "$ " +
df.format(annualIncome) +
" your tax is " + "$ " + df.format(taxAmount));
}
答案 1 :(得分:0)
看起来你错放了if语句的括号。 试试这个应该有效的代码
package incometax;
import java.util.Scanner;
import java.text.DecimalFormat;
public class IncomeTax {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
DecimalFormat df = new DecimalFormat ("#,###,000.00");
String singleStatus = "single";
String marriedStatus = "married";
String maritalStatus = "";
double annualIncome = 0;
double taxAmount = 0;
System.out.println("Please enter your martial status: ");
maritalStatus = scnr.next();
if (maritalStatus.compareTo(singleStatus) == 0){
System.out.println("Please enter your annual income: ");
annualIncome = scnr.nextDouble();
if (annualIncome <= 30000){
taxAmount = (annualIncome * .15);
System.out.println("Based on annual income of "+ "$ " +
df.format(annualIncome) + " your tax is " + "$ " +
df.format(taxAmount));
} else {
taxAmount = (annualIncome * .25);
System.out.println("Based on annual income of "+ "$ " +
df.format(annualIncome) +
" your tax is " + "$ " + df.format(taxAmount));
}
} else if (maritalStatus.compareTo(marriedStatus) == 0){
if(annualIncome <= 30000){
taxAmount = (annualIncome * .12);
System.out.println("Based on annual income of "+ "$ "
+ df.format(annualIncome) +
" your tax is " + "$ " + df.format(taxAmount));
} else {
taxAmount = (annualIncome * .20);
System.out.println("Based on annual income of "+
"$ " + df.format(annualIncome) +
" your tax is " + "$ " + df.format(taxAmount));
}
}
}
}
答案 2 :(得分:0)
一个体面的IDE将能够格式化您的代码,使这种嵌套问题更加明显。
它可能不是问题的一部分,但您还有几行重复的代码,请查看方法。例如,以下是更清洁(注意没有关于婚姻状况或年收入的确认)
import java.text.DecimalFormat;
import java.util.Scanner;
public class IncomeTax
{
private static final DecimalFormat DF = new DecimalFormat("#,###,000.00");
private static final String SINGLE_STATUS = "single";
private static final String MARRIED_STATUS = "married";
private static final double SINGLE_LOW_RATE = .15;
private static final double SINGLE_HIGH_RATE = .25;
private static final double MARRIED_LOW_RATE = .12;
private static final double MARRIED_HIGH_RATE = .20;
private static final int LOWER_BRACKET = 30000;
public static void main(String[] args)
{
Scanner scnr = new Scanner(System.in);
String maritalStatus;
double annualIncome;
System.out.println(
"Please enter your martial status: ");
maritalStatus = scnr.next();
System.out.println("Please enter your annual income: ");
annualIncome = scnr.nextDouble();
if (SINGLE_STATUS.equalsIgnoreCase(maritalStatus))
{
printTaxRate(annualIncome, SINGLE_LOW_RATE, SINGLE_HIGH_RATE);
} else
{
if (MARRIED_STATUS.equalsIgnoreCase(maritalStatus))
{
printTaxRate(annualIncome, MARRIED_LOW_RATE, MARRIED_HIGH_RATE);
}
}
}
private static double calcTaxAmount(double annualIncome, double taxRate)
{
return annualIncome * taxRate;
}
private static void printTaxRate(double annualIncome, double lowRate, double highRate)
{
double taxAmount;
if (annualIncome <= LOWER_BRACKET)
{
taxAmount = calcTaxAmount(annualIncome, lowRate);
} else
{
taxAmount = calcTaxAmount(annualIncome, highRate);
}
System.out.println("Based on annual income of " + "$ "
+ DF.format(annualIncome)
+ " your tax is " + "$ " + DF.format(taxAmount));
}
}