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.
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: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");
}
}
}
the syntax information from the book makes no sense to me because all it shows is: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 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.Code:
switch (expression) {
case label1: {statements;break;}
case label2: {statements;break;}
...
...
case labelk: {statements;break;}
default: {statements;break;}
}

