java - 为什么 inc/dec channel 不执行任何操作?

标签 java class methods

在下面的代码中,我的测试表明方法 tvObject.incChannel(...)tvObject.decChannel(...) 不会改变任何内容我的代码。为什么?

package lab2;

public class TV {

    private int channelNumber;
    private int volumeLevel;
    private String status = "ON";

    public TV(int channel, int vol, String stat) {

        status = stat;
        setChannelNumber(channel);
        setVolumeLevel(vol);
    }
    public void setChannelNumber(int channel) {
        if (status.equalsIgnoreCase("ON")) {
            channelNumber = channel;
        } else if (status.equalsIgnoreCase("OFF")) {
            System.out.println("Error TV is off");
        }   
    }
    public int getChannelNumber() {
        return channelNumber;
    }
    public void setVolumeLevel(int vol) {
        if (status.equalsIgnoreCase("ON")) {
            volumeLevel = ((vol >= 0 && vol <= 100)?vol:0);
        } else if (status.equalsIgnoreCase("OFF")) {
            System.out.println("Error TV is off");
        }       
        volumeLevel = ((vol >= 0 && vol <= 100)?vol:0);
    }
    public int getVolumeLevel() {
            return volumeLevel; 
    }
    public void incVol(int vol) {
        if (status.equalsIgnoreCase("ON")) {
            volumeLevel = ((vol >= 0 && vol <= 98)?vol += 2:volumeLevel);
        } else if (status.equalsIgnoreCase("OFF")) {
            System.out.println("Error TV is off");
        }   
    }
    public void decVol(int vol) {
        if (status.equalsIgnoreCase("ON")) {
            volumeLevel = ((vol >= 2 && vol <= 100)?vol -= 2:volumeLevel);
        } else if (status.equalsIgnoreCase("OFF")) {
            System.out.println("Error TV is off");
        }           
    }

    // does not do anything
    public void incChannel(int channel) {
        if (status.equalsIgnoreCase("ON")) {
            channelNumber = channel++;
        } else if (status.equalsIgnoreCase("OFF")) {
            System.out.println("Error TV is off");
        }       
    }

    // does not do anything
    public void decChannel(int channel) {
        if (status.equalsIgnoreCase("ON")) {
            channelNumber = channel--;
        } else if (status.equalsIgnoreCase("OFF")) {
            System.out.println("Error TV is off");
        }       
    }
    public void Toggle() {
        //String status = "ON";  //default on

        if (status.equalsIgnoreCase("ON")) {
            status = "OFF";
        } else if (status.equalsIgnoreCase("OFF")) {
            status = "ON";
        }       
    }
}


// code to test

package lab2;

public class Test {

    public static void main(String[] args) {

            TV tvObject = new TV(53, 35, "ON");
            //TV tvObject2 = new TV(3, -10, "on");
            //TV tvObject3 = new TV(3, -10, "OFF");

            tvObject.Toggle();

            tvObject.setChannelNumber(53);
            tvObject.incChannel(53);

            tvObject.setVolumeLevel(35);
            tvObject.decVol(35);

            System.out.println(tvObject.getChannelNumber());
            System.out.println(tvObject.getVolumeLevel());
    }
}

最佳答案

It should be like this:

public void incChannel(int channel) {
        if (status.equalsIgnoreCase("ON")) {
            channelNumber = ++channel; // use either this
            // channelNumber = channel+1; // or this
        } else if (status.equalsIgnoreCase("OFF")) {
            System.out.println("Error TV is off");
        }       
    }

public void decChannel(int channel) {
        if (status.equalsIgnoreCase("ON")) {
            channelNumber = --channel; // use either this
            // channelNumber = channel-1; // or this
        } else if (status.equalsIgnoreCase("OFF")) {
            System.out.println("Error TV is off");
        }       
    }

channelNumber = channel++表示先将channel分配给channelNumber,然后再增加其值。

channelNumber =++channel表示先增加channel值,然后赋值给cannelNumber。

关于java - 为什么 inc/dec channel 不执行任何操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60118074/

相关文章:

c++ - C++ 中以 Listclass-parent 作为成员的 Itemclass

java - 如何避免像 "This method must return a result of type int"这样的错误?

java - Java 是 "pass-by-reference"还是 "pass-by-value"?

java - 一种在数组中的两个索引之间交换位置的方法

java - java控制台程序出错

c++ - 访问来自不同类的对象

Java判断arraylist是否包含字符串

c++ - 我在这里做错了什么?使用指向函数 typedef 的指针定义类。

java.lang.ClassNotFoundException : org. mariadb.jdbc.Driver

java - 找不到合适的屏幕尺寸