简体   繁体   中英

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

Hello I have to create a custom project that determines age by the average blood pressure and weight of a person. I'm not finished but my question is this. How can I apply the code from getBloodPressure in the main method? I need to make at least four custom methods of my own so cannot just use that code inside the main method. Thanks in advance!

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

Remove those parameters from getBloodPressure like this, You'll then be able to call 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();
    }

Move the Scanner out of getBloodPressure , then pass in the variables as you read them in.

Scanner reader = new Scanner(System.in);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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