• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to store ten scores in an array?

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I hope someone can help me. I had to do the following assignment( i have most of it done just cant finish it off, question and source code below).

Question?
Write a Java program that asks the user to input the scores, as a percentage( e.g 87.4), of 10 students. The scores entered must be stored ina n array.

The programme must determine:

The lowest score and its equivalent grade (ie A,B,etc)
The highest " " " " "
The average score and its "

The bit i cant do is tie in the equivalent grade with the lowest highest and average score.

Code:

//Arrayofscores.java
//This programme asks the user to enter 5 exam scores and store them in an array
import java.text.*;

public class Arrayofscores
{
public static void main(String args[])
{
int [] scores = new int[10];
int smallest, highest,temp,total=0;
double average =0.0;

//ask the user to enter 10 scores
for (int i = 0;i<= scores.length-1;i++)
{
System.out.print("\n\nEnter Score " + (i+1) + ": ");
scores[i] = UserInput.getInt();
}

//find the lowest score
smallest = scores[0];

for (int i = 1; i <= scores.length-1;i++)
if (scores[i] < smallest)
smallest = scores[i];

System.out.println("\nThe lowest score is : " + smallest);

//find the highest score
highest = scores[0];

for (int i = 1; i <= scores.length-1;i++)
if (scores[i] > highest)
highest = scores[i];

System.out.println("\nThe highest score is : " + highest);

//find the average score
for (int i = 0; i<=scores.length-1;i++)
total = total + scores[i];

average = total/10.0;
System.out.println("\nThe average score is : " + average);

}

}


Any suggestions would be greatly appreciated!!
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
dave hen
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks,

I kinda already know the code if else if but what im not too sure about is where i actually tie the if/else into the code.

At the moment with the code i have, it will execute- 1 highest mark is 2- lowest mark is and average mark. what im trying to get it to say is
the highest mark is ? This is a ? grade
the lowest mark is? This is a ? grade

How do i do this,i dont think by just inserting the code above will it execute what i want.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would create a method name getGrade() that takes an int (or float), and returns a string.

put the code Joanne hinted at in the method. then, you could do

 
dave hen
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok,this what i came up with but still a little unsure and going wrong somewhere:

code:

public static float getGrade()
{
float grade;

if (score > 90)
grade = "A";
else if (score > 75)
grade = "B";

System.out.println("\nThis is a " + getGrade(highest) + " grade.");

return grade;
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hmmm...

I think you need to slow down a little. What you wrote here will cause some problems.

in your 'getGrade' method, you print some stuff - that's fine. but... you also call a funtion - the getGrade() one. In other words, the method calls itself. So, the method gets called the first time, and then calls itself.

THAT call starts going, and calls itself again.
THAT call starts going, and calls itself again.
THAT call starts going, and calls itself again.
THAT call starts going, and calls itself again...

you see the problem?
 
dave hen
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks fred for your reply but im sorry im lost,looked over my notes and i just cant get my head around writing methods besides main method,so fustrating. what exactly should the grade method code look like.
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by fred rosenberger:
hmmm...

I think you need to slow down a little. What you wrote here will cause some problems.

in your 'getGrade' method, you print some stuff - that's fine. but... you also call a funtion - the getGrade() one. In other words, the method calls itself. So, the method gets called the first time, and then calls itself.

THAT call starts going, and calls itself again.
THAT call starts going, and calls itself again.
THAT call starts going, and calls itself again.
THAT call starts going, and calls itself again...

you see the problem?




public static float getGrade()
{
float grade;

if (score > 90)
grade = "A";
else if (score > 75)
grade = "B";

System.out.println("\nThis is a " + getGrade(highest) + " grade.");



getGrade() and getGrade(int) are two different methods. No recursion here.
 
dave hen
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok so what exactly should it look like,if this isnt the right code,can someone give me the right code.


[ March 05, 2008: Message edited by: dave hen ]
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by dave hen:
ok so what exactly should it look like,if this isnt the right code,can someone give me the right code.



[ March 05, 2008: Message edited by: dave hen ]



You don't need to call the getGrade() method in your print statement. You've just set a variable (grade) in your if/else statement. Use this variable in your print statement
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic