tags:

views:

270

answers:

4

I'm trying to get the program to let me enter a users name and then a mark and display the average. This is what I've tried:

// Calculate Students Average of series of exam marks stored in an array
import uulib.GUI;

public class ExamMarks {

  public static void main(String[] args) {

    final int SIZE = 10;

    int[] marks = new int[SIZE];

    double total, average;

    String[] names = new String[]{""};


    // Read marks into array
    {
      String name;
      name = GUI.getString("Enter your name");
      GUI.show("Names"); 
    }

    for(int i=0; i<marks.length; i++) {
        marks[i] = GUI.getInt("Enter mark " + (i+1));
    }

    total = 0;
    // calculate average mark
    for(int i=0; i<marks.length; i++) {
        total = total + marks[i];
    }

    average = total/SIZE; 
    GUI.show("Average mark is " + average);
 }

}

I want it to ask for a name and then a mark ten times, and finally show the average. But it is only asking me for one name, after which it asks me for ten marks and gives me an average of these.

A: 

To address pstanton's criticism - you shopuld make it clearer what you mean by "I cannot get it to work":

  • what errors are you seeing?
  • is it compiling? Falling over at runtime? Giving you the wrong answer?

You don't seem to make use of the names[] array and you just display the String Names for no apparent reason. Is this what is broken?

oxbow_lakes
Guess thats ok - he want 'enter mark 1' being displayed on the GUI for the first mark which is index 0 in his array.
Andreas_D
Silly me - of course!
oxbow_lakes
A: 

import uulib.GUI; // Calculate Average of series of exam marks stored in an array

public class ExamMarks {

public static void main(String[] args) { final int SIZE = 10;

  int[] marks = new int[SIZE];
  double total, average;

  // Read marks into array
  for(int i=0; i<marks.length; i++)
  {
      marks[i] = GUI.getInt("Enter mark " + (i+1));
  }

  total = 0;
  // calculate average mark
  for(int i=0; i<marks.length; i++)
  {
      total = total + marks[i];
  }

  average = total/SIZE; 
  GUI.show("Average mark is " + average);  }

}

thats what I done to work out the average of ten results but im trying to get the user to enter their name and result

martin doherty
+1  A: 

Some hints and observations:

  1. I guess you want to enter 1 Student and 10 marks and calculate the average for that student. So you may not need the names array (you only get one name)
  2. name is only visible inside the block. If you need the name outside of that block, just remove the curly braces.
  3. for your average calculation you may try casting the integer values to double, like (double) total / (double) SIZE) (even though casting just one value should be enough)

For GUI usage problems - you import a custom library, bet you have to double check the manual on how to use it.

EDIT

Your comment changed it a bit ;) Usually you would now create a small class like Result that holds a studentName attribute and a mark attribute (for simplification: we assume all students have unique names, so we don't need a studentID here).

If you feel better solving the task without a class, we can emulate it with to arrays, one for names and one for marks.

The following code demonstrates the flow:

public void process() {
  final int SIZE = 10;
  String[] names = new String[SIZE];
  int[] marks = new int[SIZE];
  for(int i = 0; i < SIZE; i++) {
    names[i] = readStudentNameFromGUI();
    marks[i] = readStudentsMarkFromGUI();
  }
  double average = calculateAverage(marks);
}

Note: there are some things you could (should) do better in Java, but I think that's easy enough to get an understanding and get a solution.

Andreas_D
im looking to enter 10 students names and 10 students scores then the average.
martin doherty
thanks man, think i should use arrays also
martin doherty
A: 

Write a program that allows a lecturer to enter a list of students’ last names followed by their test scores. You may assume that there will never be more than 10 students in the class. (Hint – you should use two equal length arrays – one to store the student names and one to store the student scores. We can then retrieve/update any students name/score by using the same index to access each separate array).

that was the question

martin doherty
Thanks - you should add this information to the **question** (use "edit") and remove this answer.
peter.murray.rust