0

I am writing a code where the user is trying to find the hypotenuse of a Triangle. My code starts by asking the user which side would they like to start with side A or side B. I used the Switch method to cover both case. what i want my code to do after either case is used, is to go back to the starting point, until the user quits. For some reason my code only does one case but doesn't go back to the beginning to start a whole new loop. Am i using the wrong method? Did I write the code improperly? What can i do to fix it?

import java.util.Scanner;

public class HandleException {
public static void main (String[] args){
    int data= 0;
    int data1= 0;
    int sum= 0;

    Scanner input = new Scanner (System.in);
    //ask user for a command
    System.out.println("Which side would you like to start with A or B");
    String s= input.next();
            char letter= s.charAt(0);

                loop:while (letter!= 'q'){      


    switch (letter){

    //if user inputs 'A'
        case 'a':

            System.out.println("Enter value for side A: ");
            data= input.nextInt();
                System.out.println("Enter next command: A : value for side A; B : Value for side B; C: Calculate; Q : Quit program.");
                s=input.next();
                letter=s.charAt(0);
                    if (letter == 'b')
                    System.out.println("Enter Value For side B: ");
                    data1= input.nextInt();
                        System.out.println("Enter next command: A : value for side A; B : Value for side B; C: Calculate; Q : Quit program.");
                        s=input.next();
                        letter=s.charAt(0);
                            if (letter== 'c')
                                sum=data*data+data1*data1;
                                int sumsrt = (int) Math.sqrt(sum);
                                System.out.println("The hypotenuse is:  "+sumsrt);
                                continue;

        //if user inputs 'B'                        
        case 'b':
            System.out.println("Enter value for side B: ");
            data= input.nextInt();
                System.out.println("Enter next command: A : value for side A; B : Value for side B; C: Calculate; Q : Quit program.");
                s=input.next();
                letter=s.charAt(0);
                    if (letter == 'a')
                    System.out.println("Enter Value For side a: ");
                    data1= input.nextInt();
                        System.out.println("Enter next command: A : value for side A; B : Value for side B; C: Calculate; Q : Quit program.");
                        s=input.next();
                        letter=s.charAt(0);
                            if (letter== 'c')
                                sum=data*data+data1*data1;
                                int sumsrt1 = (int) Math.sqrt(sum);
                                System.out.println("The hypotenuse is:  "+sumsrt1); 
                                continue;




    }

}
}
}
J.Doe
  • 11
  • 3

3 Answers3

0

After first time of loop is end, it goes to the line while(letter!= 'q') and letter value is c

there is no match value in switch, so second time of loop will end and do nothing.

it will keep looping, so you won't have a chance to ask next System.in

TomN
  • 574
  • 3
  • 16
0

You are not asking for quit char once done with hypotenuse calculation. Thats why program going in infinite loop not quitting Also you use break once done.

if (letter == 'b')
                    System.out.println("Enter Value For side B: ");
                    data1= input.nextInt();
                        System.out.println("Enter next command: A : value for side A; B : Value for side B; C: Calculate; Q : Quit program.");
                        s=input.next();
                        letter=s.charAt(0);
                            if (letter== 'c')
                                sum=data*data+data1*data1;
                                int sumsrt = (int) Math.sqrt(sum);
                                System.out.println("The hypotenuse is:  "+sumsrt);
                    System.out.println("Press q for quit otherwise enter side ");

                      break;

similarly do it for case 'b' also

M Sach
  • 30,322
  • 72
  • 198
  • 300
0

Read input again after the switch cases. Try this

import java.util.Scanner;

public class HandleException {
    public static void main(String[] args) {
        int data = 0;
        int data1 = 0;
        int sum = 0;

        Scanner input = new Scanner(System.in);
        // ask user for a command
        System.out.println("Which side would you like to start with A or B");
        String s = input.next();
        char letter = s.charAt(0);

        while (letter != 'q') {

            switch (letter) {

            // if user inputs 'A'
            case 'a':

                System.out.println("Enter value for side A: ");
                data = input.nextInt();
                System.out.println("Enter next command: A : value for side A; B : Value for side B; C: Calculate; Q : Quit program.");
                s = input.next();
                letter = s.charAt(0);
                if (letter == 'b')
                    System.out.println("Enter Value For side B: ");
                data1 = input.nextInt();
                System.out.println("Enter next command: A : value for side A; B : Value for side B; C: Calculate; Q : Quit program.");
                s = input.next();
                letter = s.charAt(0);
                if (letter == 'c')
                    sum = data * data + data1 * data1;
                int sumsrt = (int) Math.sqrt(sum);
                System.out.println("The hypotenuse is:  " + sumsrt);
                break;

                // if user inputs 'B'
            case 'b':
                System.out.println("Enter value for side B: ");
                data = input.nextInt();
                System.out.println("Enter next command: A : value for side A; B : Value for side B; C: Calculate; Q : Quit program.");
                s = input.next();
                letter = s.charAt(0);
                if (letter == 'a')
                    System.out.println("Enter Value For side a: ");
                data1 = input.nextInt();
                System.out.println("Enter next command: A : value for side A; B : Value for side B; C: Calculate; Q : Quit program.");
                s = input.next();
                letter = s.charAt(0);
                if (letter == 'c')
                    sum = data * data + data1 * data1;
                int sumsrt1 = (int) Math.sqrt(sum);
                System.out.println("The hypotenuse is:  " + sumsrt1);
                break;

            }

            System.out.println("Which side would you like to start with A or B");
            s = input.next();
            letter = s.charAt(0);

        }
    }
}
Ravindra Devadiga
  • 708
  • 1
  • 6
  • 14