如果语句运算符被忽略?

时间:2013-11-29 18:36:43

标签: java if-statement addition

在以下方法中,当输入窗口出现时,它不会正确添加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总成本) 是

控制台窗口会显示此信息,但它不会为太阳能电池板添加。 基本价格: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

3 个答案:

答案 0 :(得分:7)

我认为您需要在if语句中使用&而不是单&&

改变这个:

if (typeOfCarCost.equals("Electric") || (typeOfCarCost.equals("electric") & (SolarPanelCost.equals("No") || (SolarPanelCost.equals("no")))))

到此:

if (typeOfCarCost.equals("Electric") || (typeOfCarCost.equals("electric") &&(SolarPanelCost.equals("No") || (SolarPanelCost.equals("no")))))

在其他if语句中也是如此。改变它!

旁注:

正如RhinoFeeder在下面评论的那样,重要的是要了解&和和&&所以区别很简单:

&& 逻辑AND 。它评估操作的左侧,如果是,则继续并评估右侧。

& 按位AND 。它评估了操作的两个方面。

修改: -

即使在您进行编辑后,您也错过了& 此处

& SolarPanelSelected.equals("Yes")

尝试更改if else块,如下所示:

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

答案 1 :(得分: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);
                                    }

答案 2 :(得分:0)

当您使用&时,您可以在几个地方使用&&。然后,它总是计算双方。这可能无关紧要,但形式不好。

其次,您可以控制askCarType将返回的内容。不要使用String。创建enum

enum CarType {
    ELECTRIC, HYBRID //Include GASOLINE, DIESEL if you want.
} 

这样,您只需使用==代替equals即可。即使您被迫使用String,也要记录您从askCarType返回的字符串,并且仅返回Electricelectric中的一个。每当你用你控制的字符串的两种大写形式之一检查是否相等时,你就是犯了错误。使用askSolarPanel执行类似操作,但您可能需要使用boolean

当您执行复杂的布尔表达式时,请使用括号。为了清晰起见,请使用它们。

汽车太阳能电池板?为什么呢?