有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java:Switch语句重复/默认奇数行为

这是学校作业的一部分,所以我知道有些代码有点复杂,但都是必需的。我的Switch语句有一个奇怪的问题,其他的一切都正常工作,所以我不担心代码的其余部分

如果我选择耳机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!");
   }

共 (2) 个答案

  1. # 1 楼答案

    哎呀,你的休息时间在if循环内

    对于所有这些,应该在if语句之后移动break。下面是案例3的情况:

        case 3:
            System.out.print(volMessage);
            volSelect = input.nextInt();
            if (volSelect == 3){
                h1.changeVolume(HIGH);
                System.out.println("\n" + h3);
    
            }
        break;
    

    在默认情况下,案例1和案例2都这样做之后,一切都将就绪

  2. # 2 楼答案

    你的break位置不对

    你的break应该总是在case的末尾

    以下是代码的更正部分:

    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
    

    下面是一个输出示例

    Which headphone set would you like to change the volume of? 1, 2, or 3? 1
    What volume would you like to set? 1 for LOW, 2 for MEDIUM, or 3 for HIGH. 1
    
    Volume: 1
    Plugged In: true
    Manufacturer: Logitech
    Color: Black