The revolutionary project management tool is here! Plan visually with a single glance and make sure your projects get done.

Write a program that allows the user to a series of numerical grades and then calculates an average. Entering an integer of value 0 ends the input and produces the result. Use two separate functions (methods) to 1) input data and calculate the total of all grades entered and 2)calculate the average.

I keep getting errors, int grades, etc is not a statement. This program won't compile. I'm a beginner programmer and could use some help in cleaning this up and making this compile. I have to use a for statement. Please take a look at this and let me know what you think. Thanks for the help in advance....

I keep getting errors, int grades, etc is not a statement. This program won't compile. I'm a beginner programmer and could use some help in cleaning this up and making this compile. I have to use a for statement. Please take a look at this and let me know what you think. Thanks for the help in advance....

```
// File: AverageGrades.java
// Name: Clifford Albanese
// Project 2
// Date: March 28, 2009
import java.util.Scanner;
public class AverageGrades {
public static void main(String[] args){
scanner input = new Scanner ( System.in );
int double grade;
int double sum = 0;
int double gradeCounter = 0;
int double grade = 0;
// read the number of grades
System.out.println( "Enter the number of grades: ");
grade = input.nextInt();
//calculate the total of all grades entered
For ( gradeCounter = 1; grade != 0; i++ )
// read grade from the user
System.out.println( "Enter grade(s):");
grade = input.nextInt();
// add the grade to the sum, and add 1 to count
sum = sum + grade;
//calculate the average
int double average = 0;
average = (double) sum / gradeCounter;
System.out.println( "The grade average is " + average );
} // end Main
} // end Class
```

int double grade;

int double sum = 0;

int double gradeCounter = 0;

int double grade = 0;

----> you have to declare either int type or double. not both.

Work on the following program and ask more questions to complete your homework.

```
// File: AverageGrades.java
// Name: Clifford Albanese
// Project 2
// Date: March 28, 2009
import java.util.Scanner;
public class AverageGrades {
public static void main(String[] args){
Scanner input = new Scanner ( System.in );
double grade;
double sum = 0;
int gradeCounter = 0;
// read the number of grades
System.out.println( "Enter the number of grades: ");
grade = input.nextInt();
//calculate the total of all grades entered
for (gradeCounter = 1; grade != 0; gradeCounter++ ){
// read grade from the user
System.out.println( "Enter grade(s):");
grade = input.nextInt();
// add the grade to the sum, and add 1 to count
sum = sum + grade;
//calculate the average
double average = 0;
average = (double) sum / gradeCounter;
System.out.println( "The grade average is " + average );
System.out.println( "Enter the number of grades: ");
grade = input.nextInt();
}
} // end Main
} // end Class
```

for (gradeCounter = 1; grade != 0; gradeCounter++ ){

But it seems that I still enter a predefined value of grades. I want to omit this and enter as many grades as possible until a 0 is entered. Once this occurs, it should go to calculating the average.

Do I enter and "if-else" statement after the "for" statement?

if (grade ! = 0)

system out print ( Enter grade ");

else

System.out.println( "The grade average is " + average );

Please let me know if this is right. I know I'm close to having this totally finished. You have definitely helped me quite a bit.

Thanks again!

r/

Cliff

```
// File: AverageGrades.java
// Name: Clifford Albanese
// Project 2
// Date: March 28, 2009
import java.util.Scanner;
public class AverageGrades {
public static void main(String[] args){
Scanner input = new Scanner ( System.in );
double grade;
double sum = 0;
int gradeCounter = 0;
// read the number of grades
//System.out.println( "Enter the number of grades: ");
System.out.println( "Enter grade(s):");
grade = input.nextInt();
//calculate the total of all grades entered
while(grade!=0){
sum = sum + grade;
gradeCounter++;
// read grade from the user
System.out.println( "Enter grade. Type 0 to exit:");
grade = input.nextInt();
}
//calculate the average
double average = 0;
average = (double) sum / gradeCounter;
System.out.println( "The grade Sum is " + sum );
System.out.println( "The grade average is " + average );
} // end Main
} // end Class
```

Thanks again for looking at this with me.

r/

Cliff

import java.util.Scanner;

public class AverageGrades {

public static void main(String[] args){

Scanner input = new Scanner ( System.in );

double grade;

double sum = 0;

int gradeCounter = 0;

// Enter grades

System.out.println( "Enter grades (Grade of 0 causes average calculation: ");

grade = input.nextInt();

//calculate the total of all grades entered

for (gradeCounter = 1; grade != 0; gradeCounter++ ){

sum = sum + grade;

System.out.println( "Enter grades (Grade of 0 causes average calculation: ");

grade = input.nextInt();

//calculate the average

double average = 0;

average = (double) sum / gradeCounter;

System.out.println( "The grade average is " + average );

}

} // end Main

} // end Class

You need to compute the average at the end. i.e. after reading all the values of the grades. So move the average computation out of the 'for' loop.

Output:

Enter grades (Grade of 0 causes average calculation:

10

Enter grades (Grade of 0 causes average calculation:

20

Enter grades (Grade of 0 causes average calculation:

0

Counter :2

The grade sum is 30.0

The grade average is 15.0

```
import java.util.Scanner;
public class AverageGrades {
public static void main(String[] args){
Scanner input = new Scanner ( System.in );
double grade;
double sum = 0;
int gradeCounter = 0;
// Enter grades
System.out.println( "Enter grades (Grade of 0 causes average calculation: ");
grade = input.nextInt();
//calculate the total of all grades entered
for (gradeCounter = 1; grade != 0; gradeCounter++ ){
sum = sum + grade;
System.out.println( "Enter grades (Grade of 0 causes average calculation: ");
grade = input.nextInt();
}
gradeCounter--;
//calculate the average
double average = 0;
average = (double) sum / gradeCounter;
System.out.println( "Counter :" + gradeCounter );
System.out.println( "The grade sum is " + sum );
System.out.println( "The grade average is " + average );
} // end Main
} // end Class
```

Otherwise, everything works fine. Thanks so much!

r/

Cliff

Check this for how to use methods in java: http://www.otherwise.com/Lessons/MethodsCreationAndUse.html

public void enterGrades ( ) {

}

public void aveGrades ( ) {

}

Does public static void main (String[] args) { get removed? I somewhat get this but I'm unclear!

r/

Cliff

main method is required for the JVM to understand your code and start the execution. So don't remove main method.

I hope you understand everything and the actual posted problem is solved. All the Best :)

r/

Cliff

//////////////////////////

import java.util.Scanner;

public class AverageGrades {

public static void main(String[] args){

Scanner input = new Scanner ( System.in );

double grade = 0;

double sum = 0;

int gradeCounter = 0;

public void enterGrades () {

// Enter grades

System.out.println( "Enter grades (Grade of 0 causes average calculation: ");

grade = input.nextDouble();

}

public void aveGrades ( ) {

//calculate the total of all grades entered

for (gradeCounter = 1; grade != 0; gradeCounter++ )

sum = sum + grade;

System.out.println( "Enter grades (Grade of 0 causes average calculation: ");

grade = input.nextDouble();

}

//calculate the average

double average = 0;

average = (double) sum / gradeCounter;

System.out.println( "The grade average is " + average );

} // end Main

} // end Class

Errors.jpg

V/r

Cliff

All Courses

From novice to tech pro — start learning today.

i) public double enterGrades ()

ii) public double aveGrades (double sum )

From the main program I created the object of the AverageGrades and called these 2 methods from the main code.

Work on the following code then you will understand more. :)

Open in new window