gpt4 book ai didi

java - 我如何从java中的整数中获取 double 值?

转载 作者:行者123 更新时间:2023-12-02 07:17:45 25 4
gpt4 key购买 nike

所以我很难弄清楚如何正确地得出这些值。所有的数学都是正确的,但它只表述了整数。例如,当我输入数字 87、42 和 94 时,平均值应为 74.3 重复。但对我来说它只得到 74.0。最低分数的平均值也是如此。

Scanner keyboard = new Scanner(System.in);

int a;
int b;
int c;
double grade;
double avg;
double avg2 = 0;

System.out.print("Enter score number 1: ");
a = keyboard.nextInt();
System.out.print("Enter score number 2: ");
b = keyboard.nextInt();
System.out.print("Enter score number 3: ");
c = keyboard.nextInt();

avg = ((a + b + c) / 3);


System.out.println("The average is: " + avg);

if (a < b && a < c)
{
System.out.println("The lowest score was: " + a);
System.out.println("The average without the lowest score is: " + ((b + c) / 2));
avg2 = ((b + c) / 2);
}


if (b < a && b < c)
{
System.out.println("The lowest score was: " + b);
System.out.println("The average without the lowest score is: " + ((a + c) / 2));
avg2 = ((a + c) / 2);
}


if (c < a && c < b)
{
System.out.println("The lowest score was: " + c);
System.out.println("The average without the lowest score is: " + ((a + b) /2));
avg2 = ((a + b) / 2);
}

最佳答案

您必须将 avg 表达式的分子中的总和转换为 double 或在分母中使用 double :

平均 = ((双)(a + b + c)/3);

平均值 = ((a + b + c)/3.0);

关于java - 我如何从java中的整数中获取 double 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14720547/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com