简体   繁体   中英

Hypotenuse program, can't figure out how to let program run until user enters 2

I am writing a program that finds the hypotenuse of a triangle, I need to let the program run an arbitrary amount of times until the user enters 2. I cannot figure out how to end the program when the user enters 2.

package assignment5a;

import java.util.Scanner;//import Scanner 

public class Assignment5A {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);//new Scanner variable
        int answer;
        double side1, side2, result;


        System.out.println("Enter 1 to calculate the hypotenuse of a triangle or enter 2 to quit.");
        answer = sc.nextInt();

        while(answer < 0 || answer > 2){

            System.err.println("Please enter a valid answer.");

            System.out.println("Enter 1 to calculate the hypotenuse of a triangle or enter 2 to quit.");
            answer = sc.nextInt();


        }

        System.out.println("Enter side 1 of the triangle :");//input for side 1
        side1 = sc.nextDouble();

        System.out.println("Enter side 2 of the triangle :");//input for side 2
        side2 = sc.nextDouble();

        result = hypotenuse(side1, side2);//declares result as the result of the method hypotenuse

        System.out.printf("Hypotenuse of your triangle is: %.2f%n", result);//prints results




    }

    public static double hypotenuse(double s1, double s2){//method for calculating hypotenuse

        double hypot;

        hypot = Math.sqrt((Math.pow(s1, 2) + Math.pow(s2, 2)));
        return hypot;
    }
}

Wilmol's answer and Elliot Frisch's answer / comment are half the solution.

The other half is that you need an outer loop around most of the logic so it'll repeat. Put most of main() inside a loop that uses while (true) { to start so that it'll loop forever.

Then use the logic of if (answer == 2) { ... to actually break out when the user inputs 2.

Few options:

if (answer == 2)
{
break;
}

if (answer == 2)
{
return;
}

if (answer == 2)
{
System.exit(0);
}

So I figured it out. Your answers helped a lot but i ended up putting two while loops. The code is below:

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);//new Scanner variable
    int answer;
    double side1, side2, result;


    System.out.println("Enter 1 to calculate the hypotenuse of a triangle or enter 2 to quit.");
    answer = sc.nextInt();

    while(answer < 0 || answer > 2){

        System.err.println("Please enter a valid answer.");

        System.out.println("Enter 1 to calculate the hypotenuse of a triangle or enter 2 to quit.");
        answer = sc.nextInt();
    }  
        while(answer == 1){

    System.out.println("Enter side 1 of the triangle :");//input for side 1
    side1 = sc.nextDouble();

    System.out.println("Enter side 2 of the triangle :");//input for side 2
    side2 = sc.nextDouble();

    result = hypotenuse(side1, side2);//declares result as the result of the method hypotenuse

    System.out.printf("Hypotenuse of your triangle is: %.2f%n", result);//prints results

    System.out.println("Enter 1 to calculate the hypotenuse of a triangle or enter 2 to quit.");
    answer = sc.nextInt();
 }

} public static double hypotenuse(double s1, double s2){//method for calculating hypotenuse

    double hypot;

    hypot = Math.sqrt((Math.pow(s1, 2) + Math.pow(s2, 2)));
    return hypot;
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM