I want to use switch statements in my program. I've got my code working already but want to use switch statements. How do I do it? Here is what i got so far. import java.util.Scanner; public class ReviewApplication { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); double sum = 0; double difference = 0; double product = 0; double divide = 0; double[] array = new double[5]; System.out.print(" Enter 5 floating point numbers: "); for (int i = 0; i < array.length; i++) { array[i] = keyboard.nextDouble(); sum = sum + array[i]; } char choice; while (1==1) { choice = processMenu(); if(choice == 'a') { System.out.println("the sum is " + sum);} else if (choice =='s') { difference = array[0]; for (int i = 0; i < array.length; i++) difference = difference - array[i];
I want to use switch statements in my program. I've got my code working already but want to use switch statements. How do I do it? Here is what i got so far.
import java.util.Scanner;
public class ReviewApplication {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
double sum = 0;
double difference = 0;
double product = 0;
double divide = 0;
double[] array = new double[5];
System.out.print(" Enter 5 floating point numbers: ");
for (int i = 0; i < array.length; i++) {
array[i] = keyboard.nextDouble();
sum = sum + array[i];
}
char choice;
while (1==1) {
choice = processMenu();
if(choice == 'a') {
System.out.println("the sum is " + sum);}
else if (choice =='s') {
difference = array[0];
for (int i = 0; i < array.length; i++)
difference = difference - array[i];
System.out.println("the difference is" + difference);}
else if (choice == 'm') {
product = array[0];
for (int i = 1; i < array.length; i++){
product = product * array[i];}
System.out.println("the product is " + product);
}
else if (choice == 'd') {
divide = array[0];
for (int i = 1; i < array.length; i++){
divide = divide/array[i];
}
System.out.println("the answer is" + divide);
}
else if (choice =='q')
{
System.out.println("Thanks for playing!");
System.exit(0);
} // end of main
}
public static char processMenu( ){
Scanner keyboard = new Scanner(System.in);
char response ;
while(1==1) {
System.out.println("Enter a to add numbers");
System.out.println("Enter s to subtract");
System.out.println("Enter m to multiply");
System.out.println("Enter d to divide");
System.out.println("Enter q to exit");
System.out.print(">>> ");
response = keyboard.next().charAt(0);
if (response == 'a' || response == 's' || response == 'm' ||
response == 'd'|| response == 'q') {
break;
}
else {
System.out.println("Invalid menu slection. Try again.");
}
}
return response;
}
}
Step by step
Solved in 2 steps with 3 images