如何在方法之间传递参数并使用Java正确调用方法?

该程序应执行以下操作:


编写一个名为 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; 

    }


}

运行时,输出的只是“请输入您的心率:”。一旦我输入一个整数,程序就结束了。做错了什么?


皈依舞
浏览 161回答 2
2回答

慕标5832272

您应该更改此方法以返回这样的心率:public int getHeartRate(){&nbsp; &nbsp; System.out.print("Please enter your heart rate: ");&nbsp; &nbsp; Scanner scan = new Scanner(System.in);&nbsp; &nbsp; int heartRate = scan.nextInt();&nbsp; &nbsp; // Also add this&nbsp; &nbsp; scan.close();&nbsp; &nbsp; return heartRate;}并更改此方法以返回结果:public String checkHeartRate(int heartRate){&nbsp; &nbsp; if (heartRate > 100) {&nbsp; &nbsp; &nbsp; &nbsp;return "High";&nbsp; &nbsp; }&nbsp; &nbsp; else if (heartRate < 60) {&nbsp; &nbsp; &nbsp; &nbsp;return "Low";&nbsp; &nbsp; }&nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp;return "Normal";&nbsp; &nbsp; }}然后在你的主要方法中:// get the heart rateint heartRate = u.getHeartRate();// Call the checkHeartRate methodString result = checkHeartRate(heartRate);// call the printHRResults&nbsp;printHRResults(result);那应该可以解决您的问题。

慕田峪7331174

首先,您要创建两个Scanner具有相同输入类型 ( System.in) 的对象,这是不推荐的。相反,只需创建一个Scanner对象并在程序中的任何地方使用它。这篇文章有一些很好的信息。改进了Scanner对象使用的代码的改进版本如下:public UnitSixInClassFall2018 {&nbsp; &nbsp; private static final Scanner scan = new Scanner(System.in);&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; NewMain u = new NewMain();&nbsp; &nbsp; &nbsp; &nbsp; int heartRate = u.getHeartRate();&nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; &nbsp; &nbsp; String result = u.checkHeartRate(heartRate);&nbsp; &nbsp; &nbsp; &nbsp; u.printHRResults(result);&nbsp; &nbsp; &nbsp; &nbsp; scan.close(); // Close the scanner once you are completely done using it&nbsp; &nbsp; }&nbsp; &nbsp; public int getHeartRate() {&nbsp; &nbsp; &nbsp; &nbsp; System.out.print("Please enter your heart rate: ");&nbsp; &nbsp; &nbsp; &nbsp; return scan.nextInt(); // Return the input of the user&nbsp; &nbsp; }&nbsp; &nbsp; public String checkHeartRate(int heartRate) {&nbsp; &nbsp; &nbsp; &nbsp; String result = ""; // Initialize some default value&nbsp; &nbsp; &nbsp; &nbsp; if (heartRate > 100) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result = "High";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else if (heartRate < 60) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result = "Low";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result = "Normal";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return result; // Return the result calculated from the if-statements&nbsp; &nbsp; }&nbsp; &nbsp; public void printHRResults(String result) {&nbsp; &nbsp; &nbsp; &nbsp;System.out.print("Your heart rate is " + result + ".");&nbsp; &nbsp; &nbsp; &nbsp;// Originally this method returned a string but that seems unnecessary&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java