亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

斜邊程序,在用戶輸入 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 關注
  • 101 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號