joel
joel

Reputation: 1

Java cannot be resolved to a variable issue

new to Java and coding. I tried initializing it outside of the if-else statements to see if that would fix the issue but that doesn't work either as it then claims the variables have no values. When I try to print currentScore, I get the error message that currentScore cannot be resolved to a variable. Here is my current code.

import java.util.Scanner;

public class GradeCalculator {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Please input the letter grade you would like to receive:");
        char desiredGrade = keyboard.next().charAt(0);

        System.out.println("Please put either 'Yes' or 'No' whether or not you know the score for each graded item");
        System.out.println("Do you know the grade to Exam 1?");
        String doesUserKnowExamOne = keyboard.next();


        if (doesUserKnowExamOne.equalsIgnoreCase("Yes")) {
            System.out.println("Please input the grade for Exam 1");
            double examOneGrade = keyboard.nextDouble();
            System.out.println("Please input the weight for Exam 1");
            int examOneWeight = keyboard.nextInt();
            System.out.println("Do you know the grade to Exam 2?");
            String doesUserKnowExamTwo = keyboard.next();
            double currentScore = (examOneGrade * examOneWeight) / (examOneWeight);

            if (doesUserKnowExamTwo.equalsIgnoreCase("Yes")) {
                System.out.println("Please input the grade for Exam 2");
                double examTwoGrade = keyboard.nextDouble();
                System.out.println("Please input the weight for Exam 2");
                int examTwoWeight = keyboard.nextInt();
                System.out.println("Do you know the grade to Final Exam?");
                String doesUserKnowFinal = keyboard.next();
                currentScore = (examOneGrade * examOneWeight) + (examTwoGrade * examTwoWeight) / (examOneWeight + examTwoWeight);


                if (doesUserKnowFinal.equalsIgnoreCase("Yes")) {
                    System.out.println("Please input the grade for Final Exam");
                    double finalExamGrade = keyboard.nextDouble();
                    System.out.println("Please input the weight for Final Exam");
                    int finalExamWeight = keyboard.nextInt();
                    currentScore = (examOneGrade * examOneWeight) + (examTwoGrade * examTwoWeight) + (finalExamGrade + finalExamWeight) / (examOneWeight + examTwoWeight + finalExamWeight);

                } else {
                    System.out.println("Final Exam not taken yet");
                }

            } else {
                System.out.println("Exam Two and Final Exam not taken yet");
            }
        } else {
            System.out.println("Exam One, Exam Two, and Final Exam not taken yet");
        }

        System.out.println(currentScore);
    }
}

Upvotes: 0

Views: 46

Answers (1)

jsedano
jsedano

Reputation: 4216

the problem is that you're declaring currentScore variable inside the scope of the first if and trying to read it outside said scope.

Try declaring:

double currentScore = 0;

above your first if and then instead of declaring, just do:

currentScore = (examOneGrade * examOneWeight)/(examOneWeight);

inside the first if

Upvotes: 2

Related Questions