Java 在for循环中添加变量
嗨,我正在做一个代码,你可以输入你想要多少人并查看他们的体重指数,问题是我希望我的代码将所有人的总体重指数相加,然后除以输入的总人数Java 在for循环中添加变量,java,arrays,for-loop,int,java.util.scanner,Java,Arrays,For Loop,Int,Java.util.scanner,嗨,我正在做一个代码,你可以输入你想要多少人并查看他们的体重指数,问题是我希望我的代码将所有人的总体重指数相加,然后除以输入的总人数 import java.util.Scanner; public class Main { public static double calculate(int weight, int height) { weight = 100*100*weight; height = height * height;
import java.util.Scanner;
public class Main {
public static double calculate(int weight, int height) {
weight = 100*100*weight;
height = height * height;
return weight / height;
}
public static void main(String[] args) {
int sum = 0;
System.out.println("Enter numbers here");
Scanner people = new Scanner((new Scanner(System.in)).nextLine());
while (people.hasNextInt()) {
sum += people.nextInt();
}
for (int x =1; x <= sum; x++) {
Scanner scanner = new Scanner(System.in);
System.out.println("hur lång är du i cm?");
int height = scanner.nextInt();
System.out.println("hur tung är du i kg?");
int weight = scanner.nextInt();
double BMI = calculate(weight, height);
System.out.println(BMI);
System.out.println(+x);
if (x == sum) {
break;
}
}
}
}
import java.util.Scanner;
公共班机{
公共静态双重计算(整数重量、整数高度){
重量=100*100*重量;
高度=高度*高度;
返回重量/高度;
}
公共静态void main(字符串[]args){
整数和=0;
System.out.println(“在此处输入数字”);
扫描器人员=新扫描器((新扫描器(System.in)).nextLine());
while(people.hasNextInt()){
sum+=people.nextInt();
}
对于(intx=1;x我可以看到这个代码有几个问题
- 您正在使用多个
扫描仪的实例。您应该只使用一个
- 您没有跟踪总BMI。如果要打印总平均BMI,您需要这样做。在for循环外,您需要初始化一个变量
double totalBMI=0
,在for循环内,将每个人的BMI添加到总BMI中
- 您正在对“数字”求和,但我不确定这些数字代表什么。从代码的编写方式来看,它们似乎是人的数字。是吗?如果是这样,变量
sum
最好命名为numberOfPeople
- 您不需要将
从for循环中断开。它有一个退出条件
public static double bmi(double weightInKg, double heightInCm) {
double heightInMeters = heightInCm / 100d;
// As per https://www.nhs.uk/chq/Pages/how-can-i-work-out-my-bmi.aspx
return weightInKg / heightInMeters / heightInMeters;
}
- 让我们清理一下您的代码,好吗
import java.util.Scanner;
import javax.swing.SwingUtilities;
public class BMICalculator {
public BMICalculator() {
try ( // use a "try with resources" to make sure the scanner is properly closed at the end
Scanner scanner = new Scanner(System.in);
) {
double totalBMI = 0d; // declare a variable to hold our total BMI - notice properly named variables don't need comments
System.out.println("Enter number of people (0 to exit):");
int numPeople = scanner.nextInt();
if (numPeople == 0) { return; }
System.out.println(""); // print a blank line just for looks
for (int x = 0; x < numPeople; x++) {
System.out.println(String.format("Enter measurements for person %d (Whole numbers only)", (x+1))); // x started at 0 so we need to a 1
System.out.println("hur lång är du i cm?");
int height = scanner.nextInt();
System.out.println("hur tung är du i kg?");
int weight = scanner.nextInt();
double BMI = calculate(weight, height);
totalBMI += BMI;
System.out.println(String.format("BMI for person %d = %3.3f", (1 + x), BMI));
System.out.println(""); // print a blank line just for looks
}
System.out.println(String.format("Average BMI = %3.3f.", (totalBMI / numPeople)));
}
}
public double calculate(int weight, int height) {
double _weight = (double)100*100*weight;
double _height = (double)height * height;
return _weight / _height;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() { new BMICalculator(); }
});
}
}
import java.util.Scanner;
导入javax.swing.SwingUtilities;
公共类BMI计算器{
公共计算机(){
try(//使用“try with resources”确保最后正确关闭扫描仪
扫描仪=新的扫描仪(System.in);
) {
double totalBMI=0d;//声明一个变量来保存我们的总BMI-注意,正确命名的变量不需要注释
System.out.println(“输入人数(0以退出):”;
int numpople=scanner.nextInt();
如果(numpople==0){return;}
System.out.println(“”;//打印一个空行仅用于外观
对于(int x=0;x
你在纠结什么?我正在纠结于把总的人体质量指数加起来,然后除以你用一种方法输入3个扫描仪对象的人数……看起来很奇怪。。。。