mysterious loop occuring when I run my program

I am new to programming and have been assigned a project to write a java program to calculate a students average grade, letter grade, and pass/fail using an array and multiple methods. I have the program working but for some reason when I run it a small portion of the first method loops and I'm not sure why. Below is my code with a copy of the output I am getting.

[code]

import java.io.*; //using to access the input stream class
import java.text.*; //using to access the decimal format class
import java.util.Scanner; //using to access Scanner class

public class gradecalc
{
//class declaration
private int s;
private double sm;
private double ave;

gradecalc(int s, double sm, double ave) //constructor
{
s = 0;
sm= 0;
ave = 0;
}

//method to calculate the students average
public static double gradeave(int size, double sum, double average)
{
//setting up keyboard input
Scanner keyboard = new Scanner(System.in);

System.out.println("Please enter the total number of student grades:");
size = keyboard.nextInt();

//creating grades array
double grades[] = new double[size];

System.out.println("
Enter Student Grades:");

sum = 0;

for (int i = 0; i < size; i++)
{
grades[i] = keyboard.nextDouble();
sum = sum + grades[i];
}
//I was instructed to add 5 points to the total score before calculating the average.
average = (sum+5) / size;

if (average >= 80)
System.out.println("
This student PASSED the class.");
else
System.out.println("
This student FAILED the class.");

return average;
}

//method to determine students letter grade
public static char lettergrade (char lg)
{
int s;
double sm, ave, gradeave;
char A,B,C,D,F;

s=0;
sm=0;
ave=0;

gradeave = gradeave(s, sm, ave);

if (gradeave>=90)
lg = 'A';
else if (gradeave>=80)
lg = 'B';
else if (gradeave>=70)
lg = 'C';
else if (gradeave>=60)
lg = 'D';
else
lg = 'F';

return lg;

}

public static void main (String[] args) //Main method
throws java.io.IOException
{
System.out.println("This program will calculate a student's average grade.
");

int s;
double sm, ave, gradeave;
char lg;

s=0;
sm=0;
ave=0;
lg='x';

DecimalFormat num = new DecimalFormat("###.#");

char lgrade = lettergrade(lg);

System.out.println("The average grade is " + num.format(gradeave(s,sm,ave)) + "%");
System.out.println("The student's letter grade = " + lgrade);

System.exit(0);
}
}



--------------------Configuration: --------------------
This program will calculate a student's average grade.

Please enter the total number of student grades:
3

Enter Student Grades:
90
80
70

This student PASSED the class.
Please enter the total number of student grades:
3

Enter Student Grades:
90
80
70

This student PASSED the class.
The average grade is 81.7%
The student's letter grade = B

Process completed.

[/code]

Comments

  • Hey,
    The prompting for input occurs when the method [color=Blue]gradeave(s, sm, ave)[/color] is invoked. That occurs 2 times in the code, on line 62 and line 97.

    regards, se52
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion