计算平均 Java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26852525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 03:23:43  来源:igfitidea点击:

Calculate Average Java

javamatharithmetic-expressions

提问by Jake Zywiol

I'm trying to calculate the average of the student scores and as it stands I'm struggling with the calculation part.

我正在尝试计算学生分数的平均值,目前我正在计算部分苦苦挣扎。

In my assignment I was asked to caulculate the avg age of three students and avg. heigth of three students.

在我的作业中,我被要求计算三个学生的平均年龄和平均年龄。三个学生的身高。

Could someone please guide me in the last line of this code, I apologize for the newbie question, really stuck and cannot come up with any solution to why it's not calculating my numbers.

有人可以在这段代码的最后一行指导我吗,我为新手问题道歉,真的卡住了,无法想出任何解决方案来解释为什么它不计算我的数字。

public static void main(String[] args) {
// this method will display the scores of students 

//variable declaration
int JasperAge = 20; 
int JasperHeigth = (int) 175.5;
int PaulaAge = 25; 
int PaulaHeigth = (int) 190.5;
int NicoleAge = 18;
int NicoleHeigth = (int) 165;

//output
Scanner output = new Scanner (System.in);


System.out.println("Name\t "+ " Age\t " + " Height (cm)\t");
System.out.println("\n");
System.out.println("Jasper\t  "+JasperAge+"  \t    "+JasperHeigth);
System.out.println("Paula\t  "+PaulaAge+"  \t    "+PaulaHeigth);
System.out.println("Nicole\t  "+NicoleAge+" \t    "+NicoleHeigth);
System.out.println("Average\t ((20 + 25 + 18) /3) \t ((175.5 + 190.5 + 165) /3)");
}

}

}

采纳答案by mattias

Your last line should probably be something like:

你的最后一行应该是这样的:

System.out.println("Average age: " + ((JasperAge + PaulaAge + NicoleAge) /3) + ". Average height: " +  ((JasperHeigth + PaulaHeigth + NicoleHeigth) /3) ".");

Mind my calculations, but you get the idea.

注意我的计算,但你明白了。

回答by NerosE

You just have to make your last print like this:

你只需要像这样制作你的最后一张照片:

System.out.println("Average\t  " + ((20 + 25 + 18) /3) + "\t    " + ((175.5 + 190.5 + 165) /3));

or even better use your variables:

甚至更好地使用您的变量:

System.out.println("Average\t  " + ((JasperAge + PaulaAge + NicoleAge) /3) + "\t    " + ((JasperHeigth + PaulaHeigth + NicoleHeigth) /3));

回答by Christian Wilkie

There are a few things wrong:

有几件事是错误的:

int JasperHeigth = (int) 175.5;
int PaulaHeigth = (int) 190.5;
int NicoleHeigth = (int) 165;

Given that these appear to be heights with decimal values, it is likely that you would want to store these as doubles instead of ints. When you declare a value like 175.5 as an integer, it is actually truncated to instead be 175. To store the full value of these numbers, you should instead define them as:

鉴于这些看起来是带有十进制值的高度,您可能希望将它们存储为doubles 而不是ints。当您将 175.5 之类的值声明为整数时,它实际上会被截断为 175。要存储这些数字的完整值,您应该将它们定义为:

    double JasperHeigth = 175.5;
    double PaulaHeigth = 190.5;
    double NicoleHeigth = 165;

Side note: the reason you had to cast those numbers using (int) was because 175.5is actually a doubleliteral instead of an int, which was what you declared the variable as before.

旁注:您必须使用 (int) 转换这些数字的原因是因为175.5它实际上是一个double文字而不是 an int,这是您之前声明的变量。

Next, this scanner definition line is never used:

接下来,从不使用此扫描仪定义行:

Scanner output = new Scanner (System.in);

You would use the Scannerclass to get input from the user. For instance, if you wanted the user to enter in some names or numbers. In this case, it doesn't look like you need to request any input from the user so this line can probably be deleted.

您将使用Scanner该类从用户那里获取输入。例如,如果您希望用户输入一些姓名或数字。在这种情况下,您似乎不需要向用户请求任何输入,因此该行可能会被删除。

And lastly, the problem with displaying your output is in this line:

最后,显示输出的问题在于这一行:

System.out.println("Average\t ((20 + 25 + 18) /3) \t ((175.5 + 190.5 + 165) /3)");

The problem is that by enclosing the numbers within quotation marks, your expected arithmetic will not be evaluated and instead just displayed to the user as character data. If you wanted to evaluate those expressions you could instead pull the math operations out of the quotation marks and concatenate them to the String data using the +operator:

问题在于,通过将数字括在引号内,您预期的算术不会被评估,而只会作为字符数据显示给用户。如果您想评估这些表达式,您可以改为从引号中提取数学运算,并使用+运算符将它们连接到字符串数据:

System.out.println("Average\t" +  ((20 + 25 + 18) /3) + "\t" + ((175.5 + 190.5 + 165) /3));

However, there are still a few things wrong with this. First, ((20 + 25 + 18) /3)will evaluate as integer division. It will reduce to 63/3which is 21. However, it would also display 21 if you had 64/3or 65/3, because integer division truncates the part of the number past the decimal point. To prevent truncation of your desired result, you can cast either one of the numbers in the numerator or denominator to double, or divide by a double literal such as 3.0. So something like this:

但是,这仍然存在一些问题。首先,((20 + 25 + 18) /3)将评估为整数除法。它将减少63/3为 21。但是,如果您有64/3或,它也会显示 21 65/3,因为整数除法会截断小数点后的数字部分。为防止截断所需的结果,您可以将分子或分母中的任一数字转换为 double,或除以 double 文字,例如3.0. 所以像这样:

System.out.println("Average\t" +  ((20 + 25 + 19) /3.0) + "\t" + ((175.5 + 190.5 + 165) /3.0));

Then finally, none of these numbers are actually using the variables you defined earlier, they are completely separate. If you want to actually average the variables you will need to substitute them into the expression like this:

最后,这些数字实际上都没有使用您之前定义的变量,它们是完全独立的。如果您想对变量进行实际平均,则需要将它们代入表达式中,如下所示:

System.out.println("Average\t" +  ((JasperAge + PaulaAge + NicoleAge) /3.0) + "\t" + ((JasperHeigth + PaulaHeigth + NicoleHeigth) /3.0));

Summary

概括

Here is a program with all my suggested edits:

这是一个包含我所有建议编辑的程序:

public static void main(String[] args) {
    // this method will display the scores of students

    //variable declaration
    int JasperAge = 20;
    double JasperHeigth = 175.5;
    int PaulaAge = 25;
    double PaulaHeigth = 190.5;
    int NicoleAge = 18;
    double NicoleHeigth = 165;

    System.out.println("Name\t "+ " Age\t " + " Height (cm)\t");
    System.out.println("\n");
    System.out.println("Jasper\t  "+JasperAge+"  \t    "+JasperHeigth);
    System.out.println("Paula\t  "+PaulaAge+"  \t    "+PaulaHeigth);
    System.out.println("Nicole\t  "+NicoleAge+" \t    "+NicoleHeigth);
    System.out.println("Average\t" +  ((JasperAge + PaulaAge + NicoleAge) /3.0) + "\t" + ((JasperHeigth + PaulaHeigth + NicoleHeigth) /3.0));
}

回答by duffymo

Java's an object-oriented language. You might just be starting, but it's never too soon to learn about encapsulation:

Java 是一种面向对象的语言。您可能刚刚开始,但学习封装永远不会太早:

public class Student {
    private final String name;
    private final int age;  // bad idea - why?
    private final double heightInCm;

    public Student(String n, int a, double h) {
        this.name = n;
        this.age = a;
        this.heightInCm = h;
    }

    public String getName() { return this.name; }
    public int getAge() { return this.age; }
    public double getHeightInCm() { return this.heightInCm; }

    public String toString() {
        return String.format("name: '%s' age: %d height: %10.2f (cm)", this.name, this.age, this.heightInCm);
    }
}