Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does it always print F in my else-if statement

I’m a beginner at java, So my goal was to determine the average of 3 user inputted scores using multiple classes and print whether if it is 60>n = F, 70>n = D 80>n = C, 90>n = B, 100>n = A, and 101>n = ? where n is the value of the 3 scores that was averaged. 1st class having the scanners and second class having the else-if, the inputted data and the computed average of them.

import java.util.Scanner;
public class MyClass {
    public static void main(String args[]) {
        Scanner myObj = new Scanner(System.in);
        
        System.out.println("Enter your Score on HTML: ");
        double Score1=myObj.nextDouble();
        System.out.println("Enter your Score on JAVA: ");
        double Score2=myObj.nextDouble();
        System.out.println("Enter your Score on C: ");
        double Score3=myObj.nextDouble();
        
        System.out.println("Total average: " + Score1 + Score2 + Score3);
        Second R = new Second(Score1,Score2,Score3);
        
        R.P();
    }
}


--------------------------------------------------------------

public class Second {
    double S1;
    double S2;
    double S3;
    double average;
    Second(double Score1, double Score2, double Score3)
{
    Score1 = S1;
    Score2 = S2;
    Score3 = S3;
    average = (Score1 + Score2 + Score3) /3;
}
// public char calcAverage();
// double average (Score1, Score2, Score3)

public void P(){
    if(average < 60){
        System.out.println("F");
        
    }else if(average < 70){
        System.out.println("D");
        
    }else if(average < 80){
        System.out.println("C");
        
    }else if(average < 90){
        System.out.println("B");
        
    }else if(average < 100){
        System.out.println("A");
        
    }else {
        System.out.println("?");
    }
}
}

What I am expecting is for it to print A-F depending on the computed average but it always return F regardless of how high the value was inputted.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

There’s an error in class Second constructor

    Second(double Score1, double Score2, double Score3)
{ 
    Score1 = S1; ### it should be S1 = Score1
    Score2 = S2; ### it should be S2 = Score2
    Score3 = S3; ### it should be S3 = Score3
    average = (Score1 + Score2 + Score3) /3;
}

because your S1,S2,S3 was initialized with zero it prints F

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading