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

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

如何在方法之間傳遞參數并使用Java正確調用方法?

如何在方法之間傳遞參數并使用Java正確調用方法?

皈依舞 2021-12-22 20:21:10
該程序應執行以下操作:編寫一個名為 getheartRate 的方法,它不接受任何參數并返回一個 int (heartRate)。此方法提示用戶輸入患者的心率,從命令行讀取他們的輸入,并返回此值。編寫一個名為 checkHeartRate 的方法,它接受一個 int 參數(心率)并返回一個字符串(結果)。如果心率高于 100,則返回值“High”。如果心率低于 60,則返回值“低”。否則返回“正?!?。編寫一個名為 printHRResult 的方法,它接受一個 String 參數,它是方法 checkHeartRate 的結果。將此值打印到命令行。使用適當的參數傳遞從主方法調用所有三個方法。到目前為止,我有:public class UnitSixInClassFall2018 {    /**     * @param args the command line arguments     */    public static void main(String[] args) {        UnitSixInClassFall2018 u = new UnitSixInClassFall2018();        u.getHeartRate();        System.out.println();        Scanner scan = new Scanner(System.in);        u.checkHeartRate(0);        // END MAIN    }    public int getHeartRate(){        System.out.print("Please enter your heart rate: ");        Scanner scan = new Scanner(System.in);        int heartRate = scan.nextInt();        return 0;    }    public void checkHeartRate(int heartRate){        if (heartRate > 100) {           String result = "High";        }        else if (heartRate < 60) {           String result = "Low";        }        else {           String result = "Normal";        }    }    public String printHRResults(String result) {         System.out.print("Your hear rate is " + result + ".");       return result;     }}運行時,輸出的只是“請輸入您的心率:”。一旦我輸入一個整數,程序就結束了。做錯了什么?
查看完整描述

2 回答

?
慕標5832272

TA貢獻1966條經驗 獲得超4個贊

您應該更改此方法以返回這樣的心率:


public int getHeartRate(){

    System.out.print("Please enter your heart rate: ");

    Scanner scan = new Scanner(System.in);

    int heartRate = scan.nextInt();

    // Also add this

    scan.close();

    return heartRate;

}

并更改此方法以返回結果:


public String checkHeartRate(int heartRate){

    if (heartRate > 100) {

       return "High";

    }

    else if (heartRate < 60) {

       return "Low";

    }

    else {

       return "Normal";

    }


}

然后在你的主要方法中:


// get the heart rate

int heartRate = u.getHeartRate();

// Call the checkHeartRate method

String result = checkHeartRate(heartRate);

// call the printHRResults 

printHRResults(result);

那應該可以解決您的問題。


查看完整回答
反對 回復 2021-12-22
?
慕田峪7331174

TA貢獻1828條經驗 獲得超13個贊

首先,您要創建兩個Scanner具有相同輸入類型 ( System.in) 的對象,這是不推薦的。相反,只需創建一個Scanner對象并在程序中的任何地方使用它。這篇文章有一些很好的信息。


改進了Scanner對象使用的代碼的改進版本如下:


public UnitSixInClassFall2018 {

    private static final Scanner scan = new Scanner(System.in);


    public static void main(String[] args) {

        NewMain u = new NewMain();

        int heartRate = u.getHeartRate();

        System.out.println();

        String result = u.checkHeartRate(heartRate);

        u.printHRResults(result);

        scan.close(); // Close the scanner once you are completely done using it

    }


    public int getHeartRate() {

        System.out.print("Please enter your heart rate: ");

        return scan.nextInt(); // Return the input of the user

    }


    public String checkHeartRate(int heartRate) {

        String result = ""; // Initialize some default value

        if (heartRate > 100) {

           result = "High";

        }

        else if (heartRate < 60) {

           result = "Low";

        }

        else {

           result = "Normal";

        }

        return result; // Return the result calculated from the if-statements

    }


    public void printHRResults(String result) {

       System.out.print("Your heart rate is " + result + ".");

       // Originally this method returned a string but that seems unnecessary

    }

}


查看完整回答
反對 回復 2021-12-22
  • 2 回答
  • 0 關注
  • 156 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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