在for循环中添加变量

时间:2018-05-03 19:20:10

标签: java arrays for-loop int java.util.scanner

我正在制作一个代码,您可以输入您想要的人数并查看他们的BMI,我希望我的代码总结所有人的总BMI,然后将其除以总量人们输入。

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;
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

我可以看到此代码的几个问题......

  • 您正在使用Scanner的多个实例。你应该只使用一个。

  • 您没有跟踪总体BMI。如果要打印总平均BMI,您会想要这个。在for循环之外,您需要初始化变量double totalBMI = 0并在for循环中,将每个人的BMI添加到总数中。

  • 您正在对“数字”求和,但我不确定这些数字代表什么。从编写代码的方式来看,它们看起来像是人数。是吗?如果是这样,变量sum将更好地命名为numberOfPeople

  • 您不需要{for for for循环中的break。它有退出条件。

  • BMI计算似乎对我很怀疑。如果您更准确地命名变量,则更容易发现错误。我会写:

    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;
    }
    

答案 1 :(得分:0)

让我们清理你的代码,不管吗?

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(); }
        });
    }
}