Howdy, Stranger!

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

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

mysterious loop occuring when I run my program

neon1701neon1701 Posts: 1Member
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

Sign In or Register to comment.