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

时间:2015-05-15 16:57:58

标签: java methods

您好我必须创建一个自定义项目,通过平均血压和人的体重来确定年龄。我还没完,但问题是这个。如何在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();
    }
}

2 个答案:

答案 0 :(得分:0)

getBloodPressure中删除这些参数,然后您就可以调用方法。

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

答案 1 :(得分:0)

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

Scanner reader = new Scanner(System.in);

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