#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    8
    Rep Power
    0

    JAVA Calculator Problem


    Please Help me solve my problem in using switch and also the answer would be output required decimal places. its for my final exam. thanks a lot.



    Code:
    import java.io.*;
    import java.util.*;
    import javax.swing.JOptionPane;
    
    public class exercise1
    {
    
    	public static void main(String args[]){
    		String input = JOptionPane.showInputDialog("Selection from the choices below:\n\n 1. Addition\n 2. Subtraction\n 3. Multiplication\n 4. Division\n 5. Modulo\n\n Your choice?");
    
    
    	JOptionPane.showInputDialog("\nEnter first number: ");
    	int x = Integer.parseInt(x);
    	JOptionPane.showInputDialog("Enter second number: ");
    	int y = Integer.parseInt(y);
    
    
    	int choice = Integer.parseInt(input);
    	if((choice<=5) && (choice>0))
    	{
    
    	switch (choice)
    	{
    
    	case 1: //addition 1 decimal place
    		JOptionPane.showMessageDialog(null, x + " plus " +  y + " = " + (x + y);
    	break;
    
    	case 2: //subtraction no decimal place
    		JOptionPane.showMessageDialog(x + " minus " + y + " = " + (x - y) );
    	break;
    
    	case 3: //multiplication 3 decimal place
    		JOptionPane.showMessageDialog(x + " times " + y + " = " + (x * y) );
    	break;
    
    	case 4: //division 4 decimal place
    		JOptionPane.showMessageDialog(x + " divided by " + y + " = " + (x / y) );
    	break;
    
    	case 5: //modulo
    		JOptionPane.showMessageDialog(x + " modulo " + y + " = " + (x % y) );
    	//break;
    	}
    
    	}
    	else
    	{
    	System.out.println("Please enter a 1, 2, 3, 4 or 5.");
    	}
    }
    }
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    What problems are you having? Please post any questions you have about your problems.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    8
    Rep Power
    0
    the code doens't go to the switch statement or operation statement. sorry for my bad english
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    the code doens't go to the switch statement
    Are there any error messages? Copy the full text and paste it here.
    Last edited by NormR; July 30th, 2013 at 06:47 AM.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    8
    Rep Power
    0
    Originally Posted by NormR
    Are there any error messages? Copy the full text and paste it here.
    the code will stops after you input choice number. the code does not proceed to the switch statement.
    Code:
    import java.io.*;
    import java.util.*;
    import javax.swing.JOptionPane;
    
    public class exercise1
    {
    
    	public static void main(String args[]){
    		String input = JOptionPane.showInputDialog("Selection from the choices below:\n\n 1. Addition\n 2. Subtraction\n 3. Multiplication\n 4. Division\n 5. Modulo\n\n Your choice?");
    
    
    	JOptionPane.showInputDialog("\nEnter first number: ");
    	int x = Integer.parseInt(x);
    	JOptionPane.showInputDialog("Enter second number: ");
    	int y = Integer.parseInt(y);
    
    
    	int choice = Integer.parseInt(input);
    	if((choice<=5) && (choice>0))
    	{
    
    	switch (choice)
    	{
    
    	case 1: //addition 1 decimal place
    		JOptionPane.showMessageDialog(null, x + " plus " +  y + " = " + (x + y);
    	break;
    
    	case 2: //subtraction no decimal place
    		JOptionPane.showMessageDialog(x + " minus " + y + " = " + (x - y) );
    	break;
    
    	case 3: //multiplication 3 decimal place
    		JOptionPane.showMessageDialog(x + " times " + y + " = " + (x * y) );
    	break;
    
    	case 4: //division 4 decimal place
    		JOptionPane.showMessageDialog(x + " divided by " + y + " = " + (x / y) );
    	break;
    
    	case 5: //modulo
    		JOptionPane.showMessageDialog(x + " modulo " + y + " = " + (x % y) );
    	//break;
    	}
    
    	}
    	else
    	{
    	System.out.println("Please enter a 1, 2, 3, 4 or 5.");
    	}
    }
    }
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    How are you compiling the program? It has several compiler errors that you must fix before the code will execute properly.
    If you are using an IDE, it ignores errors and tries to execute the code with compiler errors. It should give you error messages.
    Last edited by NormR; July 30th, 2013 at 08:17 PM.

IMN logo majestic logo threadwatch logo seochat tools logo