我在显示平均小数时遇到问题。它一直显示 0.00 或 0.1。我试图把它加倍,但我仍然得到相同的结果。另外,我想将我输入的第一个整数包含在总和中,但我不知道如何。请帮助:
import java.io.*;
import java.util.*;
public class WhileSentinelSum
{
static final int SENTINEL= -999;
public static void main(String[] args)
{
// Keyboard Initialization
Scanner kbin = new Scanner(System.in);
//Variable Initialization and Declaration
int number, counter;
int sum =0;
int average;
//Input
System.out.print("Enter an integer number: " );
number = kbin.nextInt();
counter=0;
//Processing
//Output
while(number != SENTINEL)
{
counter++;
System.out.print("Enter an integer number: ");
number = kbin.nextInt();
sum += number;
}
if (counter !=0)
System.out.println("\nCounter is: " + counter);
else
System.out.println("no input");
average= sum/counter;
System.out.println("The sum is " + sum);
System.out.println("The average is " + average);
System.out.println();
}//end main
}//end class