在以下方法中,当输入窗口出现时,它没有正确地为SolarPanel进行添加。

当我输入“Electric”,然后输入“Yes / yes”时,我得到以下信息
基本价格:20000
电气型号:2000
合计:22000

但是当我做“电动” +“是/是”时,我得到

基本价格:20000
电气型号:2000
太阳能板:5000
总计:27000

怎么会?

public static int CalculateCost()
                    {
                        String typeOfCarCost = askCarType();
                        String SolarPanelCost = askSolarPanel();
                        int basicPrice = 20000;
                        int ElectricModel = 2000;
                        int SolarPanel = 5000;
                        int total = 0;

                        if (typeOfCarCost.equals("Electric") || (typeOfCarCost.equals("electric") && (SolarPanelCost.equals("No") || (SolarPanelCost.equals("no")))))
                                {
                                    total = basicPrice + ElectricModel;
                                    System.out.println("Basic Price:" + basicPrice);
                                    System.out.println("Electric Model:" + ElectricModel);
                                    System.out.println("Total:" + total);

                                }
                        else if (typeOfCarCost.equals("Electric") || (typeOfCarCost.equals("electric") && (SolarPanelCost.equals("Yes") || (SolarPanelCost.equals("yes")))))
                                {
                                    total = basicPrice + ElectricModel + SolarPanel;
                                    System.out.println("Basic Price:" + basicPrice);
                                    System.out.println("Electric Model:" + ElectricModel);
                                    System.out.println("Solar Panel:" + SolarPanel);
                                    System.out.println("Total:" + total);
                                }
                        else
                        {
                            total += basicPrice;
                            System.out.println("Basic Price:" + basicPrice);
                            System.out.println("Total:" + total);
                        }
                        return total;
                    }//ends CalculateCost

例如,它要求:
电动或混合动力(我选择了电动,这将使总成本增加2000)
电动

太阳能电池板(是或否,我选择是。这将使总成本增加5000)


控制台窗口将显示此内容,但是不会对Solar Panel进行添加。
基本价格:20000
电气型号:2000
合计:22000

此外,当同时选择“电气模型”和“太阳能电池板”时,我如何考虑总折扣的-500。

整个代码如下。
有什么建议吗?
import javax.swing.*;
public class short7 {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub

    PrintOptions();

}//ends main

    public static String askCarType()
    {
        String typeOfCar;
        typeOfCar = JOptionPane.showInputDialog("Electric or Hybrid?");
        if (!typeOfCar.equals("Electric") && (!typeOfCar.equals("electric") && (!typeOfCar.equals("Hybrid") && (!typeOfCar.equals("hybrid")))))
        {
            JOptionPane.showMessageDialog(null, "You have to choose either an Electric or Hybrid type of car.");
            typeOfCar = JOptionPane.showInputDialog("Electric or Hybrid?");
        }
        return typeOfCar;
    }//ends askCarType

        public static String askSolarPanel()
        {
            String wantSolarPanel;
            wantSolarPanel = JOptionPane.showInputDialog("Do you want a Solar Panel?");
            if (!wantSolarPanel.equals("Yes") && (!wantSolarPanel.equals("yes") && (!wantSolarPanel.equals("No") && (!wantSolarPanel.equals("no")))))
            {
                JOptionPane.showMessageDialog(null, "You have to enter either Yes or No");
                wantSolarPanel = JOptionPane.showInputDialog("Do you want a Solar Panel?");
            }
            return wantSolarPanel;
        }//ends askSolarPanel

            public static int calculateDiscount()
            {
                String typeOfCarSelected = askCarType();
                String SolarPanelSelected = askSolarPanel();
                int Discount = 0;
                if (typeOfCarSelected.equals("Electric") && (typeOfCarSelected.equals("electric") && (typeOfCarSelected.equals("Hybrid") && (typeOfCarSelected.equals("hybrid") & SolarPanelSelected.equals("Yes") || SolarPanelSelected.equals("yes")))))
                {
                     Discount = 500;
                }
                else
                {
                    Discount = 0;
                }
                return Discount;
            }//ends calculateDiscount

                    public static int CalculateCost()
                    {
                        String typeOfCarCost = askCarType();
                        String SolarPanelCost = askSolarPanel();
                        int basicPrice = 20000;
                        int ElectricModel = 2000;
                        int SolarPanel = 5000;
                        int total = 0;

                        if (typeOfCarCost.equals("Electric") || (typeOfCarCost.equals("electric") & (SolarPanelCost.equals("No") || (SolarPanelCost.equals("no")))))
                                {
                                    total = basicPrice + ElectricModel;
                                    System.out.println("Basic Price:" + basicPrice);
                                    System.out.println("Electric Model:" + ElectricModel);
                                    System.out.println("Total:" + total);

                                }
                        else if (typeOfCarCost.equals("Electric") || (typeOfCarCost.equals("electric") & (SolarPanelCost.equals("Yes") || (SolarPanelCost.equals("yes")))))
                                {
                                    total = basicPrice + ElectricModel + SolarPanel;
                                    System.out.println("Basic Price:" + basicPrice);
                                    System.out.println("Electric Model:" + ElectricModel);
                                    System.out.println("Solar Panel:" + SolarPanel);
                                    System.out.println("Total:" + total);
                                }
                        else
                        {
                            total += basicPrice;
                            System.out.println("Basic Price:" + basicPrice);
                            System.out.println("Total:" + total);
                        }
                        return total;
                    }//ends CalculateCost


                            public static void PrintOptions()
                            {
                            int printTotal = CalculateCost();
                            }//ends PrintOptions



}//ends class short7

最佳答案

您需要使用双括号和“&&”,如下所示:-

if ((typeOfCarCost.equals("Electric") || typeOfCarCost.equals("electric")) && ((SolarPanelCost.equals("No") || SolarPanelCost.equals("no")))
                                    {
                                        total = basicPrice + ElectricModel;
                                        System.out.println("Basic Price:" + basicPrice);
                                        System.out.println("Electric Model:" + ElectricModel);
                                        System.out.println("Total:" + total);

                                    }
else if ((typeOfCarCost.equals("Electric") || typeOfCarCost.equals("electric")) && (SolarPanelCost.equals("Yes") || SolarPanelCost.equals("yes")))
                                    {
                                        total = basicPrice + ElectricModel + SolarPanel;
                                        System.out.println("Basic Price:" + basicPrice);
                                        System.out.println("Electric Model:" + ElectricModel);
                                        System.out.println("Solar Panel:" + SolarPanel);
                                        System.out.println("Total:" + total);
                                    }

关于java - 如果语句运算符被忽略?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20291656/

10-08 21:03