There are five students, each student has 3 courses of results, from the keyboard input the above data (including student number, name, three lessons), calculated average results, the original data and calculated average scores are stored in the disk File "stud".
public class Example50 {
public static void Main (string[] args) {
Stud ();
}
public static void Stud () {
Scanner ss = new Scanner (system.in);
String[][] A = new string[5][6];
for (int i = 1; i < 6; i++) {
System.out.print ("Please enter the" + i + "Student number:");
A[i-1][0] = Ss.nextline ();
System.out.print ("Please enter the" + i + "Student's name:");
A[I-1][1] = Ss.nextline ();
for (int j = 1; j < 4; J + +) {
System.out.print ("Please enter the student's first" + j + "score:");
A[i-1][j + 1] = Ss.nextline ();
}
System.out.println ("\ n");
}
The following calculates the average score
float avg;
int sum;
for (int i = 0; i < 5; i++) {
sum = 0;
for (int j = 2; J < 5; J + +) {
sum = sum + integer.parseint (a[i][j]);
}
Avg = (float) sum/3;
A[I][5] = string.valueof (avg);
}
The following write disk file
String S1;
try {
File F = new file ("E:\\eclipse Workplace\\java Classic Algorithm \\src\\stud.txt");
if (f.exists ()) {
System.out.println ("file exists");
} else {
SYSTEM.OUT.PRINTLN ("File does not exist, creating file");
F.createnewfile ();//does not exist create
}
BufferedWriter output = new BufferedWriter (new FileWriter (f));
for (int i = 0; i < 5; i++) {
for (int j = 0; J < 6; J + +) {
S1 = a[i][j] + "\ r \ n";
Output.write (S1);
}
System.out.println ();
}
Output.close ();
SYSTEM.OUT.PRINTLN ("The data has been written to the e-drive file Stud! ");
} catch (Exception e) {
E.printstacktrace ();
}
}
}
Java Classic Programming question 50-way 50