Advertisement
Guest User

Untitled

a guest
Oct 24th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. package javaapplication3;
  2.  
  3. import java.util.StringTokenizer;
  4. import javax.swing.JOptionPane;
  5.  
  6. public class JavaApplication3 {
  7.  
  8. public static void main(String[] args) {
  9.  
  10. String expr;
  11. int num1, num2, res = 0;
  12.  
  13. while ((expr = JOptionPane.showInputDialog("Liczba 1 op Liczba2")) != null) {
  14. // klasa do rozbioru tekstu na elementy
  15. StringTokenizer st = new StringTokenizer(expr);
  16.  
  17. if (st.countTokens() != 3) {
  18. continue;
  19. }
  20.  
  21. String snum1 = st.nextToken(), sop = st.nextToken(), snum2 = st.nextToken();
  22.  
  23. try {
  24. num1 = Integer.parseInt(snum1);
  25. num2 = Integer.parseInt(snum2);
  26. } catch (NumberFormatException ex) {
  27. continue;
  28. }
  29.  
  30. if (sop.length() != 1) {
  31. continue;
  32. }
  33.  
  34. switch(sop)
  35. {
  36. case "+":
  37. res = num1 + num2;
  38. break;
  39.  
  40. case "-":
  41. res = num1 - num2;
  42. break;
  43.  
  44. case "*":
  45. res = num1 * num2;
  46. break;
  47.  
  48. case "/":
  49. res = num1 / num2;
  50. break;
  51.  
  52.  
  53. }
  54.  
  55. JOptionPane.showMessageDialog(null, "wynik= "+res);
  56.  
  57. }
  58.  
  59.  
  60. }
  61.  
  62.  
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement