我为TestScores
和ScoreException
创建了一个程序,现在我想弄清楚如何计算得分的平均值。以下是我编写平均值代码时的代码,它产生了一个找不到合适的字符串
TestScores
import javax.swing.*;
public class TestScore
{
public static void main(String args[]) throws Exception
{
int[] id = {1234, 2345, 3456, 4567, 5678};
int[] score = {0, 0, 0, 0, 0};
String scoreString = new String();
final int HIGHLIMIT = 100;
String inString, outString = "";
for(int x = 0; x < id.length; ++x)
{
inString = JOptionPane.showInputDialog(null,
"Enter score for student id number: " + id[x]);
score[x] = Integer.parseInt(inString);
try
{
if(score[x] > HIGHLIMIT)
{
scoreString = "Score over " + HIGHLIMIT;
throw (new ScoreException(scoreString));
}
}
catch(ScoreException e)
{
JOptionPane.showMessageDialog(null, e.getMessage());
score[x] = 0;
}
}
for(int x = 0; x < id.length; ++x)
outString = outString + "ID #" + id[x] + " Score " +
score[x] + "\n";
JOptionPane.showMessageDialog(null, outString);
}
}
我的ScoreException
代码
public class ScoreException extends Exception
{
public ScoreException(String s)
{
super(s);
}
}
这就是我对普通代码的看法,但它没有正确显示
int score[] = new score[] { 45, 98 ,80, 74, 93};
double result = 0;
{
for(int x=0; x < int.length; x++){
result += score[x];
}
System.out.println(result/count)
答案 0 :(得分:4)
double total=0;
double average=0;
for(int x = 0; x < score.length; ++x)
total+=score[x];
average=total/score.length;
JOptionPane.showMessageDialog("Average Score:"+ average);
你不明白的是什么?