# Switch

• 03-19-2013, 04:49 AM
KillerToFu_xX
Switch
Solved
• 03-19-2013, 10:31 AM
Filoflow
Re: Switch
I am sure that everybody will learn from this experience...
• 03-19-2013, 12:20 PM
DarrylBurke
Re: Switch
Quote:

Originally Posted by KillerToFu_xX
Solved

The original post was:
Quote:

Originally Posted by KillerToFu_xX
Trying to figure out if i can make a switch statement do the fallowing code but i'm not sure how i can make the switch read my variables.

Quote:

import java.util.Scanner;

public class question3
{
public static void main(String[] args)
{
System.out.println(" Please select one of these options.");
System.out.println(" 1. Press A to add three numbers ");
System.out.println(" 2. Press S to subtract two numbers ");
System.out.println(" 3. Press M to multiply by three numbers ");
System.out.println(" 4. Press D to divide by two numbers ");
System.out.println(" 5. Press O to modulus two numbers ");
System.out.println(" 6. Press E to quit the program ");
System.out.println(" ");

Scanner input = new Scanner( System.in );
String aString = input.next();
char choice = aString.charAt(0);

switch (option)
{
case 'A': {
break;
case 'S': {
subtract (); }
break;
case 'M': {
multiply (); }
break;
case 'D': {
divide (); }
break;
case 'O': {
modulus (); }
break;
case 'E': {
quit ();}
break;
default: }

}

Scanner input = new Scanner( System.in );
float number1;
float number2;
float number3;
float sum;

System.out.printf( "Enter first interger: " );
number1 = input.nextInt();
System.out.printf( "Enter second interger: " );
number2 = input.nextInt();
System.out.printf( "Enter Third interger: " );
number3 = input.nextInt();

sum = number1 + number2 + number3;
System.out.printf( "Sum equals %f\n", sum);
}
public static void subtract(){
Scanner input = new Scanner( System.in );
float number1;
float number2;
float difference;

System.out.printf( "Enter number to be subtracted: " );
number1 = input.nextInt();
System.out.printf( "Enter interger to subtract: " );
number2 = input.nextInt();

difference = number1 - number2;
System.out.printf( "The difference is %f\n", difference);

}
public static void multiply(){
Scanner input = new Scanner( System.in );
float number1;
float number2;
float number3;
float product;

System.out.printf( "Enter first interger to multiply: " );
number1 = input.nextInt();
System.out.printf( "Enter second interger: " );
number2 = input.nextInt();
System.out.printf( "Enter Third interger: " );
number3 = input.nextInt();

product = number1 * number2 * number3;
System.out.printf( "The product is %f\n", product);

}

public static void divide(){
Scanner input = new Scanner( System.in );
float number1;
float number2;
float dividen;

System.out.printf( "Enter first interger: " );
number1 = input.nextInt();
System.out.printf( "Enter second interger: " );
number2 = input.nextInt();

dividen = number1 / number2;
System.out.printf( "The dividen equals %f\n", dividen);

}

public static void modulus(){
Scanner input = new Scanner ( System.in );
float number1;
float number2;
float sum;

System.out.printf( "Enter first interger: " );
number1 = input.nextInt();
System.out.printf( "Enter second inerger: " );
number2 = input.nextInt();

sum = number1 % number2;
System.out.printf( "The answer equals %f\n", sum);

}

public static void quit(){
System.out.printf( "Thank you for trying to use the program.");
return;

}

}
Any ideas would be sweet thanks!