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件)
- 最新から表示
- 回答順に表示
No.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
以下、上の例と同じ)
にしたい、ということでしょうか。
No.1
- 回答日時:
// クラスの役割が不明瞭に思います。
// このようなプログラムではいかがでしょう。
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(); // 結果を表示
}
}
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
似たような質問が見つかりました
- Java java final 1 2022/06/10 22:49
- Java java 入力 3 4 3 出力 ABC DEFG HIJ このようなプログラムの書き方を教えてくだ 2 2022/07/15 14:18
- Java Java プログラム public class Main { public static void 3 2023/08/10 23:46
- Ruby 【JAVA】数字をひし形に出力するプログラムについて 2 2022/07/11 23:32
- Java javaでのプログラム(配列)について質問です. 2 2022/10/14 22:27
- C言語・C++・C# C言語 プログラミング 4 2022/05/22 11:53
- C言語・C++・C# 大量のデータを読み込んで表示する速度を改善したい 8 2023/05/07 13:29
- C言語・C++・C# C言語のエラーについて 2 2022/07/11 13:56
- Java 直し方について教えて頂きたいです。 4 2022/08/13 02:11
- Java Java 配列<選挙> 4 2023/07/31 15:07
関連するカテゴリからQ&Aを探す
おすすめ情報
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
変数を動的に利用するには?
-
NoSuchMethodErrorが解決できま...
-
中カッコ{}だけの記述について
-
【初心者です】javaで平均値を...
-
System.exit()の値を取得したい
-
randomで
-
ArrayList でスタックを
-
インタフェイス実装と抽象クラ...
-
getActualMaximum(Calendar.DAY...
-
Javaでlog4jを使ってログ出力を...
-
java 継承の問題で分からないと...
-
Socketの接続のタイムアウトを...
-
GetterとSetterをやったのに。
-
Java 最大公約数 gcd
-
コマンドライン引数の*(アフ...
-
プログラミングの問題です。大...
-
コンストラクタ,interface,abst...
-
元旦からの経過日数を求めたい
-
コンパイラのバグ?それとも
-
元旦からの日数を入力して、何...
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
プログラミングの問題です。大...
-
変数を動的に利用するには?
-
中カッコ{}だけの記述について
-
System.exit()の値を取得したい
-
Javaでlog4jを使ってログ出力を...
-
NoSuchMethodErrorが解決できま...
-
javaで特定の文字列から特定の...
-
Socketの接続のタイムアウトを...
-
Java プログラム public class ...
-
javaのプログラミングで作るRPG...
-
インタフェイス実装と抽象クラ...
-
コマンドライン引数の*(アフ...
-
【初心者です】javaで平均値を...
-
Javaで日本語の出力が文字化けする
-
(大至急)JavaでATMもどきを作成
-
コンストラクタの引数の中のnew?
-
Java 最大公約数 gcd
-
C# DatagridviewにExcelシート...
-
randomで
-
GetterとSetterをやったのに。
おすすめ情報