mardi 29 septembre 2015

Why is there a "deadbranch" in my code?

below my code was working fine until my last if-else. It appears I've done something wrong with my boolean variables canGraduate and onProbation. Perhaps I'm reassigning them incorrectly in the prior if-else statements. The deadbranch occurs at the else half of my last if-else. Thank you for your time.

package lab5;
import java.util.Scanner;
public class Lab5 {

public static void main(String[] args) {

    //creates scanner object
    Scanner scanner = new Scanner(System.in);

    //PART II
    //creating variables
    double gpa;
    int totalCreditsTaken;
    int mathScienceCredits;
    int liberalArtsCredits;
    int electiveCredits;
    boolean canGraduate = true;
    boolean onProbation = false;

    //prompts user for imput
    System.out.println("What is your GPA?");
        gpa = scanner.nextDouble();
    System.out.println("What's the total amount of credits you've taken?");
        totalCreditsTaken = scanner.nextInt();
    System.out.println("How many math and science credits have you taken?");
        mathScienceCredits = scanner.nextInt();
    System.out.println("How many liberal arts credits have you taken?");
        liberalArtsCredits = scanner.nextInt();
    System.out.println("How many elective credits have you taken?");
        electiveCredits = scanner.nextInt();

    //creates first "if" statment to determine if GPA is high enough to be on track or on probation 
    if (gpa < 2.0){
        System.out.println("You're on academic probation.");
        onProbation = true;
    }

    //PART III
    //creates a conditional to see if there's enough credits to graduate 
    if (totalCreditsTaken < 40 ){
        System.out.println("You need more credit(s) to graduate.");
        canGraduate = false;
    }
    else{
        System.out.println("Examining credit breakdown...");
        canGraduate = true;
    }

    //PART VI
    //Nested if-else if-else to determine if the student qualifies for BA or BS
    if ((mathScienceCredits >= 9) && (electiveCredits >= 10)){
        System.out.println("You qualify for a BS degree.");
        canGraduate = true;
    }
    else if ((liberalArtsCredits >= 9) && (electiveCredits >= 10)){
        System.out.println("You qualify for a BA degree.");
        canGraduate = true;
    }
    else{
        System.out.println("You currently don't meet the degree requirments.");
        canGraduate = false;
    }

    //PART V
    //Uses an if statement to either congradulate the student or tell the student to take more classes
    if ((onProbation = true) || (canGraduate = false)){
        System.out.println("You don't qualify to graduate.");
    }
    else{
        System.out.println("Congradualations you qualify to graduate.");
    }

}
}

Aucun commentaire:

Enregistrer un commentaire