package lab3;
import java.util.Scanner;
public class Lab3
{
static int n;
static double testScores[] = new double[n];
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int studentID;
System.out.println("Enter student ID: ");
studentID = input.nextInt();
System.out.println("Enter number of test scores");
n = input.nextInt();
readTestScore(n);
//double[] testarray = readTestScore(n);
//System.out.println("Test Scores are:" + testarray);
}
public char getLetterGrade(double score)
{
char letter;
if(score >= 90)
{
printComment('A');
letter = 'A';
}
if(score >= 80)
{
printComment('B');
letter = 'B';
}
if(score >= 70)
{
printComment('D');
letter = 'C';
}
else if(score >= 60)
{
printComment('D');
letter = 'D';
}
else if(score < 60)
printComment('F');
letter = 'F';
return letter;
}
static void printComment(char grade)
{
if(grade == 'A')
System.out.println("COMMMENT\t\t\t:\t" + "Very Good!");
if(grade == 'B')
System.out.println("COMMMENT\t\t\t:\t" + "Good!");
if(grade == 'C')
System.out.println("COMMMENT\t\t\t:\t" + "Satisfactory.");
if(grade == 'D')
System.out.println("COMMMENT\t\t\t:\t" + "Needs Improvement.");
if(grade == 'F')
System.out.println("COMMMENT\t\t\t:\t" + "Poor.");
}
static void printTestResults(double []testList)
{
for(int i = 0; i < testList.length; i++)
{
System.out.println("Test Score\t\t\t" + "Letter Grade\t\t\t" + "Comment\t\t\t");
System.out.println(testScores[i] + "\t\t\t" );//+ getLetterGrade(score) + "\t\t\t" + printComment(grade));
}
}
static double[] readTestScore(int size)
{
Scanner input = new Scanner(System.in);
int i =0;
System.out.println("Enter Test Scores:\t");
for(i = 0; i<n; i++)
{
testScores[i] = input.nextDouble();
}
testScores[i] /=n;
System.out.println("these are test scores: " + testScores);
return (testScores);
}
}发布于 2015-09-22 14:46:46
因为这个原因
static int n;
static double testScores[] = new double[n];数组被设置为默认大小int ie 0,这会在方法中给出错误
https://stackoverflow.com/questions/32710099
复制相似问题