Java 如何将自己方法的操作实现到主方法中?
您好,我必须创建一个自定义项目,通过一个人的平均血压和体重来确定年龄。我还没说完,但我的问题是。如何在main方法中应用getBloodPressure中的代码?我需要自己创建至少四个自定义方法,所以不能只在主方法中使用该代码。提前谢谢Java 如何将自己方法的操作实现到主方法中?,java,methods,Java,Methods,您好,我必须创建一个自定义项目,通过一个人的平均血压和体重来确定年龄。我还没说完,但我的问题是。如何在main方法中应用getBloodPressure中的代码?我需要自己创建至少四个自定义方法,所以不能只在主方法中使用该代码。提前谢谢 /* * This program will determine the user's age by asking it general health information. */ package choice.assignment; import
/*
* 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();
}
}
/*
*该程序将通过询问用户的一般健康信息来确定用户的年龄。
*/
包选择、分配;
导入java.util.Scanner;
导入javax.swing.JOptionPane;
/*
*@作者talhaiqbal18
*/
公营课取消{
公共静态void main(字符串[]args){
扫描仪阅读器=新扫描仪(System.in);
System.out.println(“欢迎!此程序将确定您的年龄,要求您输入关于您自己的一般健康信息”);
System.out.println(“”);
新选项设计()。获取血压();
}
私人血压(整数s,整数d){
扫描仪阅读器=新扫描仪(System.in);
s=reader.nextInt();
d=reader.nextInt();
如果(s>=75&&s=50&&d=75&&s=50&&d像这样从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();
}
publicstaticvoidmain(字符串[]args){
扫描仪阅读器=新扫描仪(System.in);
System.out.println(“欢迎!此程序将确定您的年龄,要求您输入关于您自己的一般健康信息”);
System.out.println(“”);
新选项设计()。获取血压();
}
个人血压(){
扫描仪阅读器=新扫描仪(System.in);
int s=reader.nextInt();
int d=reader.nextInt();
如果(s>=75&&s=50&&d=75&&s=50&&d将扫描仪
移出getBloodPressure
,然后在读取变量时传入变量
Scanner reader = new Scanner(System.in);
int s = reader.nextInt();
int d = reader.nextInt();
new ChoiceAssignment().getBloodPressure(s, d);
你的问题不清楚。你到底在问什么?这很管用,但让我觉得恶心。为什么这样的方法应该接受用户输入?为什么不从main
获取它?