我的java程序有一个等式,它不能识别括号中的双精度值(double PropertyTax),因此可以在等式中使用它。有什么建议吗?谢谢
import javax.swing.JOptionPane;
import java.util.Scanner;
public class CSC212{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
String State = JOptionPane.showInputDialog("Enter the location ex: CA, AZ, WA, ect...");
if(State == "AL")
{
double PropertyTax = 0.33;
}
else if(State == "AK")
{
double PropertyTax = 1.04;
}
else if(State == "AZ")
{
double PropertyTax = 0.72;
}
else if(State == "AR")
{
double PropertyTax = 0.52;
}
else if(State == "CA")
{
double PropertyTax = 0.74;
}
else if(State == "CO")
{
double PropertyTax = 0.6;
}
else if(State == "CT")
{
double PropertyTax = 1.63;
}
else if(State == "DE")
{
double PropertyTax = 0.43;
}
else if(State == "FL")
{
double PropertyTax = 0.97;
}
else if(State == "GA")
{
double PropertyTax = 0.83;
}
else if(State == "HI")
{
double PropertyTax = 0.26;
}
else if(State == "ID")
{
double PropertyTax = 0.69;
}
else if(State == "IL")
{
double PropertyTax = 1.73;
}
else if(State == "IN")
{
double PropertyTax = 0.85;
}
else if(State == "IA")
{
double PropertyTax = 1.29;
}
else if(State == "KS")
{
double PropertyTax = 1.01;
}
else if(State == "KY")
{
double PropertyTax = 0.72;
}
else if(State == "LA")
{
double PropertyTax = 0.18;
}
else if(State == "ME")
{
double PropertyTax = 1.09;
}
else if(State == "MD")
{
double PropertyTax = 0.87;
}
else if(State == "MA")
{
double PropertyTax = 1.04;
}
else if(State == "MI")
{
double PropertyTax = 1.05;
}
else if(State == "MN")
{
double PropertyTax = 1.05;
}
else if(State == "MS")
{
double PropertyTax = 0.52;
}
else if(State == "MO")
{
double PropertyTax = 0.91;
}
else if(State == "MT")
{
double PropertyTax = 0.83;
}
else if(State == "NE")
{
double PropertyTax = 1.76;
}
else if(State == "NV")
{
double PropertyTax = 0.84;
}
else if(State == "NH")
{
double PropertyTax = 1.86;
}
else if(State == "NJ")
{
double PropertyTax = 1.89;
}
else if(State == "NM")
{
double PropertyTax = 0.55;
}
else if(State == "NY")
{
double PropertyTax = 1.23;
}
else if(State == "NC")
{
double PropertyTax = 0.78;
}
else if(State == "ND")
{
double PropertyTax = 1.42;
}
else if(State == "OH")
{
double PropertyTax = 1.36;
}
else if(State == "OK")
{
double PropertyTax = 0.74;
}
else if(State == "OR")
{
double PropertyTax = 0.87;
}
else if(State == "PA")
{
double PropertyTax = 1.35;
}
else if(State == "RI")
{
double PropertyTax = 1.35;
}
else if(State == "SC")
{
double PropertyTax = 0.5;
}
else if(State == "D.C.")
{
double PropertyTax = 0.46;
}
else if(State == "SD")
{
double PropertyTax = 1.28;
}
else if(State == "TN")
{
double PropertyTax = 0.86;
}
else if(State == "TX")
{
double PropertyTax = 1.18;
}
else if(State == "UT")
{
double PropertyTax = 0.6;
}
else if(State == "VT")
{
double PropertyTax = 1.59;
}
else if(State == "WA")
{
double PropertyTax = 0.92;
}
else if(State == "WV")
{
double PropertyTax = 0.49;
}
else if(State == "WI")
{
double PropertyTax = 1.76;
}
else if(State == "WY")
{
double PropertyTax = 0.58;
}
String houseValue = JOptionPane.showInputDialog("Enter the value of the home");
double hValue = Integer.parseInt(houseValue);
double product = PropertyTax * hValue;
JOptionPane.showMessageDialog(null, "The property tax is " +product);
}
}
您可以用另一种方法执行此操作,并删除所有if/else语句。在输入框中显示状态列表,用户可以从中选择所需状态。选择的索引与包含每个状态的所有Tex速率的并行数组相关。有几种方法可以使这更容易。
// Component for JOptionPane to ensure it is always displayed on top
JFrame iFrame = new JFrame();
iFrame.setAlwaysOnTop(true);
iFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
iFrame.setLocationRelativeTo(null);
double propertyTax = 0.0d;
// Parallel Arrays
// List values t display in Input-Box
String[] states = {"Alabama", "Alaska", "Arizona", "Arkansas", "California",
"Colorado", "Connecticut", "Delaware", "Florida", "Georgia",
"Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas",
"Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts",
"Michigan", "Minnesota", "Mississippi", "Missouri", "Montana",
"Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico",
"New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma",
"Oregon", "Pennsylvania", "Rhode Island", "South Carolina",
"South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia",
"Washington", "West Virginia", "Wisconsin", "Wyoming"};
// Tax rates related to the above States
double[] taxes = {0.33d, 1.04d, 0.72d, 0.52d, 0.74d, 0.6d, 1.63d, 0.43d,
0.97d, 0.83d, 0.26d, 0.69d, 1.73d, 0.85d, 1.29d, 1.01d,
0.72d, 0.18, 1.04d, 1.09d, 0.87d, 1.05d, 1.05d, 0.52d,
0.91d, 0.83d, 1.76d, 0.84d, 1.86d, 1.89d, 0.55d, 1.23d,
0.78d, 1.42d, 1.36d, 0.74d, 0.87d, 1.35d, 1.35d, 0.5d,
0.46d, 1.28d, 0.86d, 1.18d, 0.6d, 1.59d, 0.92d, 0.49d,
1.76d, 0.58d};
// Show Input-Box dialog...
String selectedState = (String) JOptionPane.showInputDialog(iFrame, "Select a State:",
"State Selection", JOptionPane.QUESTION_MESSAGE, null, states, "");
iFrame.dispose();
if (selectedState != null && !selectedState.equals("")) {
// Get the index value of the selected State so as
// to get the tax rate related to that state.
for (int i = 0; i < states.length; i++) {
if (selectedState.equals(states[i])) {
propertyTax = taxes[i];
break;
}
}
// Get house value from User...
String houseValue = JOptionPane.showInputDialog(iFrame, "Enter the value of the home", "Home Value",
JOptionPane.QUESTION_MESSAGE);
if (houseValue == null || houseValue.equals("")) {
houseValue = "0.0";
}
iFrame.dispose();
double hValue = Double.parseDouble(houseValue);
// Calculate the property tax
double product = hValue * (propertyTax / 100.0d);
// Display results...
JOptionPane.showMessageDialog(iFrame, "The property tax is " + product);
iFrame.dispose();
}
不一定更短,但它确实消除了用户输入错误的可能性。
if statements in Smarty have much the same flexibility as php if statements, with a few added features for the . Everyif must be paired with an/if .else andelseif are also permitted. "eq", "ne","neq",
概要 <#if condition> ... <#elseif condition2> ... <#elseif condition3> ... ... <#else> ... </#if> 这里: condition, condition2, 等:将被计算成布尔值的表达式。 elseif 和 else 是可选的。 描述 你可以使用 if, elseif 和 else 指令来条
一个If语句后面跟着一个或多个ElseIf语句,这些语句由布尔表达式组成,然后是一个默认的else语句,当所有条件都变为false时执行。 语法 (Syntax) 以下是VBScript中If Elseif - Else语句的语法。 If(boolean_expression) Then Statement 1 ..... ..... Statement n ElseIf
if语句后面可以跟一个else if...else语句,这对于使用单个if...else if语句测试各种条件非常有用。 语法 (Syntax) if...else if...else语句的语法如下 - if boolean_expression_1 { /* Executes when the boolean expression 1 is true */ } else if boolea
if语句后面可以跟一个else if...else语句,这对于使用单个if...else if语句测试各种条件非常有用。 语法 (Syntax) if...else if...else语句的语法如下 - if boolean_expression_1 { /* Executes when the boolean expression 1 is true */ } else if boolea
我想隐藏< code >投资组合和< code >订单菜单。 我成功地用隐藏了菜单。 我的问题是,我应该如何使用隐藏? 我不太理解HTML和Angular语法。。。 编辑