带有JOptionpane GUI和elseif值[重复]的Java

问题描述 投票:0回答:1

我的Java程序的方程式不能从方括号内识别出double值(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);  

            }

}
java double joptionpane brackets
1个回答
0
投票

您可以使用另一种方法来消除所有的[[if / else语句。在输入框中显示状态列表,用户可以从中选择所需状态选择的索引与包含每个状态的所有Tex Rates的并行数组有关。有几种方法可以简化此过程。

enter image description here

// 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(); }

不一定较短,但确实消除了用户输入错误的可能性。
© www.soinside.com 2019 - 2024. All rights reserved.