This question already has answers here:
How do I compare strings in Java?
(23个答案)
在10个月前关闭。
我正在做这个Java作业,使用if else语句和joptionpane根据婚姻状况查找税收收入。问题是在获得税收和婚姻状况输入后,输出不会显示出来,只是终止了程序。也没有错误。
我尝试将分号放在第一个if语句之后,并且以某种方式起作用,但是它显示了2种不同的输出,而不是1种
示例输出为:
用户输入状态->“ s”->用户然后输入税款->“ 9000”->输出应显示->“ 950”或“ 900”(如果状态输入为m
(23个答案)
在10个月前关闭。
我正在做这个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);
}
}
}
}
示例输出为:
用户输入状态->“ s”->用户然后输入税款->“ 9000”->输出应显示->“ 950”或“ 900”(如果状态输入为m
最佳答案
看来您可以使用它,但为了将来参考,请不要使用==
来比较Strings
。
比较字符串的正确方法是使用.equals
,如下所示:
status.equals("m")
08-24 18:54