0

我需要让用户输入一个操作数,输入 2 个整数,显示结果,然后重复。它不会重复,当你输入一个整数继续时,它会终止。任何帮助将不胜感激,我已经被困了一段时间了!谢谢。

package lab03;

import java.util.Scanner;
import javax.swing.JOptionPane;

/**
 * 
 */
public class Lab03 {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    int n, p, q = 1;
    boolean run = true;
    String operator;

    while (run == true) {

        operator = JOptionPane.showInputDialog("Enter one of + - * / ");
        String trimOperator = operator.trim();
        char m = trimOperator.charAt(0);

        String firstOperand = JOptionPane
                .showInputDialog("Enter first integer operand: ");
        n = Integer.parseInt(firstOperand);

        String secondOperand = JOptionPane
                .showInputDialog("Enter second integer operand: ");
        p = Integer.parseInt(secondOperand);

        switch (m) {
        case '+':
            System.out.println(n + " plus " + p + " is " + (n + p));
            run = false;
            break;
        case '-':
            System.out.println(n + " minus " + p + " is " + (n - p));
            run = false;
            break;
        case '*':
            System.out
                    .println(n + " multiplied by " + p + " is " + (n * p));
            run = false;
            break;
        case '/':
            System.out.println(n + " divided by " + p + " is " + (n / p)
                    + " with remainder " + (n % p));
            run = false;
            break;
        default:
            System.out.println("Invalid character");
            run = false;
            break;

        }

        String lastInteger = JOptionPane
                .showInputDialog("Enter 0 to quit, or any other integer to continue. ");
        q = Integer.parseInt(lastInteger);
        System.out.println(q);
        System.exit(0);

        continue;
    }
}

抱歉没有评论。

4

0 回答 0