为了账号安全,请及时绑定邮箱和手机立即绑定

斜边程序,在用户输入 2 之前无法弄清楚如何让程序运行

斜边程序,在用户输入 2 之前无法弄清楚如何让程序运行

开满天机 2022-09-07 15:27:14
我正在编写一个查找三角形斜边的程序,我需要让程序运行任意次数,直到用户进入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;    }}
查看完整描述

3 回答

?
阿波罗的战车

TA贡献1862条经验 获得超6个赞

Wilmol的答案和Elliot Frisch的答案/评论是一半的解决方案。

另一半是,你需要围绕大多数逻辑的外部循环,这样它就会重复。将大部分内容放在用于启动的循环中,以便它永远循环。main()while (true) {

然后使用逻辑...在用户输入 2 时实际突破。if (answer == 2) {


查看完整回答
反对 回复 2022-09-07
?
慕的地6264312

TA贡献1817条经验 获得超6个赞

所以我想通了。你的答案帮助很大,但我最终放了两个同时循环。代码如下:


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

 }

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


    double hypot;


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

    return hypot;

}

}


查看完整回答
反对 回复 2022-09-07
?
DIEA

TA贡献1820条经验 获得超2个赞

几个选项:


if (answer == 2)

{

break;

}


if (answer == 2)

{

return;

}


if (answer == 2)

{

System.exit(0);

}


查看完整回答
反对 回复 2022-09-07
  • 3 回答
  • 0 关注
  • 78 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信