这是学校作业的一部分,所以我知道一些代码有点复杂,但都是必需的。我在我的Switch语句中遇到了一个奇怪的问题,但其他所有工作都正常,但我并不担心其他代码。Java:切换语句重复/违约奇数行为
如果我选择耳机1并输入音量1,则按预期工作。如果我选择耳机1并输入音量2,它会提出问题两次,然后工作。如果我选择耳机1并输入音量3,则重复三次。如果我选择耳机2并输入音量1,它会问我两次,然后进入默认情况。奇怪的行为也持续存在。
我猜这是显而易见的东西,我错过了,但我已经排除了一段时间,无法弄清楚发生了什么。我会很感激任何指导或代码更正。谢谢!
import java.util.Scanner;
public class HeadPhone {
//Three constants to denote headphone volume
public static final int LOW = 1;
public static final int MEDIUM = 2;
public static final int HIGH = 3;
//Private variables
private static int volume;
private boolean pluggedIn;
private String manufacturer;
private String headPhoneColor;
//This constructs the default headphone object
HeadPhone(){
volume = MEDIUM;
pluggedIn = false;
manufacturer = "DEFAULT";
headPhoneColor = "DEFAULT";
}
public void setVolume(int v){
if(v < LOW){
volume = LOW;
}
else if(v > HIGH){
volume = HIGH;
}
else{
volume = v;
}
}
public void setPluggedIn(boolean p){
pluggedIn = p;
}
public void setManufacturer(String m){
manufacturer = m;
}
public void setColor(String C){
headPhoneColor = C;
}
public int getVolume(){
return volume;
}
public boolean getPluggedIn(){
return this.pluggedIn;
}
public String getManufacturer(){
return this.manufacturer;
}
public String getColor(){
return this.headPhoneColor;
}
public void changeVolume(int volume){
setVolume(volume);
}
public String toString(){
String temp = "Volume: " + volume + "\nPlugged In: " + pluggedIn +
"\nManufacturer: " + manufacturer + "\nColor: " + headPhoneColor + "\n";
return temp;
}
public static void main(String args[]){
String volMessage = "What volume would you like to set? 1 for LOW, 2 for MEDIUM, or 3 for HIGH. ";
//Creates the object h1 and displays its statistics
HeadPhone h1 = new HeadPhone();
h1.setVolume(LOW);
h1.setManufacturer("Logitech");
h1.setColor("Black");
h1.setPluggedIn(true);
h1.changeVolume(MEDIUM);
System.out.println("Headphone Set #1\n" + h1);
//Creates the object h2 and displays its statistics
HeadPhone h2 = new HeadPhone();
h2.setVolume(LOW);
h2.setManufacturer("Steel Series");
h2.setColor("Silver");
h2.setPluggedIn(true);
h2.changeVolume(HIGH);
System.out.println("Headphone Set #2\n" + h2);
//Creates the object h3 and displays its statistics
HeadPhone h3 = new HeadPhone();
h3.setVolume(LOW);
h3.setManufacturer("Panasonic");
h3.setColor("Red");
h3.setPluggedIn(true);
h3.changeVolume(LOW);
System.out.println("Headphone Set #3\n" + h3);
Scanner input = new Scanner(System.in);
//This logic allows the user to either select to change the volume on their headphones or not
System.out.print("\n\nWould you like to change the volume on your headphones? Y or N? ");
String choice = input.nextLine();
if (choice.equals("Y") || choice.equals("y")){
System.out.print("\nWhich headphone set would you like to change the volume of? 1, 2, or 3? ");
int hpSelect = input.nextInt();
//This switch statement allows the user to change the volume of their selected headphone set
switch(hpSelect){
case 1:
System.out.print(volMessage);
int volSelect = input.nextInt();
if (volSelect == 1){
h1.changeVolume(LOW);
System.out.println("\n" + h1);
break;
}
case 2:
System.out.print(volMessage);
volSelect = input.nextInt();
if (volSelect == 2){
h1.changeVolume(MEDIUM);
System.out.println("\n" + h2);
break;
}
case 3:
System.out.print(volMessage);
volSelect = input.nextInt();
if (volSelect == 3){
h1.changeVolume(HIGH);
System.out.println("\n" + h3);
break;
}
default:
System.out.println("Invalid entry.");
break;
}//End switch statement
input.close();
}//End if statement
else{
System.out.println("Okay! Enjoy your music!");
}
你可以尝试把break语句放在if语句之外吗? – dragon66
我已经把break语句移到了if括号之外。但是现在每个if语句中的println命令从不执行。 – bnr32jason
学校任务...耶稣,希望我的学校教授Java .-。 –