如何获取实际平均值,而不是四舍五入的值?

huangapple go评论131阅读模式
英文:

How to get the actual average, not rounded?

问题

这是我所做的更改(为了这个问题更改了打印消息),当我输入4和11时,你应该得到7.5,但实际上只给了我7。我该如何修复这个问题?

import java.util.Scanner;

class U1_L6_Average_Finder {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        double a;  // 将变量的类型更改为double
        double b;  // 将变量的类型更改为double
        System.out.println("输入4");  // 将打印消息更改为中文
        a = scan.nextDouble();  // 使用scan.nextDouble()以获取浮点数输入
        System.out.println("输入11");  // 将打印消息更改为中文
        b = scan.nextDouble();  // 使用scan.nextDouble()以获取浮点数输入
        System.out.println("这是你的平均值");  // 将打印消息更改为中文
        System.out.print((a + b) / 2);
    }
}
英文:

this is what I have (changed the print message for this question), and when I do 4 and 11, you should get 7.5, but it's only giving me 7. how do I fix this?

import java.util.Scanner;

class U1_L6_Average_Finder {
public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int a;
    int b;
    System.out.println("put in 4");
    a = scan.nextInt();
    System.out.println("Put in 11");
    b = scan.nextInt();
    System.out.println("Here is your average");
    System.out.print((a + b) / 2);
}

答案1

得分: 1

你正在使用整数(int a, b;),因此它会对你的结果进行四舍五入。

与此替代,使用 doublefloat 来获得你想要的结果,例如:
double a, b;

但是,如果你不想修改变量的类型,你可以编辑最后的 System.out.print,使用 f(例如),这将把你的值转换为 float,例如:

System.out.print((a + b) / 2.0f);

查看这个链接以更了解变量:
Java变量

英文:

You're using integers (int a, b;), so it will round your results.

Instead of it, use double or float to get what you want, e.g:
double a, b;

But, if you don't want to modify the type of your variable, you can edit your last System.out.print using f (for example), that will convert your value to float, e.g:

System.out.print((a + b) / 2.0f);

Check this to understand more about variables:
Java Variables

答案2

得分: 1

你可以通过将分母中的 2 改为 2.02f2.0f 来强制将商强制转换为浮点值。如果你想要一个双精度值,可以用 d 代替 f

public class AverageFloatingPointCoercionExample {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int a, b;

        System.out.print("输入值 #1 (4): ");
        a = scan.nextInt();

        System.out.print("输入值 #2 (11): ");
        b = scan.nextInt();

        System.out.print("这是您的平均值: ");
        System.out.print((a + b) / 2.0f); // 7.5
    }
}

另外,你也可以显式地将分子进行类型转换。

System.out.print(((float) (a + b)) / 2);
英文:

You could coerce the quotient to a floating-point value by changing the 2 (denominator) to a 2.0, 2f, 2.0f. If you want a double, you can use a d instead of an f.

public class AverageFloatingPointCoercionExample {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int a, b;

        System.out.print("Enter value #1 (4): ");
        a = scan.nextInt();

        System.out.print("Enter value #2 (11): ");
        b = scan.nextInt();

        System.out.print("Here is your average: ");
        System.out.print((a + b) / 2.0f); // 7.5
    }
}

Alternatively, you can cast the numerator explicitly.

System.out.print(((float) (a + b)) / 2);

答案3

得分: 0

  • 你已经掌握了输入/平均的部分。所以这是缺失的部分。
  • 只是输出 r...
  • 但是如果你想要特定小数位数(这里是 5 位)...
  • 我这样做是为了考虑你是否想要计算超过 2 个项目的平均值...
import java.text.DecimalFormat;
class Playground {
    public static void main(String[ ] args) {
        int i = 5;
        int j = 17;
        double r = (double)i/j;
        DecimalFormat df = new DecimalFormat("#.#####");
        System.out.println(r);
        System.out.println(df.format(r));
    }
}

输出:

0.29411764705882354

0.29412

英文:
  • You have the input/averaging nailed. So here's the missing bit.
  • Just output r...
  • But if you want a certain number of decimal places (5 here)...
  • I did this in case you wanted to average more than 2 items...

import java.text.DecimalFormat;
class Playground {
    public static void main(String[ ] args) {
        int i = 5;
        int j = 17;
        double r = (double)i/j;
        DecimalFormat df = new DecimalFormat("#.#####");
        System.out.println(r);
        System.out.println(df.format(r));
    }
}

Outputs:

0.29411764705882354

0.29412

答案4

得分: 0

你正在使用整数除法。整数除法将会得到一个整数作为答案。你需要将其中一个值改为双精度类型(double)。

示例:

System.out.println((double)(a+b)/2);

或者

System.out.println((a+b)/2.0));

英文:

You are using integer divison. Integer divison will result integer as answer. You have to change one of the values to double.

Example:

System.out.println((double)(a+b)/2);

or

System.out.println((a+b)/2.0));

huangapple
  • 本文由 发表于 2020年9月9日 03:33:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63800482.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定