使用命令行参数输入计算 BMI
Calculate BMI using command line arguments inputs
这里是第一个问题。我必须使用之前计算 BMI 的分配,并重新格式化它以接受命令行参数作为身高和体重的输入。
“您的程序应通过 main(String[] args) 获取体重和身高,即,当您 运行 您的程序必须执行以下操作:
java 我的程序名称 180 5 7
其中 MyProgramName 是程序的名称,180 是以磅为单位的重量,5 是英尺,7 是英寸值。
程序应像以前一样在终端 window 中输出 BMI 值(下面的项目 f)。"
我对如何在对参数执行操作数时将参数调用到代码中感到困惑。
这是我的原始代码:
'int weight;
int heightInInches;
int bmi;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter your weight in pounds: ");
weight = keyboard.nextInt();
System.out.print("Enter your height in inches: ");
heightInInches = keyboard.nextInt();
bmi = ((weight * 703)/(heightInInches * heightInInches));
System.out.println("Your height is " + heightInInches + " and your
weight is: " + weight + " pounds");
System.out.println("Your BMI is " + bmi);'
我见过这样的东西,只是将两个数字相加,但不知道如何将其更改为 BMI 公式。
int sum = 0;
for (int i = 0; i < args.length; i++) {
sum = sum + Integer.parseInt(args[i]);
}
System.out.println("The sum of the arguments passed is " + sum);
谢谢
您的 String[] args
看起来像 s: ["180","5","7"]
所以 args[0]
就是你的体重。
args[1]
将是 heightInFeet(乘以 12 得到 heightInInches)
args[2]
将是高度的英寸部分
所以代码变成:
int weight = Integer.parseInt(args[0]);
int heightInInches = Integer.parseInt(args[1])*12 + Integer.parseInt(args[2]);
bmi = ((weight * 703)/(heightInInches * heightInInches));
这里是第一个问题。我必须使用之前计算 BMI 的分配,并重新格式化它以接受命令行参数作为身高和体重的输入。
“您的程序应通过 main(String[] args) 获取体重和身高,即,当您 运行 您的程序必须执行以下操作:
java 我的程序名称 180 5 7
其中 MyProgramName 是程序的名称,180 是以磅为单位的重量,5 是英尺,7 是英寸值。
程序应像以前一样在终端 window 中输出 BMI 值(下面的项目 f)。"
我对如何在对参数执行操作数时将参数调用到代码中感到困惑。 这是我的原始代码:
'int weight;
int heightInInches;
int bmi;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter your weight in pounds: ");
weight = keyboard.nextInt();
System.out.print("Enter your height in inches: ");
heightInInches = keyboard.nextInt();
bmi = ((weight * 703)/(heightInInches * heightInInches));
System.out.println("Your height is " + heightInInches + " and your
weight is: " + weight + " pounds");
System.out.println("Your BMI is " + bmi);'
我见过这样的东西,只是将两个数字相加,但不知道如何将其更改为 BMI 公式。
int sum = 0;
for (int i = 0; i < args.length; i++) {
sum = sum + Integer.parseInt(args[i]);
}
System.out.println("The sum of the arguments passed is " + sum);
谢谢
您的 String[] args
看起来像 s: ["180","5","7"]
所以 args[0]
就是你的体重。
args[1]
将是 heightInFeet(乘以 12 得到 heightInInches)
args[2]
将是高度的英寸部分
所以代码变成:
int weight = Integer.parseInt(args[0]);
int heightInInches = Integer.parseInt(args[1])*12 + Integer.parseInt(args[2]);
bmi = ((weight * 703)/(heightInInches * heightInInches));