这里的第一个问题。我必须使用先前的分配来计算BMI,并将其重新格式化以接受命令行参数作为身高和体重的输入。
“您的程序应通过main(String [] args)获得重量和高度,即,当您运行程序时,必须执行以下操作:java MyProgramName 180 5 7
其中MyProgramName是程序的名称,180是“以磅为单位的重量,5是英尺,7是英寸。程序将在终端窗口中像以前一样输出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));
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句