Java 我的代码中关于调用if语句中的单元的未知错误
在我的程序中,它会要求您输入一个值,该值会给您相应的扣减,但我的代码一直给我相同的输出,即“费率扣减:634.57” 即使它应该给我更高的扣减额“扣减率:1624.57”或“扣减率:2655.72” 我为我生疏的英语道歉,但非常感谢你能帮助我Java 我的代码中关于调用if语句中的单元的未知错误,java,netbeans,Java,Netbeans,在我的程序中,它会要求您输入一个值,该值会给您相应的扣减,但我的代码一直给我相同的输出,即“费率扣减:634.57” 即使它应该给我更高的扣减额“扣减率:1624.57”或“扣减率:2655.72” 我为我生疏的英语道歉,但非常感谢你能帮助我 public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStre
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Cp11bTaxActivityKedawenJ jr = new Cp11bTaxActivityKedawenJ();
headerdisplay();
}
public static void headerdisplay() throws IOException
{
System.out.println("Monthly Salary Tax Checker");
Name();
try
{
Salary();
} catch (IOException e) {
}
System.out.println("==============================================================");
}
public static String Name() throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String inputN;
System.out.print("Input a name: ");
inputN = br.readLine();
return inputN;
}
public static double Salary() throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Input a salary: ");
double inputS = Double.parseDouble(br.readLine());
TaxCheck(inputS);
return 0;
}
public static void TaxCheck(Double salary) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
double totalSalary = salary;
double salary1 = 15000.00;
double salary2 = 20000.00;
double salary3 = 25000.00;
double salary4 = 30000.00;
double salary5 = 35000.00;
double deduction3 = 634.57;
double deduction4 = 1624.57;
double deduction5 = 2655.72;
if (salary >= salary3)
{
System.out.println("Rate Deduction: " + deduction3 + "");
totalSalary = totalSalary - deduction3;
System.out.println("Total salary: " + totalSalary + "");
} else {
System.out.println("This salary does not have a deduction");
System.out.println("Total salary: "+totalSalary+"");
}
again();
if (salary >= salary4)
{
System.out.println("Rate Deduction: " + deduction4 + "");
totalSalary = totalSalary - deduction4;
System.out.println("Total salary: " + totalSalary + "");
} else {
System.out.println("This salary does not have a deduction");
System.out.println("Total salary: " + totalSalary + "");
}
again();
if (salary >= salary5)
{
System.out.println("Rate Deduction: " + deduction5 + "");
totalSalary = totalSalary - deduction5;
System.out.println("Total salary: " + totalSalary + "");
} else {
System.out.println("This salary does not have a deduction");
System.out.println("Total salary: " + totalSalary + "");
}
again();
}
public static void again() throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Would you like to try again?:\n [Y] for Yes [N] for No");
String choice = br.readLine();
if (choice.equals("Y"))
{
headerdisplay();
}
if (choice.equals("N"))
{
System.out.println("Thank you for using me! (^_^) Goodbye");
}
}
}
对于检查薪资4和薪资5的if块,无法访问该代码,因为在检查薪资3的if块之后再次调用
(
)
再次调用方法()
方法,仅在TaxCheck()方法的末尾调用,如下所示
此外,您应该将薪资检查的顺序从高到低,即从薪资5更改为薪资3。我是新来的,因此,如果此问题/帖子太长,或者如果我将整个代码粘贴到此处是不好的,粘贴整个代码比给我们不完整的图片要好。代码不在类中,我再次表示歉意。你没有发布整个课程。这将不会编译为已发布。我想是工资的问题。您没有遵循Java编码标准。由于样式的原因,您的代码很难阅读。您可以简化文本用户界面。空的捕捉块是一个非常糟糕的主意。如果发生什么事你都不知道。打印堆栈跟踪。如前所述,即使我输入35000,它仍然会给我相等的扣减,就像我输入25000一样。我更改我的答案,你需要颠倒顺序,如果(薪水>=薪水5)必须是第一位,依此类推
public static void main(String[] args)throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//Cp11bTaxActivityKedawenJ jr = new Cp11bTaxActivityKedawenJ ();
headerdisplay();
}
public static void headerdisplay () throws IOException{
System.out.println("Monthly Salary Tax Checker");
Name();
try{
Salary();
}catch(IOException e){
}
System.out.println("==============================================================");
}
public static String Name()throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String inputN;
System.out.print("Input a name: ");
inputN= br.readLine();
return inputN;
}
public static double Salary()throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Input a salary: ");
double inputS = Double.parseDouble(br.readLine());
TaxCheck (inputS);
return 0;
}
public static void TaxCheck(Double salary)throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
double totalSalary = salary;
double salary1 = 15000.00;
double salary2 = 20000.00;
double salary3 = 25000.00;
double salary4 = 30000.00;
double salary5 = 35000.00;
double deduction3 = 634.57;
double deduction4 = 1624.57;
double deduction5 = 2655.72;
if (salary>=salary5){
System.out.println("Rate Deduction: "+deduction5+"");
totalSalary = totalSalary - deduction5;
System.out.println("Total salary: "+totalSalary+"");
}
else if (salary>=salary4){
System.out.println("Rate Deduction: "+deduction4+"");
totalSalary = totalSalary - deduction4;
System.out.println("Total salary: "+totalSalary+"");
}
else if (salary>=salary3){
System.out.println("Rate Deduction: "+deduction3+"");
totalSalary = totalSalary - deduction3;
System.out.println("Total salary: "+totalSalary+"");
}
else {
System.out.println("This salary does not have a deduction");
System.out.println("Total salary: "+totalSalary+"");
}
again();
}
public static void again() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Would you like to try again?:\n [Y] for Yes [N] for No");
String choice = br.readLine();
if (choice.equals("Y")){
headerdisplay();
}
if (choice.equals("N")){
System.out.println("Thank you for using me! (^_^) Goodbye");
}
}