Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package javaapplication3;
- import java.util.StringTokenizer;
- import javax.swing.JOptionPane;
- public class JavaApplication3 {
- public static void main(String[] args) {
- String expr;
- int num1, num2, res = 0;
- while ((expr = JOptionPane.showInputDialog("Liczba 1 op Liczba2")) != null) {
- // klasa do rozbioru tekstu na elementy
- StringTokenizer st = new StringTokenizer(expr);
- if (st.countTokens() != 3) {
- continue;
- }
- String snum1 = st.nextToken(), sop = st.nextToken(), snum2 = st.nextToken();
- try {
- num1 = Integer.parseInt(snum1);
- num2 = Integer.parseInt(snum2);
- } catch (NumberFormatException ex) {
- continue;
- }
- if (sop.length() != 1) {
- continue;
- }
- switch(sop)
- {
- case "+":
- res = num1 + num2;
- break;
- case "-":
- res = num1 - num2;
- break;
- case "*":
- res = num1 * num2;
- break;
- case "/":
- res = num1 / num2;
- break;
- }
- JOptionPane.showMessageDialog(null, "wynik= "+res);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement