Reputation: 21
I'm trying to divide variables from pointsearned and creditsearned method in the pointsaverage method but it gives "Your grade point average isNaN" when i run it , how do i fix this ?(I'm a beginner)
public class JavaApplication40 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) {
double credits = 0;
double Points = 0;
double average = 0;
IDnumber();
CreditsEarned(credits);
PointsEarned(Points);
System.out.println("Your grade point average is" + PointAverag(average, Points, credits));
}
public static void IDnumber(){
String docNuMBER;
System.out.println("Enter your student ID number ");
docNuMBER = keyboard.nextLine();
}
public static double CreditsEarned( double credits){
double NumCreditsEarned;
System.out.println("Enter your Credit hours earned");
NumCreditsEarned = keyboard.nextDouble();
return NumCreditsEarned;
}
public static double PointsEarned(double points){
double NumberOpoints;
System.out.println("Enter your credit points");
NumberOpoints = keyboard.nextDouble();
return NumberOpoints;
}
public static double PointAverag(double grade , double NumberOpoints ,
double NumCreditsEarned) {
double average ;
average = NumberOpoints/NumberOpoints;
return average ;
Upvotes: 2
Views: 72
Reputation: 158
Here's what's happening in your program:
You set 'credits' and 'points' to 0. These two variables have not been modified when you get to pass them to 'pointAverag'
System.out.println("Your grade point average is" + PointAverag(average, Points, credits));
this line, literally, does this:
System.out.println("Your grade point average is" + PointAverag(0, 0, 0));
And eventually leads to:
average = 0/0
in 'PointAverag' which is NAN when stored in a double.
Watch out for this line:
average = NumberOpoints/NumberOpoints;
it has logical mistake in it. It will always store either 1 or NAN.
As mentionned in the comments, you need to update those variables, 'credits' and 'points', by storing the returned value of your methods 'PointsEarned' and 'CreditsEarned' in them:
CreditsEarned(credits);
PointsEarned(Points);
Becomes
credits = CreditsEarned(credits);
points = PointsEarned(Points);
Hope this helps.
Upvotes: 2
Reputation: 4467
Write:
System.out.println("Your grade point average is" + PointAverag(IDnumber(), PointsEarned(Points), CreditsEarned(credits)));
instead.
If you don't use a returned value, it gets discarded.
Upvotes: 1