运行代码时,我的音量变量没有更改。 我尝试更改代码和新变量名称的顺序,但输出未更改。 changeVolume()函数没有执行它应该做的事情。
请帮助我弄清楚为什么它不起作用。贝娄是我的代码和运行它的屏幕截图:
import java.awt.Color;
public class HeadPhone{
//Three constants
public static final int LOW = 1;
public static final int MEDIUM = 2;
public static final int HIGH = 3;
//a private int data filed for volume
private int volume;
//and a pluggedIn value
private boolean pluggedIn;
//A private String data field for manufacturer
private String manufacturer;
//A private Color data field
private Color headPhoneColor;
//A private String field
private String headPhoneModel;
//setters and getters for the declared values
public void setVolume(int volume){
this.volume = volume;
}
public int getVolume(){
return volume;
}
public void setPluggedIn(boolean pluggedIn){
this.pluggedIn = pluggedIn;
}
public boolean getPluggedIn(){
return pluggedIn;
}
public void setManufacturer(String manufacturer){
this.manufacturer = manufacturer;
}
public void setHeadPhoneColor(Color headPhoneColor){
this.headPhoneColor = headPhoneColor;
}
public Color getHeadPhoneColor(){
return headPhoneColor;
}
public void setHeadPhoneModel(String headPhoneModel){
this.headPhoneModel = headPhoneModel;
}
public String getHeadPhoneModel(){
return headPhoneModel;
}
//A no argument default constructor
public HeadPhone(){
volume = MEDIUM;
pluggedIn = false;
manufacturer = "Apple";
headPhoneColor = Color.WHITE;
headPhoneModel = "Mic-Enabled";
}
//toString method that returns
//the current set of headphones
public String toString() {
String headPhoneColorString = "";
if (headPhoneColor == Color.RED){
headPhoneColorString = "Red";
}
if (headPhoneColor == Color.BLUE){
headPhoneColorString = "Blue";
}
if (headPhoneColor == Color.BLACK){
headPhoneColorString = "Black";
}
if (headPhoneColor == Color.CYAN){
headPhoneColorString = "Cyan";
}
if (headPhoneColor == Color.YELLOW){
headPhoneColorString = "Yellow";
}
if (headPhoneColor == Color.WHITE){
headPhoneColorString = "White";
}
String pluggedUnplugged = (pluggedIn == true) ? "plugged in" : "unplugged";
return "\n\n Volume Set To: " + volume +
"\n Headphone set is: " + pluggedUnplugged +
"\n Manufacturer: " + manufacturer +
"\n Headphone Color: " + headPhoneColorString +
"\n Headphone Model: " + headPhoneModel;
}
//a changeVolume method to adjust headphones volume
public void changeVolume(int volume) {
if (volume <= LOW){
volume = LOW;
}else if (volume >= HIGH){
volume = HIGH;
}else{
volume = MEDIUM;
}
}
//main class
public static void main(String[] args) {
HeadPhone defaultHeadphoneSet= new HeadPhone();
System.out.println(defaultHeadphoneSet);
HeadPhone set1 = new HeadPhone();
set1.changeVolume(HIGH);
set1.setPluggedIn(true);
set1.setManufacturer("Beats");
set1.setHeadPhoneColor(Color.RED);
set1.setHeadPhoneModel("Wireless");
System.out.println(set1);
HeadPhone set2 = new HeadPhone();
set2.changeVolume(HIGH);
set2.setPluggedIn(true);
set2.setManufacturer("BOSS");
set2.setHeadPhoneColor(Color.BLACK);
set2.setHeadPhoneModel("Noisecancelling");
System.out.println(set2);
HeadPhone set3 = new HeadPhone();
set3.changeVolume(1);
set3.setPluggedIn(false);
set3.setManufacturer("SkullCandy");
set3.setHeadPhoneColor(Color.CYAN);
set3.setHeadPhoneModel("Taptic");
System.out.println(set3);
}
}