我的程序遇到了一些困难。我试图在JOption窗格的对话框中输出一个私有实例变量。
公共类CarShowroom {
private String brand;
private double horsepower;
private String type;
private int speed;
private int engineSize;
public CarShowroom (String model, String make, int power) {
brand = model;
type = make;
speed = power;
}
public String getBrand() {
return model;
}
public String getType() {
return make;
}
public int getSpeed() {
return power;
}
public void setEngineSize(int engineSize) {
this.engineSize = engineSize;
}
public void setHorsepower(double horsepower) {
this.horsepower = horsepower;
我在测试器类中创建了一个对象,该对象然后将值赋予setEngineSize()方法。但是,当我然后尝试在带有字符串output1的对话框中输出此消息时,它不显示我为引擎或马力设置的值,我可能错过了什么?
import javax.swing.JOptionPane;
公共类CarShowroom {
static int input;
static boolean quit = false;
public static void main(String[] args) {
CarShowroom Milan = new CarShowroom("Lamborghini", "Murciealago", 230);
Milan.setEngineSize(4);
Milan.setHorsepower(699.99);
Milan.carSpecifications();
do {
String choice;
int theNum;
input = JOptionPane.showInputDialog("Good Ebening!" + "\n\n1. Show Car Specs
\n\n2.Upgrade your car\n\n3.Quit the Program" + "\n\nPlease pick a one:");
theNum = Integer.parseInt(choice);
switch (theNum) {
case 1:
String option = Milan.getBrand();
String option1 = Milan.getType();
int option2 = Milan.getSpeed();
int option4 = Milan.setEngineSize(4);
double option5 = Milan.setHorsepower(699.99);
String output1 = "The brand of your car is: " + option + "\nThe model of your Car is: " + option1 + "\nThe speed of your car is: " + option2 + "\nThe engine size of your car is: " +option4 + "\nThe horsepower of your car is: " +option5;
JOptionPane.showConfirmDialog(null, output1, "Milan Showroom", JOptionPane.DEFAULT_OPTION);
break;
case 2:
case 3:
quit = true;
break;
}
} while (!quit);
JOptionPane.showMessageDialog(null,"You have left the showroom, come back!");
}
}
在Carshowroom类上创建getter方法
int getEnginesize() {
return this.enginesize;
}
double getHorsePower() {
return this.horsepower;
}
并且在设置值之后,从getter方法获取值
Milan.setEngineSize(4);
Milan.setHorsepower(699.99);
int option4 = Milan.getEngineSize();
double option5 = Milan.getHorsePower();