我有一个主类,如下所示:
class Main {
public static void main (String[] args) {
Mobil one = new Mobil ("xxxxxx", "yyyyyy", 00000001, true);
Mobil two = new Mobil ("yyyyyy", "xxxxxx", 10245624, false);
one.touchcontrol();
two.touchcontrol();
}
}
我有这个 Mobil 类:
class Mobil {
String type;
String manufactureat;
int modellnumber;
boolean touchtype;
public Mobil (String manufacturer, String inittype, int number, boolean touch) {
manufacturer = manufactureat;
inittype = type;
number = modellnumber;
touch = touchtype;
}
public void touchcontrol() {
if (touchtype == false)
{
System.out.println("This model, has not got Touchscreen!");
}
else
{
System.out.println("This model, has Touchscreen!");
}
}
但是当我运行程序并调用 one.touchcontrol();
和 two.touchcontrol();
时,它显示没有模型具有触摸屏。我不知道我错过了什么。
最佳答案
您需要在构造函数中交换变量赋值。
manufactureat = manufacturer;
type = inittype;
modellnumber = number;
touchtype = touch;
在 Java 中(以及几乎所有其他语言)的变量赋值中,左手将检索右手的值。
另请参阅:
关于java - if-then-else 语句不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5606938/