Thang Nguyen
Posts: 18
Nickname: thang
Registered: Apr, 2002
|
|
Re: Read input
|
Posted: Apr 30, 2002 11:19 AM
|
|
/*I don't understand you needs very much but here is my simple code, you can try it out and see if it fixes your problem. NOTE: To test the program, I try only 3 students, but you can increase this number as many as you want (replace number 3 in the arrays: name, mark1, mark2, mark3, avg...). You can round the highest and lowest mark to 2-decimal numbers by using Math.round(..); And maybe some modifications at FAIL, PASS, CREDIT to meet your ideas.
Hope this helps you out a bit. If it stil does not work, email me at intelteq@hotmail.com - Thang */
import java.io.*;
public class Class1 { public static void main(String args[]) throws IOException { String []name = new String [3]; //student's name double [] mark1 = new double[3]; //student's mark double [] mark2 = new double[3]; double [] mark3 = new double[3]; BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
for(int a =0; a <name.length; a++) { System.out.println("\nEnter data for student" + (a+1)); System.out.print("Enter Name:\t"); name[a] = input.readLine(); System.out.print("Enter mark1\t"); mark1[a] = new Double(input.readLine()).doubleValue(); System.out.print("Enter mark2:\t"); mark2[a] = new Double(input.readLine()).doubleValue(); System.out.print("Enter mark3:\t"); mark3[a] = new Double(input.readLine()).doubleValue(); } double highestAvg, lowestAvg; double avg[] = new double[3];
//calculate mark average for (int a = 0; a < name.length; a++) { avg[a] = (mark1[a] + mark2[a] + mark3[a]) / 3; } //Find highest average highestAvg = avg[0]; for (int a = 1; a < name.length; a++) { if (highestAvg < avg[a]) highestAvg = avg[a]; }
//find lowest average lowestAvg = avg[0]; for (int a = 1; a < name.length; a++) { if (lowestAvg > avg[a]) lowestAvg = avg[a]; }
//print output for (int a = 0; a< name.length; a++) { System.out.println ("\nMark average of student " + name[a] + "(# " +(a+1) +")"); System.out.println("Mark1 = " + mark1[a]); System.out.println("Mark2 = " + mark2[a]); System.out.println("Mark3 = " + mark3[a]); System.out.println("Average =" + avg[a]); if (avg[a] < 50) System.out.println ("FAIL"); else if (avg[a] >=50 && avg[a] < 70) System.out.println ("PASS"); else if (avg[a] >= 70 ) System.out.println ("CREDIT"); else System.out.println ("DISTINCTION"); } System.out.println("\nHighest mark in class: " + highestAvg); System.out.println("Lowest mark in class: " + lowestAvg); System.in.read(); } } ///END of Class :~
|
|