![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Newbie
Join Date: Apr 2006
Posts: 7
Rep Power: 0
![]() |
Need help setting up this array
I'm trying to display an average of grades.
I don't know what I'm doing wrong but I'm trying to set up: - a BufferReader - float grades [] = new float [variable]; - use 3 'for loops' one loop to put in grades while asking user to enter grades, a loop to get data inside array and compute average, and a loop to output grades here is what I have so far: import javax.swing.JOptionPane;
// Computes the average of a series of scores
public class averaginggrades
{
// the array that holds the scores
private static int[] grades;
public static void main(String[] args)
{
// Prompt user to enter number of grades
String input = JOptionPane.showInputDialog("Enter number of grades:");
int numberOfgrades = Integer.parseInt(input);
// initialize array
grades = new int[numberOfgrades];
// Prompt user to enter grades and store them in an array
for (int i = 0; i < grades.length; i++)
{
input = JOptionPane.showInputDialog("Enter grade #" + (i + 1) + ": ");
grades[i] = Integer.parseInt(input);
System.out.println("grades #" + (i + 1) + " = " + grades[i]);
}
// Compute sum of scores
int sum = 0;
for (int i = 0; i < grades.length; i++)
{
sum += grades[i];
}
int largest = grades[0];
for (int i = 0; i < grades.length; i++)
{
if (grades[i] > largest)
{
largest = grades[i];
}
}
int smallest = grades[0];
for (int i = 1; i < grades.length; i++)
{
if(grades[i] < smallest)
{
smallest = grades[i];
}
}
// Display average grades
System.out.println("grade average = " + sum / grades.length);
// output the array
for(int x = 0; x < grades.length; x++)
{
System.out.print(grades[x] + " ");
}
System.out.println();
}
} |
|
|
|
|
|
#2 |
|
Sexy Programmer
|
[PHP]
//used to get average of grades public int Average() { int sumOfGrades = 0; for (int i = 0; i < grades.length; i++) { sumOfGrades += grades[i]; } //you can change the varible type, I am just using int for an example int average = (sum / grades.length); return average; } //used to output the grades public void showGrades() { for (int i = 0; i < grades.length; i++) System.out.println("Grade #" + i + 1 + " = " + grades[i]); } [/PHP] and for the loop to put in grades while asking user to enter grades, it looks like it's in your program already. I hope this helps.
__________________
I would love to change the world, but they won't give me the source code! |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Apr 2006
Posts: 7
Rep Power: 0
![]() |
where in my code do I add what you put?
and am I suppose to delete something to add what you have? |
|
|
|
|
|
#4 |
|
Sexy Programmer
|
[PHP]
import javax.swing.*; // Computes the average of a series of scores public class averaginggrades { // the array that holds the scores private static int[] grades; public static void main(String[] args) { // Prompt user to enter number of grades String input = JOptionPane.showInputDialog("Enter number of grades:"); int numberOfgrades = Integer.parseInt(input); // initialize array grades = new int[numberOfgrades]; // Prompt user to enter grades and store them in an array for (int i = 0; i < grades.length; i++) { input = JOptionPane.showInputDialog("Enter grade #" + (i + 1) + ": "); grades[i] = Integer.parseInt(input); System.out.println("grades #" + (i + 1) + " = " + grades[i]); } // Compute sum of scores int sum = 0; for (int i = 0; i < grades.length; i++) { sum += grades[i]; } int largest = grades[0]; for (int i = 0; i < grades.length; i++) { if (grades[i] > largest) largest = grades[i]; } int smallest = grades[0]; for (int i = 1; i < grades.length; i++) { if(grades[i] < smallest) smallest = grades[i]; } // Display average grades System.out.println("grade average = " + Average()); // outputs the grades showGrades() } //used to get average of grades public static int Average() { int sumOfGrades = 0; for (int i = 0; i < grades.length; i++) sumOfGrades += grades[i]; //you can change the varible type, I am just using int for an example int average = (sum / grades.length); return average; } //used to output the grades public static void showGrades() { for (int i = 0; i < grades.length; i++) System.out.println("Grade #" + i + 1 + " = " + grades[i]); } } [/PHP] This should be it. The whole code.
__________________
I would love to change the world, but they won't give me the source code! |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Apr 2006
Posts: 7
Rep Power: 0
![]() |
here's the error I got when I tried to compile it:
C:\Documents and Settings\Desktop\averaginggrades.java:48: ';' expected } ^ 1 error Tool completed with exit code 1 |
|
|
|
|
|
#6 |
|
Sexy Programmer
|
add a ";" to showGrades(), lol
__________________
I would love to change the world, but they won't give me the source code! |
|
|
|
|
|
#7 |
|
Newbie
Join Date: Apr 2006
Posts: 7
Rep Power: 0
![]() |
lol ok I did that and now this comes up:
C:\Documents and Settings\Desktop\averaginggrades.java:56: cannot find symbol symbol : variable sum location: class averaginggrades int average = (sum / grades.length); ^ 1 error Tool completed with exit code 1 |
|
|
|
|
|
#8 |
|
Newbie
Join Date: Apr 2006
Posts: 7
Rep Power: 0
![]() |
the up arrow is actually pointing under the "s" in sum
|
|
|
|
|
|
#9 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
According to the error message, it can't find the sum variable. So figure out what's going wrong. In that function, the variable doesn't exist. However, there is a sumOfGrades variable, and I'd bet that's what you're looking for.
You gotta try and do stuff yourself. Problem solving is the fun part of coding. |
|
|
|
|
|
#10 | |
|
Newbie
Join Date: Apr 2006
Posts: 7
Rep Power: 0
![]() |
Quote:
I've been trying to figure this out all day that's why I came here for help |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|