如何将我自己的方法的操作实现到 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
中删除那些参数,然后你就可以调用方法了。
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);
您好,我必须创建一个自定义项目,根据一个人的平均血压和体重来确定年龄。我还没有完成,但我的问题是这样的。如何在 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
中删除那些参数,然后你就可以调用方法了。
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);