问题
In the following method, it doesn't do the addition for the SolarPanel correctly when the input window appears.
When I type in "Electric" followed by "Yes/yes" i get the following Basic Price:20000 Electric Model:2000 Total:22000
but when I do "electric" + "yes/Yes" I get
Basic Price:20000 Electric Model:2000 Solar Panel:5000 Total:27000
how come?
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
For example, it asks: Electric or Hybrid ( I chose Electric, this should add 2000 to the total cost) Electric
Solar Panel (Yes or No, I chose yes. This should add 5000 to the total cost) Yes
The console window displays this however, it doesn't do the addition for the Solar Panel. Basic Price:20000 Electric Model:2000 Total:22000
Additionally, how can I factor in the discount of -500 on the total when both an Electric Model and Solar Panel are chosen.
The whole code is posted below. Any advice?
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
回答1:
You need to use double bracket and '&&' as below:-
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:
I think instead of single &
you need to have double &&
in your if statement.
Change this:
if (typeOfCarCost.equals("Electric") || (typeOfCarCost.equals("electric") & (SolarPanelCost.equals("No") || (SolarPanelCost.equals("no")))))
to this:
if (typeOfCarCost.equals("Electric") || (typeOfCarCost.equals("electric") &&(SolarPanelCost.equals("No") || (SolarPanelCost.equals("no")))))
Similarly in rest of your if
statements. Change that!
On a side note:
As RhinoFeeder commented below its important that you understand the difference between & and &&. So the difference is simple:
&& is logical AND. It evaluates the left side of the operation, if it's true, it continues and evaluates the right side.
& is bitwise AND. It evaluates both sides of the operation.
EDIT:-
Even after your edit you missed the & here
& SolarPanelSelected.equals("Yes")
Try to change your if else block like this:
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);
}
回答3:
You use &
in a couple of places when you should use &&
. Then, it always computes both sides. This might not matter, but it's bad form.
Second, you have control of what askCarType
will return. Don't use String
s. Create an enum
:
enum CarType {
ELECTRIC, HYBRID //Include GASOLINE, DIESEL if you want.
}
This way, you can just use ==
instead of equals
. Even if you are forced to use String
s, document which strings you are returning from askCarType
, and only return one of Electric
and electric
. Any time you are checking for equality with one of two capitalized forms of a string you control, you are making a mistake. Do something similar with askSolarPanel
, though you might want to use a boolean
instead.
When you do complex boolean expressions, use parentheses. Use them a lot for clarity.
Solar panels on cars? Why?
来源:https://stackoverflow.com/questions/20291656/if-statement-operator-being-ignored