我的任务是创建一个小型的美国国税局临时税收计算器。要完成的作业目标是:
编写一个提示用户输入以下信息的类:
备案状态:单身或结婚(单身填写1,结婚填写2)
应纳税所得额
计算和打印:
备案状态
应纳税所得额
联邦税
我应该对代码中包含的每个参数都有特定的计算,包括0到27050、27050到65500等等。我遇到的真正问题是,我似乎不能让数字真正显示出来。
我的代码如下:
import java.util.Scanner;
public class P4_Icel_Murad_IRS
{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.printf("Enter Marital Status: (Single = 1, Married = 2):");
int relation = in.nextInt();
System.out.printf("Enter taxable income:");
int tax = in.nextInt();
if (relation == 1){
String status = "Single";
if (tax <= 27050){
double tax2 = tax - 0.0;
double tax3 = (tax2 * 0.15);
System.out.printf(status,tax,tax3);
}else if (27050 < tax && tax >= 65550){
double tax2 = (tax - 27050.0)* 0.275;
double tax3 = 4057.5 + tax2;
}else if (65550 < tax && tax <= 136750){
double tax2 = (tax - 65550.0) * 0.305;
double tax3 = 14645.0 + tax2;
}else if (136750 < tax && tax <= 297350){
double tax2 = (tax - 136750.0 - tax) * 0.355;
double tax3 = 36361.00 + tax2;
}else if (297350 < tax && tax <= 1e100){
double tax2 = (tax - 297350) * 0.391;
double tax3 = 93374.0 + tax2;
}
}
if (relation == 2){
String status = "Married";
if (tax <= 27050){
double tax2 = tax - 0.0;
double tax3 = (tax2 * 0.15);
System.out.printf(status,tax,tax3);
}else if (27050 < tax && tax >= 65550){
double tax2 = (tax - 27050.0)* 0.275;
double tax3 = 4057.5 + tax2;
}else if (65550 < tax && tax <= 136750){
double tax2 = (tax - 65550.0) * 0.305;
double tax3 = 14645.0 + tax2;
}else if (136750 < tax && tax <= 297350){
double tax2 = (tax - 136750.0 - tax) * 0.355;
double tax3 = 36361.00 + tax2;
}else if (297350 < tax && tax <= 1e100){
double tax2 = (tax - 297350) * 0.391;
double tax3 = 93374.0 + tax2;
}
}
}
}发布于 2015-09-22 10:18:32
您将在单个if块中本地声明变量tax2 tax3。
您可以打印其中的一个块,但不是所有块。
将它们的声明移到
int tax = in.nextInt();
double tax2 = 0;
double tax3 = 0;然后在所有逻辑的末尾打印出它们的值。
https://stackoverflow.com/questions/32707341
复制相似问题