繁体   English   中英

斜边程序,直到用户输入2才弄清楚如何让程序运行

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

我正在编写一个找到三角形斜边的程序,我需要让该程序运行任意次,直到用户输入2。我无法弄清楚当用户输入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的答案和Elliot Frisch的答案/评论只是解决方案的一半。

另一半是,您需要围绕大多数逻辑的外部循环,以便重复执行。 main()大部分放入使用while (true) {的循环中,以使其永远循环。

然后,使用if (answer == 2) { ...的逻辑在用户输入2时实际中断。

几种选择:

if (answer == 2)
{
break;
}

if (answer == 2)
{
return;
}

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

所以我想通了。 您的答案有很大帮助,但我最后放了两个while循环。 代码如下:

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();
 }

}公共静态双斜边(double s1,double s2){//计算斜边的方法

    double hypot;

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

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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