📄 average.java
字号:
//********************************************************************
// Average.java Author: Lewis/Loftus
//
// Demonstrates the use of a while loop, a sentinel value, and a
// running sum.
//********************************************************************
import java.text.DecimalFormat;
import cs1.Keyboard;
public class Average
{
//-----------------------------------------------------------------
// Computes the average of a set of values entered by the user.
// The running sum is printed as the numbers are entered.
//-----------------------------------------------------------------
public static void main (String[] args)
{
int sum = 0, value, count = 0;
double average;
System.out.print ("Enter an integer (0 to quit): ");
value = Keyboard.readInt();
while (value != 0) // sentinel value of 0 to terminate loop
{
count++;
sum += value;
System.out.println ("The sum so far is " + sum);
System.out.print ("Enter an integer (0 to quit): ");
value = Keyboard.readInt();
}
System.out.println ();
System.out.println ("Number of values entered: " + count);
average = (double)sum / count;
DecimalFormat fmt = new DecimalFormat ("0.###");
System.out.println ("The average is " + fmt.format(average));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -