📄 examgrades.java
字号:
//********************************************************************
// ExamGrades.java Author: Lewis/Loftus
//
// Demonstrates the use of various control structures.
//********************************************************************
import java.text.DecimalFormat;
import cs1.Keyboard;
public class ExamGrades
{
//-----------------------------------------------------------------
// Computes the average, minimum, and maximum of a set of exam
// scores entered by the user.
//-----------------------------------------------------------------
public static void main (String[] args)
{
int grade, count = 0, sum = 0, max, min;
double average;
// Get the first grade and give max and min that initial value
System.out.print ("Enter the first grade (-1 to quit): ");
grade = Keyboard.readInt();
max = min = grade;
// Read and process the rest of the grades
while (grade >= 0)
{
count++;
sum += grade;
if (grade > max)
max = grade;
else
if (grade < min)
min = grade;
System.out.print ("Enter the next grade (-1 to quit): ");
grade = Keyboard.readInt ();
}
// Produce the final results
if (count == 0)
System.out.println ("No valid grades were entered.");
else
{
DecimalFormat fmt = new DecimalFormat ("0.##");
average = (double)sum / count;
System.out.println();
System.out.println ("Total number of students: " + count);
System.out.println ("Average grade: " + fmt.format(average));
System.out.println ("Highest grade: " + max);
System.out.println ("Lowest grade: " + min);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -