If-then-else语句不起作用

Zwi*_*bel 0 java

我有一个主类,看起来像这样:

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();
        }
}
Run Code Online (Sandbox Code Playgroud)

我有这个美孚课程:

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!");
    }
}
Run Code Online (Sandbox Code Playgroud)

但是当我运行程序并调用它时one.touchcontrol();,two.touchcontrol();它表明没有模型有触摸屏.我不知道我错过了什么.

Bal*_*usC 7

您需要在构造函数中交换变量赋值.

manufactureat = manufacturer;
type = inittype;
modellnumber = number;
touchtype = touch;
Run Code Online (Sandbox Code Playgroud)

在Java(以及几乎所有其他语言)中的变量赋值中,左手将检索右手的值.

也可以看看:

  • @Mark我更喜欢`this`而不是前缀/后缀这个名字.但那只会在那时混淆OP.让他先熟悉语言基础:) (2认同)