简体   繁体   中英

Something went wrong with a total number of a loop

The below programs are divided into two classes(two different files), and what they do is to scan a user's input, including names, ages, heights, and weights, and calculate each inputs BMIs. And I wanted to show the number of loops at the end of the loop, which is stated on the 28th line of Jan7, but somehow the multiplied number of the user input is displayed. What is wrong with the code? And how could I fix it? Please advise.

import java.util.Scanner;

class Jan7 {
    public static void main(String[] args) {  
        System.out.print("Type a loop number: ");
        Scanner scannerNum = new Scanner(System.in);
        int num = scannerNum.nextInt();
        int total = 0;
        for (int i = 0; i < num; i++) {

            Scanner scanner = new Scanner(System.in);
            System.out.print("Tell us your first name: ");
            String scanFN = scanner.next();
            System.out.print("Tell us yout last name: ");
            String scanLN = scanner.next();
            System.out.print("Tell us yout age: ");
            int scanAge = scanner.nextInt();
            System.out.print("Tell us yout hegiht: ");
            double scanH = scanner.nextDouble();
            System.out.print("Tell us yout weight: ");
            double scanW = scanner.nextDouble();
            Jan7Person person = new Jan7Person(scanFN, scanLN, scanAge, scanH, scanW);
            person.printData();

            total += num;

        }
        System.out.println(total); //28th line
    }
}

class Jan7Person {
    public static int count = 0;

    public String firstName;
    public String lastName;
    public int age;
    public double height;
    public double weight;

    Jan7Person(String firstName, String lastName, int age, double height, double weight) {
        Jan7Person.count++;

        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
        this.height = height;
        this.weight = weight;
    }

    public String fullName() {
        return this.firstName + " " + this.lastName;
    }

    public double bmi() {
        return this.weight / this.height / this.height;
    }

    public void printData() {
        System.out.println("Your name is " + this.fullName() + ".");
        System.out.println("Your age is " + this.age + ".");
        System.out.println("Your BMI is " + Math.round(this.bmi()) + ".");
    }



}

The core of your code is:

int num = scannerNum.nextInt();
int total = 0;
for (int i = 0; i < num; i++) {
    total += num;
}
System.out.println(total);

Which obviously prints the square of num .

What you need seems already done in person.printData(); .

If you want to construct a structure for printing all person 's data, please use an Array or a List .

如果我正确理解Dunno,但如果您想显示当前所在的循环号,请使用循环的i或i + 1(如果您不想为零)。

total = i;

i may not understand what you really want but if you want the program to print the number of round of the loop you have to replace

total += num;

with

total = num+1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM