The Artima Developer Community
Sponsored Link

Java Answers Forum
Read input

2 replies on 1 page. Most recent reply: Apr 30, 2002 11:29 AM by Thang Nguyen

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Jeannie Kwa

Posts: 4
Nickname: kaw
Registered: Apr, 2002

Read input Posted: Apr 29, 2002 7:38 AM
Reply to this message Reply
Advertisement
My program needs to read input for 3 each subject marks, and name of student.
1) Calculate each student average score, overall highest average score and lowest average score for 15 students.

Even so, i still have errors on this program. Kindly help me on this matter, appreciate your assistance for it.

//run this program as GoodLuck.java
import java.io.*;
import java.lang.*;
class GoodLuckk
{
public static void main(String args[]) throws IOException
{
int []students = new int[15];
String Studname;
int mark1, mark2, mark3;

BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter your Name:");
System.out.println("Enter your mark1");
System.out.println("Enter your mark2");
System.out.println("Enter your mark3");

for(int i =0; i <=14; i++)
{
Studname = input.readLine();
int mark1= input.readInt();
int mark2= input.readInt();
int mark3= input.readInt();
students = Integer.parseInt(Studname);
students = Integer.parseInt(mark1);
students = Integer.parseInt(mark2);
students = Integer.parseInt(mark3);

};

double HighestAvg = -1.0;
double LowestAvg = 101.0;



for (int i = 0; i < students.length; i++)

for (int i = 0; i < students.length; i++)
{
double avg = (students .mark1 + students .mark2 +
students .mark3) / 3.0;

if (avg > highestAvg)
highestAvg = avg;

if (avg < lowestAvg)
lowestAvg = avg;

System.out.print (students .name + " " +
students .mark1+ " " +
students .mark2 + " " +
students .mark3 + " " + avg + " ");

if (avg < 40)
System.out.println ("FAIL");
else
if (avg < 60)
System.out.println ("PASS");
else
if (avg < 70)
System.out.println ("CREDIT");
else
System.out.println ("DISTINCTION");
}

System.out.println ();

System.out.println ("Highest Average = " + HighestAvg);
System.out.println ("Lowest Average = " + LowestAvg);
}
}


Thang Nguyen

Posts: 18
Nickname: thang
Registered: Apr, 2002

Re: Read input Posted: Apr 30, 2002 11:19 AM
Reply to this message Reply
/*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 :~

Thang Nguyen

Posts: 18
Nickname: thang
Registered: Apr, 2002

Re: Read input Posted: Apr 30, 2002 11:29 AM
Reply to this message Reply
Sorry...
Math.round(); does not round a decimal number to 2-decimal number but cuts off the # after the decimal point.

Use NumberFormat for rounding to n-decimal number.

Flat View: This topic has 2 replies on 1 page
Topic: Java AWT Previous Topic   Next Topic Topic: converting String to byte and Vice versa

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use