Results 1 to 5 of 5
  1. #1
    Johnathanrs is offline Member
    Join Date
    Jan 2014
    Posts
    2
    Rep Power
    0

    Default If-Else/Switch not outputing correctly

    Trying to learn switch statements. Can't figure out what I am doing wrong.

    Java Code:
    import java.util.Scanner;
    
    public class Ok1 {
    	public static void main(String args[]) {
    		
    		Scanner input = new Scanner(System.in);
    		
    		int grade;
    		
    		try {
    			System.out.println("Enter a grade: ");
    			grade = input.nextInt();
    		} finally {
    			input.close();
    		} //end try finally
    		
    		int calculate = grade; 
    		String letter;
    		switch (calculate) {
    		case 1: if (grade >= 90) {
    			letter = "A"; }
    		break;
    		case 2: if (grade >= 80) {
    			letter = "B"; }
    		break;
    		case 3: if (grade >= 70) {
    			letter = "C"; }
    		break;
    		case 4: if (grade <= 69) {
    			letter = "F"; }
    		break;
    		default: letter = "F";
    		break;
    		} //end switch
    	
    		System.out.println("Your grade is: " + letter);
    		System.out.println("You Scored: " + grade);
    	} //end main
    } //end Ok1
    When I run, it makes me enter letter = null and it wont output correctly.

  2. #2
    vasilenko93 is offline Member
    Join Date
    Jan 2014
    Location
    Citrus Heights, CA
    Posts
    8
    Rep Power
    0

    Default Re: If-Else/Switch not outputing correctly

    Why are you using a switch statement? If I enter lets say 80 for grade, that should be a 'B'. But your four switches are 1, 2, 3 and 4, and the default is "F". This means you always get Fs

  3. #3
    Johnathanrs is offline Member
    Join Date
    Jan 2014
    Posts
    2
    Rep Power
    0

    Default Re: If-Else/Switch not outputing correctly

    Learning purposes, trying to figure out how to get it to work with a switch. Got it to work like this:

    Java Code:
    import java.util.Scanner;
    
    public class Ok1 {
    	public static void main(String args[]) {
    		
    		Scanner input = new Scanner(System.in);
    		
    		int grade;
    		
    		try {
    			System.out.println("Enter a grade: ");
    			grade = input.nextInt();
    		} finally {
    			input.close();
    		} //end try finally
    		
    		char letter;
    		
    		if (grade >= 90) {
    			letter = 'A';
    		} else if (grade >= 80) {
    			letter = 'B';
    		} else if (grade >= 70) {
    				letter = 'C';
     			} else {
     				letter = 'F';
    		}
    		
    		switch (letter) {
    		case 'A': System.out.println("You got an A!"); 
    		break;
    		case 'B': System.out.println("You got an B!"); 
    		break;
    		case 'C': System.out.println("You got an C!");
    		break;
    		case 'F': System.out.println("You got an F!");
    		break;
    		} //end switch
    	
    	} //end main
    } //end Ok1
    If anyone has any recommondations to combine this using a better switch, please post. Thanks.
    Last edited by Johnathanrs; 01-27-2014 at 01:51 AM.

  4. #4
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Re: If-Else/Switch not outputing correctly

    The reason that it didn't work:

    You don't use brackets when you do your cases. Remember that.

    The brackets only go after the switch itself

    So, it has to be

    Java Code:
    switch (char or int) {
    case 'L': 
    do this;
    break;
    
    case 'D':
    do this;
    break;
    
    default: 
    do this;
    //no break needed
    }
    Link to a tutorial on switches: The switch Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,602
    Rep Power
    5

    Default Re: If-Else/Switch not outputing correctly

    Switch statements are primarily for discrete differences in values. For ranges, if/else is better. To do a range on a switch you would need to enumerate close to 40 cases, letting the default handle those who flunk.

    Regards,
    Jim
    Last edited by jim829; 01-27-2014 at 02:59 AM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. 0.01 doesnt add correctly
    By bioa10 in forum New To Java
    Replies: 3
    Last Post: 08-21-2013, 07:21 AM
  2. Replies: 9
    Last Post: 02-05-2012, 11:02 PM
  3. I can't get this to run correctly
    By LostinJavaLand in forum New To Java
    Replies: 4
    Last Post: 07-15-2010, 06:49 AM
  4. how to scale correctly ?
    By h9h in forum Java 2D
    Replies: 10
    Last Post: 10-29-2009, 07:06 AM

Posting Permissions

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