private int channel;
private int volume;
private boolean flag;
//no-arg constructor
public TV(){}
//default constructor
public TV(int channel,int volume,boolean flag){
this.channel=channel;
this.volume=volume;
this.flag=flag;
}
public int getChannel(){
return channel;
}
public int getVolume(){
return volume;
}
public boolean isOn(){
return flag;
}
public void setChannel(int channel){
this.channel=channel;
}
public void setVolume(int volume){
this.volume=volume;
}
public void volumeUp(){
this.volume+=1;
}
public void volumeDown(){
this.volume-=1;
}
public void setFlag(boolean flag){
this.flag=flag;
}
public String toString(){
return ((flag) ? "TV is on" : "TV is off") +
"\nChannel is " + channel + "\nVolume is "
+ volume;
}
}
import java.util.Scanner;
public class testTV{
public static void main(String[] args){
//create object
TV tv = new TV();
Scanner input = new Scanner(System.in);
System.out.print("do you want to switch the power ? On = 1 or Off = 0: ");
//powerOnOff = input.nextBoolean();
int power = input.nextInt();
if (power == 1)
tv.setFlag(true);
else if(power == 0){
System.out.println("The tv is off ");
//tv.setPowerOnOff(false);
//else
System.exit(0);
}
System.out.print("Enter the TV channel between 0 to 99: ");
int channel = input.nextInt();
if (channel >=0 && channel <=99)
tv.setChannel(channel);
else
System.exit(0);
System.out.print("Enter the TV volume between 0 to 20: ");
int volume = input.nextInt();
if (volume >=0 && volume <=99)
tv.setVolume(volume);
else
System.exit(0);
// System.out.print("Pls increased the volume by unit: ");
// volume = input.nextInt();
// tv.setVolumeUp(volume);
// System.out.print("Pls decreased the volume by unit: ");
// volume = input.nextInt();
// tv.setVolumeDown(volume);
//tv.setPowerOnOff(powerOnOff);
tv.setChannel(channel);
tv.setVolume(volume);
tv.volumeUp();
tv.volumeDown();
System.out.println(tv);
System.out.println("\nThe channel's TV is: " + tv.getChannel());
System.out.println("\nThe volume is : " + tv.getVolume());
System.out.println("\nWhen the volume increased by 1 \n" + tv);
System.out.println("\nWhen the volume decreased by 1 \n" + tv);
}
}
为么 VOLUME dint increase & decrease!!!哪出了问题