Results 1 to 3 of 3
  1. #1
    Thammer is offline Member
    Join Date
    Sep 2014
    Posts
    47
    Rep Power
    0

    Default if-else. How to not execute both statements.

    Hi, I am making a program, where the user answers 3 questions and then I add the number of correct answers in to the (int) numberofcorrect variable, then I want to print the results, and no matter how many correct, the program executes first the correct if statement, then the else statement.
    Eg: I have 2 correct it will print:
    "Grade B, 2 of 3 "
    "You failed the test"

    Why does it do that? How can I change my code so the else statement dosent print if one of the if statments is allready printed?
    I want to know how to not execute the else statement, if one of the if statements have allready been executed.

    Below is my current code for this problem:

    if (numberofcorrect == 3){
    System.out.println("Grade A, full score");}
    if (numberofcorrect == 2){
    System.out.println("Grade B, 2 of 3 ");}
    if (numberofcorrect ==1) {
    System.out.println("Grade C, 1 of 3");}
    else {
    System.out.println("You failed the test"); }

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,993
    Rep Power
    9

    Default Re: if-else. How to not execute both statements.

    If you have multiple tests, you want to use "else if" for the second and rest of the tests (and an else in the final test, if you need it). Maybe some proper indenting will help you see it:
    Java Code:
    if (numberofcorrect == 3) {
       System.out.println("Grade A, full score");
    }
    
    if (numberofcorrect == 2) {
       System.out.println("Grade B, 2 of 3 ");
    }
    
    if (numberofcorrect ==1) {
       System.out.println("Grade C, 1 of 3");
    }
    else {
       System.out.println("You failed the test"); 
    }
    Edit: and no, I did not leave out the "else if" part in my example. I merely try to show that proper formatting will improve the chances of finding this "bug".
    Last edited by SurfMan; 10-04-2014 at 03:34 PM.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    Thammer is offline Member
    Join Date
    Sep 2014
    Posts
    47
    Rep Power
    0

    Default Re: if-else. How to not execute both statements.

    Quote Originally Posted by SurfMan View Post
    If you have multiple tests, you want to use "else if" for the second and rest of the tests (and an else in the final test, if you need it). Maybe some proper indenting will help you see it:
    Java Code:
    if (numberofcorrect == 3) {
       System.out.println("Grade A, full score");
    }
    
    if (numberofcorrect == 2) {
       System.out.println("Grade B, 2 of 3 ");
    }
    
    if (numberofcorrect ==1) {
       System.out.println("Grade C, 1 of 3");
    }
    else {
       System.out.println("You failed the test"); 
    }
    Edit: and no, I did not leave out the "else if" part in my example. I merely try to show that proper formatting will improve the chances of finding this "bug".
    Thank you for taking your time to answer.
    It worked perfectly now :)

Similar Threads

  1. Replies: 3
    Last Post: 10-24-2011, 01:05 PM
  2. How to execute jsp
    By Ravidasineni1234 in forum Java Software
    Replies: 3
    Last Post: 05-07-2011, 02:01 PM
  3. Replies: 0
    Last Post: 04-01-2008, 11:41 AM
  4. Replies: 0
    Last Post: 09-28-2007, 01:59 PM
  5. why does not execute this?
    By aleplgr in forum New To Java
    Replies: 6
    Last Post: 08-01-2007, 12:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •