你可以重用if语句吗?程序循环只到第一个if循环

时间:2016-07-06 18:53:54

标签: java loops if-statement methods user-input

该程序通过取A侧和B侧的值来计算三角形的斜边。如果程序运行,它为用户提供4种选择,在用户输入A,B之前不会发生任何事情, C或Q.假设用户输入A,它允许用户输入A面的值,它将返回4个主要选项。问题是,当用户输入A,B,C或Q时,它只是循环再次询问A侧。以下是我目前的代码。我怎么做到它不仅仅接受价值A?感谢

public class Newest_Exercise_1 {

     public static void main(String[] args) {    
         Scanner input = new Scanner(System.in);
         char letter;
         int valueOfA;
         int valueOfB;
         double hypotenuse;


         Boolean loop =true;

         letter = GettingUserInput(input);
            System.out.println("value of letter: " +letter);

         while(loop){
         if(letter=='A' || letter =='a'){
                valueOfA = InputWasA();
                System.out.println(valueOfA);
                letter = GettingUserInput(input);


         }
         if(letter=='B' || letter =='b'){
                valueOfB = InputWasB();
                System.out.println(valueOfB);
                letter = GettingUserInput(input);

         }

         if(letter=='C' || letter =='c'){
                System.out.println("made it to C");
                hypotenuse = Math.sqrt((valueOfA*valueOfA)+(valueOfB*valueOfB));
                System.out.println("Hypotenuse is: "+hypotenuse);
                letter = GettingUserInput(input);


         }
         if(letter=='Q' || letter =='q'){
                System.out.println("made it to Q");
                System.out.println("Program Closed");
                System.exit(0);
         }  
         }
     }

     public static char GettingUserInput(Scanner input){


            System.out.println("A-Enter value of side A");
            System.out.println("B-Enter value of side B");
            System.out.println("C-Calculate");
            System.out.println("Q-Quit the program");

            String s = input.next();
            System.out.println("value of s: "+s);
            char letter = s.charAt(0);

            while(letter != 'A' && letter != 'a' && letter != 'B' && letter != 'b' && letter != 'C' && letter != 'c' && letter != 'Q' && letter != 'q'){
                System.out.println("Invalid entry, Please try again");
                System.out.println("A-Enter value of side A");
                System.out.println("B-Enter value of side B");
                System.out.println("C-Calculate");
                System.out.println("Q-Quit the program");
                s = input.next();
                letter = s.charAt(0);
            }


        return letter; 
     }

     public static int InputWasA(){
         Scanner input = new Scanner(System.in);
         Boolean loop1 = true;
         int valueOfA = 0;

         while(loop1){
             try{
                 System.out.println("Enter value of side A");
                 valueOfA=input.nextInt();
                 loop1 = false;
             }
             catch(Exception e){
                 System.out.println("That was not an integer!");
                 input.next();
             }

         }
         return valueOfA;
     }

     public static int InputWasB(){
         Scanner input = new Scanner(System.in);
         Boolean loop2 = true;
         int valueOfB = 0;

         while(loop2){
             try{
                 System.out.println("Enter value of side B");
                 valueOfB=input.nextInt();
                 loop2 = false;
             }
             catch(Exception e){
                 System.out.println("That was not an integer!");
                 input.next();
             }

         }
         return valueOfB;
     }

     public static void InputWasC(){
            System.out.println("Made it to InputWasC!");
     }





}

1 个答案:

答案 0 :(得分:2)

您只执行letter = GettingUserInput(input)一次,因此当然letter将永远是相同的值。

所以:

  • 将此行
  • 中的循环内移动
  • 不要在其他任何地方重复它 - 它现在在每次迭代的顶部运行
  • 并废弃了loop变量,只需使用true
  • 即可

使代码看起来像:

while (true) {
    letter = GettingUserInput(input);
    System.out.println("value of letter: " + letter);

    if (letter=='A' || letter =='a') {
        valueOfA = InputWasA();
        System.out.println(valueOfA);
    }

    // etc

我拒绝提出其他改进建议,只保留导致所述问题的改进,但是......

由于你的循环有:

  • 初始状态(读信)
  • 终止测试(不是“q”)
  • 迭代操作(读另一个字母)

它是最清晰的,最好编码为for循环。另外,为了避免所有这些双重案例测试,GettingUserInput()方法应该只返回小写字母,即它的最后一行应该是:

return Character.toLowerCase(letter); 

鉴于这种变化,我认为你的循环看起来应该是这样的:

for (char letter = GettingUserInput(input); letter != 'q'; letter = GettingUserInput(input)) {
    if (letter =='a') {
        valueOfA = InputWasA();
        System.out.println(valueOfA);
     }

     if (letter =='b') {
         valueOfB = InputWasB();
         System.out.println(valueOfB);
     }

     if (letter =='c') {
         System.out.println("made it to C");
         hypotenuse = Math.sqrt((valueOfA*valueOfA)+(valueOfB*valueOfB));
         System.out.println("Hypotenuse is: "+hypotenuse);
     }
}

System.out.println("Program Closed");

请注意字母q的代码是循环后的任何内容。