我创建了一个Java程序,该程序接受三个整数,并将打印出这三个整数的和,乘积,最小和最大以及平均值。
import java.util.Scanner;
public class ThreeNums
{
public static void main (String[] args)
{
new ThreeNums();
}
public ThreeNums ()
{
int num1, num2, num3, sum, smallest, largest, exitPrompt = 0;
float average;
Scanner scan = new Scanner (System.in);
while (exitPrompt == 0)
{
System.out.printf ("\n\nPlease enter three integers.\n");
System.out.printf ("\t1st integer: ");
num1 = scan.nextInt();
System.out.printf ("\t2nd integer: ");
num2 = scan.nextInt();
System.out.printf ("\t3rd integer: ");
num3 = scan.nextInt();
System.out.printf ("\n");
sum = num1+num2+num3;
System.out.println ("Sum: " + sum);
System.out.printf ("Product: %d\n", num1*num2*num3);
if (num1<num2)
smallest = num1;
else
smallest = num2;
if (smallest<num3)
smallest = smallest;
else
smallest = num3;
if (num1>num2)
largest = num1;
else
largest = num2;
if (largest>num3)
largest = largest;
else
largest = num3;
System.out.printf ("Smallest integer: %d\n", smallest);
System.out.printf ("Largest integer: %d\n", largest);
average = sum/3;
System.out.printf ("Average: %.3f\n", average);
System.out.printf ("\nExit? 0 if NO, any other digit if YES\n");
exitPrompt = scan.nextInt();
}
System.out.printf ("\n\n");
}
}
除了(我认为)第56-57行之外,一切都很好:
average = sum/3;
System.out.printf ("Average: %.3f\n", average);
应该打印用户将输入的三个整数的平均值。例如,如果用户输入5、7和7,则应该显示平均值为6.333:
Please enter three integers.
1st integer: 5
2nd integer: 7
3rd integer: 7
Sum: 19
Product: 245
Smallest integer: 5
Largest integer: 7
Average: 6.333
Exit? 0 if NO, any other digit if YES
但是,平均而言,它仅显示6.000。
.
.
.
Average: 6.000
.
.
如果我输入78、45和23,则仅显示48.000,而不是48.667,依此类推。我的代码可能是什么问题?请帮忙...谢谢!
答案 0 :(得分:1)
此处:df %>%
mutate(ID_rleid = with(rle(type), rep(seq_along(lengths), lengths)))
和average = sum/3;
均为sum
的{{1}},因此结果为整数,然后将其赋予3
。您可以通过以下方法克服这一点:
int