虽然循环不会重复

时间:2013-09-29 03:09:17

标签: java while-loop

我需要让用户输入一个操作数,输入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;
    }
}

很抱歉没有评论。

0 个答案:

没有答案