Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
July 29th, 2013, 10:39 PM
 lordrey22
Registered User

Join Date: Jul 2013
Posts: 8
Time spent in forums: 57 m
Reputation 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
July 29th, 2013, 10:47 PM
 NormR
Contributing User

Join Date: Aug 2010
Location: SW Missouri
Posts: 3,580
Time spent in forums: 2 Weeks 3 Days 2 h 42 m 49 sec
Reputation Power: 347

#3
July 29th, 2013, 10:51 PM
 lordrey22
Registered User

Join Date: Jul 2013
Posts: 8
Time spent in forums: 57 m
Reputation Power: 0
the code doens't go to the switch statement or operation statement. sorry for my bad english

#4
July 30th, 2013, 07:41 AM
 NormR
Contributing User

Join Date: Aug 2010
Location: SW Missouri
Posts: 3,580
Time spent in forums: 2 Weeks 3 Days 2 h 42 m 49 sec
Reputation Power: 347
Quote:
 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 07:47 AM.

#5
July 30th, 2013, 09:12 PM
 lordrey22
Registered User

Join Date: Jul 2013
Posts: 8
Time spent in forums: 57 m
Reputation Power: 0
Quote:
 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.");
}
}
}```

#6
July 30th, 2013, 09:15 PM
 NormR
Contributing User

Join Date: Aug 2010
Location: SW Missouri
Posts: 3,580
Time spent in forums: 2 Weeks 3 Days 2 h 42 m 49 sec
Reputation Power: 347
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 09:17 PM.

 Viewing: Dev Shed Forums > Programming Languages > Java Help > JAVA Calculator Problem