我无法获得此程序的输出以返回学生的字母等级。另外,我在最高等级方面遇到了麻烦。任何帮助都会很棒。
import java.util.Scanner;
public class GPACalc {
public static void main(String[] args) {
Scanner input = new Scanner ( System.in );
System.out.println( "Last Name: " );
String studentName = input.next();
System.out.println( "First Name: " );
String firstName = input.next();
System.out.println( "Enter CWID: " );
int id = input.nextInt();
System.out.print( "Enter Student Grades: " );
int count = 0;
double total = 0.000;
double avg;
int max = 0, min = 0;
int gradeNum = 0;
while ( true ) {
int grade;
grade = input.nextInt();
if ( grade < 0.000 || grade > 100 )
break;
if ( grade > max );
max = grade;
if ( grade < min )
;
min = grade;
total = total + grade;
count++;
}
avg = total / count;
char letter = 'H';
switch( letter ) {
case 'A':
avg = 100;
System.out.println("You have a perfect score. Keep up the excellent work!");
break;
case 'B':
avg = 90;
System.out.println("You have an A. Great Job!");
break;
case 'C':
avg = 80;
System.out.println("You have a B. Keep studying hard!");
break;
case 'D':
avg = 70;
System.out.println("You have a C. Commit a little more of your time.");
break;
case 'E':
avg = 60;
System.out.println("You have a D. You might want to study.");
break;
case 'F':
avg = 50;
System.out.println("You have an F. Better luck next semester.");
break;
case 'G':
avg = 0;
System.out.println( "It's time to change majors.");
break;
}
System.out.println("\n----------------------------------------------------------");
System.out.println( " STUDENT IFORMATION" );
System.out.println("\n----------------------------------------------------------");
System.out.println( " Student Name -- " + firstName + " " + studentName );
System.out.println( " Student ID -- " + id );
System.out.println( " Students GPA -- " + avg );
System.out.println( " # of Grades -- " + count );
System.out.println( " Highest Grade -- " + max );
System.out.println( " Lowest Grade -- " + min );
System.out.println("\n----------------------------------------------------------");
}
}
答案 0 :(得分:2)
您的“if”语句不起作用,因为您使用分号跟随它们。你的意思是这样做:
if (a) dosomething;
...而是你这样做:
if (a) ; // does nothing
dosomething; // always does something regardless of "a"
要么完美地完成它,要么它不起作用。没有“A for effort”。
关于你使用switch语句的愿望,这很简单,但你已经离开并试图向后做。首先,你计算了平均值。 (这是一个好主意。)然后你告诉电脑,等级是'H'。 (为什么?这显然不是一个好主意。)然后你要求它打开等级(这是一个'H'),如果是'H',你用一个零覆盖你计算的平均值。所以所有学生都会得到零。而H's。
你打算做的是取你计算的平均值,然后从那里得到成绩信。因此,切换平均值,并提供平均值可以作为案例标签:
switch (avg)
{
case 100: case 99: case 98: // etc.
letter = 'A';
break;
// and so on
}
由于您可能不想将“case 50”键入“case 0”,只需使用“default:”匹配您未明确列出的所有案例。
P.S。使你的平均成为一个int(而不是一个双重),以便你可以打开它
p.p.s。打印出你计算的变量,这样你才能真正知道它们是你认为的应该是......我们称之为“调试”;-)