在方法askCarType()
和askSolarPanel()
中,inputdialog运行两次,一次是在他们自己的方法中,一次是在最终方法PrintOptions()
中。
我需要他们只运行一次,这是在最后的方法PrintOptions()
。
我该怎么做?
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"))
&& ((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();
final int basicPrice = 20000;
final int ElectricModel = 2000;
final int SolarPanel = 5000;
final int Discount = calculateDiscount();
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 - Discount;
System.out.println("Basic Price:" + basicPrice);
System.out.println("Electric Model:" + ElectricModel);
System.out.println("Solar Panel:" + SolarPanel);
System.out.println("Discount:" + Discount);
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() {
CalculateCost();
}// ends PrintOptions
}// ends class short7
答案 0 :(得分:1)
您正在拨打askCarType()
和askSolarPanel()
两次,您需要的是给他们打电话一次!所以在CalculateCost()
中调用它们并发送两个字符串{{1}和typeOfCarCost
这样的SolarPanelCost
方法:
calculateDiscount
并在public static int calculateDiscount(String typeOfCarSelected, String SolarPanelSelected) {
int Discount = 0;
if ((typeOfCarSelected.equals("Electric") || typeOfCarSelected.equals("electric")) && ((SolarPanelSelected.equals("Yes") || SolarPanelSelected.equals("yes")))) {
Discount = 500;
} else {
Discount = 0;
}
return Discount;
}//ends calculateDiscount
CalculateCost()
答案 1 :(得分:0)
您可以声明两个类变量。
String typeOfCar;
String wantSolarPanel;
在askCarType()方法中分配
typeOfCar = ................
并在askSolarPanel()方法中分配
wantSolarPanel = .....................
然后使用CalculateCost()和calculateDiscount()中的这些变量,而不是再次调用askCarType()和askSolarPanel()方法。