Results 1 to 8 of 8
  1. #1
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default switch statements

    I am having a problem understanding switch statements. What I am confused about is where the logic goes. For instance, I created the below problem using if statements. The program verifies whether or not it is true that numbers whose sum of digits is divisible by 3 represent numbers divisible by 3.

    Java Code:
    import java.util.Scanner;
    
    public class ProgrammingProblemTwo {
    
        public static void main(String[] args) {
    
        	//Create new scanner
        	Scanner input = new Scanner(System.in);
    
        	//Ask user to input a five digit number
        	System.out.println("Please input a five digit number");
        	//create variable number
        	int number = input.nextInt();
    
        	//create variable first digit in order to isolate the first digit
        	int firstDigit = number / 10000;
    
        	//Create variables a and secondDigit in order to isolate the second digit
        	int a = number / 1000;
        	int secondDigit = a % 10;
    
        	//Create variables b and thirdDigit in order to isolate the third digit
        	int b = number / 100;
        	int thirdDigit = b % 10;
    
        	//Create variables c and fourthDigit in order to isolate the fourth digit
        	int c = number / 10;
        	int fourthDigit = c % 10;
    
        	////Create variable fifthDigit in order to isolate the fifth digit
        	int fifthDigit = number % 10;
    
        	int sum = firstDigit + secondDigit + thirdDigit + fourthDigit +
        		fifthDigit;
    
        	if(number % 3 == 0 && sum % 3 == 0){
        		System.out.println("Both " + number + " and sum " + sum +
        			" are divisible by 3");
        	}
        		if(number % 3 != 0 && sum % 3 != 0){
        			System.out.println("Both " + number + " and sum " + sum +
        			" are indivisible by 3");
        		}
        			if(number % 3 == 0 && sum % 3 != 0 || number % 3 != 0 && sum % 3 == 0){
        				System.out.println("The famous statement is wrong");
        			}
       }
    }
    Where I get confused about is where the logic is at in switch statements. For example, here is the code I have so far using the switch statement example in the book:

    Java Code:
    import java.util.Scanner;
    
    public class ProgrammingProblemTwo {
    
        public static void main(String[] args) {
    
        	//Create new scanner
        	Scanner input = new Scanner(System.in);
    
        	//Ask user to input a five digit number
        	System.out.println("Please input a five digit number");
        	//create variable number
        	int number = input.nextInt();
    
        	//create variable first digit in order to isolate the first digit
        	int firstDigit = number / 10000;
    
        	//Create variables a and secondDigit in order to isolate the second digit
        	int a = number / 1000;
        	int secondDigit = a % 10;
    
        	//Create variables b and thirdDigit in order to isolate the third digit
        	int b = number / 100;
        	int thirdDigit = b % 10;
    
        	//Create variables c and fourthDigit in order to isolate the fourth digit
        	int c = number / 10;
        	int fourthDigit = c % 10;
    
        	////Create variable fifthDigit in order to isolate the fifth digit
        	int fifthDigit = number % 10;
    
        	int sum = firstDigit + secondDigit + thirdDigit + fourthDigit +
        		fifthDigit;
    
    	switch (number){
    
        	        case 1: {System.out.println("Both " + number + " and sum " + sum +
        					" are divisible by 3"); break;}
        		case 2: {System.out.println("Both " + number + " and sum " + sum +
        					" are indivisible by 3"); break;}
        		case 3: {System.out.println("The famous statement is wrong");break;}
    		}
        }
    }
    the syntax information from the book makes no sense to me because all it shows is:

    Java Code:
    switch (expression) {
    
         case label1: {statements;break;}
         case label2: {statements;break;}
         ...
         ...
         case labelk: {statements;break;}
         default: {statements;break;}
    }
    The book example shows that the statements section is where you add the System.out.println information. It doesn't explain where the logic is at.

  2. #2
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Think of switch statements as easier if/else ladders:

    Java Code:
    swith(value) {
        case ifValueEqualsThis:
            runThisCode();
            break; //if you don't put break here, all code below this will be run too.
        case ifValueEqualsThat:
            runThatCode();
            break;
        ....
        default:
            nothingElseFits();
    }

  3. #3
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default

    Quote Originally Posted by ra4king View Post
    Think of switch statements as easier if/else ladders:

    Java Code:
    swith(value) {
        case ifValueEqualsThis:
            runThisCode();
            break; //if you don't put break here, all code below this will be run too.
        case ifValueEqualsThat:
            runThatCode();
            break;
        ....
        default:
            nothingElseFits();
    }
    So the logic is part of the case. OK. What if you have two values, such as in this case I have number and sum. I attempted to cut and paste my logic into the case but I get "incompatible types" when I compile it.

    Java Code:
    switch (sum){
    
        		case number % 3 == 0 && sum % 3 == 0: {System.out.println(
        			"Both " + number + " and sum " + sum + " are divisible by 3"); break;}
        		case number % 3 != 0 && sum % 3 != 0: {System.out.println(
        			"Both " + number + " and sum " + sum + " are indivisible by 3"); break;}
        		case number % 3 == 0 && sum % 3 != 0 || number % 3 != 0 && sum % 3 == 0:
        			{System.out.println("The famous statement is wrong");break;}
    Please excuse my thick head.

  4. #4
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Now you're treating it as a boolean acceptor.
    Whenever it runs over each "case" it does if(sum == caseValue).

  5. #5
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default

    Quote Originally Posted by ra4king View Post
    Now you're treating it as a boolean acceptor.
    Whenever it runs over each "case" it does if(sum == caseValue).
    OK, I get it now. It would appear that I can't use a switch statement for this particular problem because the program has to print either "Both number and sum are divisible by 3," "Both n and sum are indivisible by 3," or "The famous statement is wrong." Therefore I have to be able to compare the sum and number variables.

  6. #6
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Whatever works and fits your program best :)

  7. #7
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default

    Quote Originally Posted by ra4king View Post
    Whatever works and fits your program best :)
    Thank you very much for your help. Even though I cannot use a switch statement in this particular program, you helped me understand how to use it.

  8. #8
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

Similar Threads

  1. Using a switch/case with my if statements
    By coding in forum New To Java
    Replies: 2
    Last Post: 03-07-2011, 08:01 AM
  2. Help with if else statements
    By np2392 in forum New To Java
    Replies: 2
    Last Post: 09-24-2010, 01:25 AM
  3. if else statements
    By sweetpea123 in forum New To Java
    Replies: 4
    Last Post: 04-12-2010, 07:02 PM
  4. Replies: 2
    Last Post: 11-22-2008, 05:30 AM
  5. Help with if else statements
    By zoe in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 07:56 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
  •