繁体   English   中英

如何将自己的方法的操作实施到main方法中?

[英]How can I implement the actions of my own method into the main method?

您好,我必须创建一个自定义项目,该项目由一个人的平均血压和体重决定年龄。 我还没有完成,但是我的问题是这个。 如何在main方法中应用getBloodPressure中的代码? 我需要自己制作至少四个自定义方法,因此不能只在main方法中使用该代码。 提前致谢!

/*
 * This program will determine the user's age by asking it general health information.
 */

package choice.assignment;


import java.util.Scanner;
import javax.swing.JOptionPane;

/*
 * @author talhaiqbal18
 */

public class ChoiceAssignment {

    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        System.out.println("Welcome! This program will determine your age asking you to input general health information about yourself");
        System.out.println("");
        new ChoiceAssignment().getBloodPressure();
    }

    private void getBloodPressure(int s, int d) {
        Scanner reader = new Scanner(System.in);
        s = reader.nextInt();
        d = reader.nextInt();
        if (s >= 75 && s <= 100 && d >= 50 && d <= 75) {
            System.out.println("You are between the ages of 1 - 12 months. That makes me wonder...How are you using the computer?");
        }
        else if (s >= 75 && s <= 100 && d >= 50 && d <= 75) {
            System.out.println("You are between the ages of 1 - 12 months. That makes me wonder...How are you using the computer?");
        }
    }

    public void getWeight(int w) {
        Scanner reader = new Scanner(System.in);
        w = reader.nextInt();
    }
}

像这样从getBloodPressure删除那些参数,然后就可以调用method了。

 public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        System.out.println("Welcome! This program will determine your age asking you to input general health information about yourself");
        System.out.println("");
        new ChoiceAssignment().getBloodPressure();
    }

    private void getBloodPressure() {
        Scanner reader = new Scanner(System.in);
        int s = reader.nextInt();
        int d = reader.nextInt();
        if (s >= 75 && s <= 100 && d >= 50 && d <= 75) {
            System.out.println("You are between the ages of 1 - 12 months. That makes me wonder...How are you using the computer?");
        }
        else if (s >= 75 && s <= 100 && d >= 50 && d <= 75) {
            System.out.println("You are between the ages of 1 - 12 months. That makes me wonder...How are you using the computer?");
        }
    }

    public void getWeight(int w) {
        Scanner reader = new Scanner(System.in);
        w = reader.nextInt();
    }

Scanner移出getBloodPressure ,然后在读入变量时将其传入。

Scanner reader = new Scanner(System.in);

int s = reader.nextInt();
int d = reader.nextInt();
new ChoiceAssignment().getBloodPressure(s, d);

暂无
暂无

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

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