首页 > 解决方案 > 为什么 inc/dec 通道不做任何事情?

问题描述

在下面的代码中,我的测试表明这些方法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());
    }
}

标签: javaclassmethods

解决方案


它应该是这样的:

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++表示先将通道分配给通道编号,然后再增加其值。

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


推荐阅读