Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: switch case , check range

  1. #1
    Junior Member
    Join Date
    Oct 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default switch case , check range

    Hello guys,
    i am totally new to java.How do i check INT range for switch case: ?

    	int grade = 68;
    		switch (grade) {
    		case 100:
    		System. out.println( "You got an A. Great job!" );
    		break;
    		case 80:
    		System. out.println( "You got a B. Good work!");
    		break;
    		case ("60-79"):
    		System. out.println( "You got a C. What went wrong?" );
    		break;
    		default:
    		System. out.println( "You got an F. You’ll do well in Congress!" );
    		}
    }
    }


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: switch case , check range

    You dont.
    A switch case can only take constant values.

    What you want is probably an if-then-else statement.

  3. The Following User Says Thank You to Cornix For This Useful Post:

    GregBrannon (October 9th, 2014)

  4. #3
    Junior Member
    Join Date
    Sep 2014
    Posts
    21
    My Mood
    Cheeky
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: switch case , check range

    If you really want to torture yourself and anyone else looking at your code, you could do this:

    int grade = 68;
     
    switch (grade/10) {
    //stuff
        case 6:
            System.out.println("60-69");
            break;
    //stuff

    Even more !?what?!
    int grade = 68;
     
    switch (grade/20) {
    //stuff
        case 3:
            System.out.println("60-79");
            break;
    //stuff

    Just because you can, doesn't mean you should.

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: switch case , check range

    I agree, that's painful to look at, and no sane person should do that or suggest that anyone else do that.

  6. #5
    Junior Member
    Join Date
    Oct 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: switch case , check range

    You got an A. Great job!" );
    break;
    case 80:
    System. out.println( "You got a B. Good work!");
    break;
    case ("60-79"):
    System. out.println( "You got a C. What went wrong?" );
    break;
    default:
    aliraza8

Similar Threads

  1. switch case
    By sathirish in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 10th, 2014, 04:08 AM
  2. Switch case problem
    By pavani in forum Loops & Control Statements
    Replies: 4
    Last Post: August 11th, 2013, 09:34 PM
  3. switch case
    By viyyapu harsha in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 24th, 2013, 12:03 AM
  4. Need help with switch case
    By niko25 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 18th, 2012, 12:10 AM
  5. Case Switch Help?
    By xionyus in forum What's Wrong With My Code?
    Replies: 17
    Last Post: October 19th, 2011, 08:59 PM