gpt4 book ai didi

java - 根据婚姻状况查找税收收入

转载 作者:行者123 更新时间:2023-12-02 00:45:56 24 4
gpt4 key购买 nike

我正在做这个java作业,使用if else语句和joptionpane根据婚姻状况查找税收收入。问题是在获得税收和婚姻状况输入后,输出没有显示,它只是终止了程序。也没有错误。

我尝试在第一个 if 语句后面加一个分号,它以某种方式工作,但它显示 2 个不同的输出而不是 1


public class Program_4_2 {

public static void main (String[] args) {

String status;
String tax;
double tax_income;

status = JOptionPane.showInputDialog("Please enter s for single, m for married: ");
tax = JOptionPane.showInputDialog("Enter Your Tax Income: ");
tax_income = Double.parseDouble(tax);

// SINGLE TAX;
if (status == "s")
{
if (tax_income > 0 && tax_income < 8000)
{
tax_income = tax_income * 0.1 + 0;
JOptionPane.showMessageDialog(null, "Your Tax is " + tax_income);
}

else if (tax_income > 8000 && tax_income < 32000)
{
tax_income = (tax_income - 8000) * 0.15 + 800;
JOptionPane.showMessageDialog(null, "Your Tax is " + tax_income);
}
else if (tax_income > 32000)
{
tax_income = (tax_income - 32000) * 0.25 + 4400;
JOptionPane.showMessageDialog(null, "Your Tax is " + tax_income);
}
}

// Married Tax
if (status == "m")
{
if (tax_income > 0 && tax_income < 16000)
{
tax_income = tax_income * 0.1 + 0;
JOptionPane.showMessageDialog(null, "Your Tax is " + tax_income);
}

else if (tax_income > 16000 && tax_income < 64000)
{
tax_income = (tax_income - 16000) * 0.15 + 1600;
JOptionPane.showMessageDialog(null, "Your Tax is " + tax_income);
}
else if (tax_income > 64000)
{
tax_income = (tax_income - 64000) * 0.25 + 8800;
JOptionPane.showMessageDialog(null, "Your Tax is " + tax_income);
}
}

}
}

示例输出如下:

user input status -> "s" -> user then inputs tax -> "9000" -> output should show -> "950" or "900" if the status input is m

最佳答案

看起来你已经成功了,但为了将来的引用,不要使用 == 来比较 Strings

比较字符串的正确方法是使用 .equals ,如下所示:

status.equals("m")

关于java - 根据婚姻状况查找税收收入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57897739/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com