プロが教える店舗&オフィスのセキュリティ対策術

6人分の小テストの点数を記録し,平均点,最高点,平均点以下の人の一覧を計算するプログラムを作成したいのです。
入力はコマンドラインから行います。
しかし,TEST配列がうまく初期化がうまく記述できないため,コンパイル時点で”シンボルが見つけられません”というエラーが出てしまいます。

Javaの経験が浅いので、文法そのものが間違っているか心配です。C言語についてはある程度知識がありますから、C言語と対比して教えて頂けたりすると大変たすかります。

宜しくお願い致します。


class Lecture {
static Lecture[] TEST;
static double avg=0;
static int max=0;
static int i = 0;
static String kamoku;
int scorebox;
String name;
String student_number;

Lecture(int size){
TEST = new Lecture[size];
for (int i = 0; i < size; i++) {
Exercise a = new Exercise();
Student b = new Student();
TEST[i] = new Lecture(a,b);
}
}
static void add(Exercise score, Student aStudent)
{
TEST[i].name = aStudent.name;
TEST[i].student_number = aStudent.student_number;
TEST[i].scorebox = score.score;
i++;
}
static void avg()
{
int sum=0;
for (int i = 0; i < 6; i++)
{
sum += TEST[i].scorebox;
}
avg = sum / 6;
}
static void max()
{
int max = 0;
int temp = 0;
for (int i = 0; i < 6; i++)
{
if (max < TEST[i].scorebox)
{
temp = i;
max = TEST[i].scorebox;
}
}
max = i;
}
static void show_kamoku(){
System.out.println("科目:"+kamoku);
}
static void show(){
Lecture.max();
System.out.println("平均点:"+avg);
System.out.println("最高得点者:"+TEST[max]);
}

static void under_avg(){
Lecture.avg();
System.out.println("平均点を下回った者");
for(int i=0 ; i<6 ; i++){
if(avg > TEST[i].scorebox){
System.out.println(TEST[i]);
}
}
}
}

class Exercise {
int score;
Exercise(int score) {
this.score = score;
}
Exercise()
{
}
public String toString() {
return " 得点:" + score;
}
}

class Student {
String name;
String student_number;

Student(String student_number,String name ) {
this.name = name;
this.student_number = student_number;
}
Student()
{
}
public String toString() {
return "学籍番号:" + student_number + " 名前:" + name;
}
}

class ExerciseEvaluation {
public static void main(String args[]){

Lecture.kamoku=args[0];
Lecture[] lec = new Lecture[6];

Student Y0 = new Student(args[1],args[2]);
Exercise X0 = new Exercise(Integer.parseInt(args[3]));
lec[0].add(X0, Y0);

Student Y1 = new Student(args[4], args[5]);
Exercise X1 = new Exercise(Integer.parseInt(args[6]));
lec[1].add(X1, Y1);

Student Y2 = new Student(args[7], args[8]);
Exercise X2 = new Exercise(Integer.parseInt(args[9]));
lec[2].add(X2, Y2);

Student Y3 = new Student(args[10], args[11]);
Exercise X3 = new Exercise(Integer.parseInt(args[12]));
lec[3].add(X3, Y3);

Student Y4 = new Student(args[13], args[14]);
Exercise X4 = new Exercise(Integer.parseInt(args[15]));
lec[4].add(X4, Y4);

Student Y5 = new Student(args[16], args[17]);
Exercise X5 = new Exercise(Integer.parseInt(args[18]));
lec[5].add(X5, Y5);

Lecture.show_kamoku();
Lecture.show();
Lecture.under_avg();
}
}

A 回答 (2件)

引き続きHKBが回答します。



>できれば、コマンドラインからの入力という条件は外したくないのですが…。
ここでの「コマンドラインからの入力」というのは、
何を指していますか?

私の例では、プロンプトを出して名前、点数などの入力を求める形式
(java Lecture 2
name > 太郎
score > 90
name > 花子
score > 80
average : 85
max : 太郎, 90
under average :
花子, 80)
になっています。

wintervさんは、引数として指定する形式
(java Lecture 太郎 90 花子 80
average : 85
以下、上の例と同じ)
にしたい、ということでしょうか。
    • good
    • 0

// クラスの役割が不明瞭に思います。


// このようなプログラムではいかがでしょう。

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

// compile : javac Lecture.java
// execute : java Lecture 6 (生徒数6名の場合)

// 生徒1名を表すクラス
class Student
{
private String name; // 名前
private int score; // 得点
public Student(String name, int score)
{
this.name = name;
this.score = score;
}
public String getName()
{
return name;
}
public int getScore()
{
return score;
}
}

// 小テストをまとめるクラス
class Test
{
private Student students []; // 受験した生徒
private int count; // 受験した人数
public Test(int count)
{
students = new Student[count];
this.count = count;
}
// 名前、点数の入力
public void input()
{
for(int i=0; i<count ; i++)
{
String name = inputLine("name > ");
String score = inputLine("score > ");
students[i] = new Student(name, Integer.parseInt(score));
}
}
// 平均点、最高点、平均点以下の人を表示
public void result()
{
// 平均点の表示
double avg = average();
System.out.println("average : " + avg);
// 最高点の表示
int maxindex = maxIndex();
System.out.println("max : " + students[maxindex].getName() + ", " + students[maxindex].getScore());
// 平均点以下の人の表示
System.out.println("under average : ");
for(int i=0 ; i<count ; i++)
{
Student s = students[i];
if(s.getScore() <= avg)
{
System.out.println(s.getName() + ", " + s.getScore());
}
}
}
// プロンプトを表示し、文字列入力
private String inputLine(String prompt)
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print(prompt);
String result;
try
{
result = br.readLine();
}
catch(IOException e)
{
System.err.println("input error");
result = "";
}
return result;
}
// 平均点計算
private double average()
{
int sum = 0;
for(int i=0 ; i<count ; i++)
{
sum += students[i].getScore();
}
return (double)sum / (double)count;
}
// 最高点の人の配列添字を返す
private int maxIndex()
{
int maxindex = 0;
int maxscore = students[0].getScore();
for(int i=0 ; i<count ; i++)
{
if(students[i].getScore() > maxscore)
{
maxindex = i;
maxscore = students[i].getScore();
}
}
return maxindex;
}
}

// 実行部
class Lecture
{
public static void main(String args [])
{
Test test = new Test(Integer.parseInt(args[0]));
test.input(); // 名前、点数を入力
test.result(); // 結果を表示
}
}
    • good
    • 0
この回答へのお礼

ありがとうございます。

できれば、コマンドラインからの入力という条件は外したくないのですが…。

お礼日時:2008/06/23 21:23

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!