2015-09-28 31 views
0

这是学校作业的一部分,所以我知道一些代码有点复杂,但都是必需的。我在我的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!"); 
    } 
+1

你可以尝试把break语句放在if语句之外吗? – dragon66

+0

我已经把break语句移到了if括号之外。但是现在每个if语句中的println命令从不执行。 – bnr32jason

+0

学校任务...耶稣,希望我的学校教授Java .-。 –

回答

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 
+0

好吧,我纠正了这一点,但现在我的System.out.println(“\ n”+ h1);代码不执行,程序就结束了。 – bnr32jason

+0

@ bnr32jason它工作得很好。你使用什么输入? –

+0

在您的原始文章中,我错过了每个右括号后面的分号,或者您忘记了输入它们。无论哪种方式,我加了他们,现在一切正常。谢谢你的帮助! – bnr32jason

0

让人惊讶的例子,你的休息是在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,你会全部设置。

+0

如果不是一个循环,它是一个控制或分支结构,只是fyi。 –