简体   繁体   English

System.exit(0)无法正常工作和面向对象的设计。

[英]System.exit(0) not working & Object Oriented Design.

I am writing a simple java program to perform Addition and Multiplication using Object Oriented technique . 我正在编写一个简单的Java程序,以使用面向对象技术执行加法乘法 First it asks about the operation Addition, Multiplication or Exit. 首先,它询问有关加法,乘法或退出的操作。 When Addition and Multiplication button is pressed it gets two numbers from user, performs the task and gives the result.But my problem is when Exit button is pressed it does not terminates instead it ask for numbers. 当按下加法和乘法按钮时,它会从用户那里获得两个数字,执行任务并给出结果。但是我的问题是,当按下退出按钮时,它不会终止,而是要求输入数字。
And second thing which i want to ask is that am i following the Object Oriented approach . 我想问的第二件事是我是否遵循面向对象的方法

import javax.swing.JOptionPane;
public class Calculator {

private static int number1;
private static int number2;

public static void setNumber1(int n1) {
    number1 = n1;
}
public static void setNumber2(int n2) {
    number2 = n2;
}
public static int getNumber1() {
    return number1;
}

public static int getNumber2() {
    return number2;
}

public static void numbers(){
    int n=Integer.parseInt(JOptionPane.showInputDialog("Enter first number:"));
    int nn=Integer.parseInt(JOptionPane.showInputDialog("Enter first number:"));
    setNumber1(n);
    setNumber2(nn);
}
public static void calculate(int o){
    switch(o){
    case 0:
        JOptionPane.showMessageDialog(null, "Addition is  :"+(number1+number2));
        break;
    case 1:
        JOptionPane.showMessageDialog(null, "Product is  :"+(number1*number2));
        break;
    case 2:
        System.exit(0);
        break;
    }
}

public static void main(String[] args) {
    // TODO Auto-generated method stub
    //Custom button text
    Object[] options = {"Addition","Product", "Exit!"};
    int op = JOptionPane.showOptionDialog(null,"What operation Would you like to perform ?","Addition or Product Calculator",
                                         JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE,
                                         null,options,options[2]);

    Calculator c=new Calculator();
    c.numbers();
    c.calculate(op);
}

} }

The problem that your program wont exit is that you are calling the numbers method which reads the numbers before the checking options in the calculate method. 您的程序无法退出的问题是,您正在调用numbers方法,该方法会在calculate方法中的检查选项之前读取数字。 So you can prevent calling of numbers method when op = 2 by checking it with an if . 因此,可以通过使用if进行检查来防止在op = 2时调用numbers方法。
code to do that is: 做到这一点的代码是:

Calculator c=new Calculator();
if(op!=2) {
   c.numbers();
}
c.calculate(op);

The reason why it asks for numbers is because you are calling the method numbers() before calculate(int o) . 它要求输入数字的原因是因为您要在calculate(int o)之前调用numbers()方法。 You have to switch the order but with some modifications to your code. 您必须切换顺序,但对代码进行一些修改。

And about OOP, Java is purely OOP. 关于OOP,Java纯粹是OOP。 But to get the most out of it read and exercise more on the object oriented concepts like inheritance, interfaces ... 但是要充分利用它,请阅读并更多地研究诸如继承,接口之类的面向对象的概念。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM