为什么我的MVC代码不执行任何操作?

时间:2019-04-15 20:17:29

标签: java model-view-controller console

首先,我是Java的初学者。 从现在开始两周以来我一直在学习它,但是我以前学过C#。 我以前从未听说过MVC,目前我对此有疑问。 我必须在MVC中做一个基于控制台的计算器(我真的不知道为什么,我听说MVC是针对具有UI的应用程序的)。之前,我已经为带有UI的应用程序成功完成了此操作,但是我不知道如何以及在何处处理控制台输入和输出。 目前,我有以下几行代码:

package com.kristofgero;

public class Model {
    private double e;
    public void osszead(double a, double b) {
        e = a+b;
    }
    public void kivon(double a, double b) {
        e = a-b;
    }
    public void szoroz(double a, double b) {
        e = a*b;
    }
    public void oszt(double a, double b) {
        e = a/b;
    }
    public double getCalculationValue() {
        return e;
    }
}


package com.kristofgero;
import java.util.Scanner;

public class View {
    Scanner scanner = new Scanner(System.in);
    private double a = scanner.nextDouble();
    private double b = scanner.nextDouble();
    private double e = 0;
    public double getA() {
        return a;
    }
    public double getB() {
        return b;
    }
    public double getE() {
        return e;
    }
    void displayError(String hiba) {
        System.out.println(hiba);
    }
}

package com.kristofgero;

public class Controller {
    private View theView;
    private Model theModel;

    public Controller(View theView, Model theModel) {
        this.theView = theView;
        this.theModel = theModel;
    }
    class Calculate {
        public void calculateMethod() {
            double a = 0;
            double b = 0;
            double e = 0;
            try {
                a = theView.scanner.nextDouble();
                b = theView.scanner.nextDouble();
                String jel = theView.scanner.nextLine();
                switch (jel) {
                    case "+": e = a+b; break;
                    case "-": e = a-b; break;
                    case "*": e = a*b; break;
                    case "/": e = a/b; break;
                }
            } catch (Exception error) {
                theView.displayError("Két számot adjon meg!");
            }
        }
    }
}

package com.kristofgero;

public class Main {
    public static void main(String[] args) {
        View theView = new View();
        Model theModel = new Model();
        Controller theController = new Controller(theView, theModel);

    }
}

我必须使用此计算器才能读取用户之前给出的2个数字的正确运算。 现在,我的代码实际上什么也没做。

1 个答案:

答案 0 :(得分:0)

不确定为什么将calculateMethod放在内部类中,但是需要调用该方法才能从用户那里获取输入。

public class Controller {
    private View theView;
    private Model theModel;

    public Controller(View theView, Model theModel) {
        this.theView = theView;
        this.theModel = theModel;
    }

        public void calculateMethod() {
            double a = 0;
            double b = 0;
            double e = 0;
            try {
                a = theView.scanner.nextDouble();
                b = theView.scanner.nextDouble();
                String jel = theView.scanner.nextLine();
                switch (jel) {
                    case "+": e = a+b; break;
                    case "-": e = a-b; break;
                    case "*": e = a*b; break;
                    case "/": e = a/b; break;
                }
            } catch (Exception error) {
                theView.displayError("Két számot adjon meg!");
            }
        }
}

public class Main {
    public static void main(String[] args) {
        View theView = new View();
        Model theModel = new Model();
        Controller theController = new Controller(theView, theModel);
        theController.calculateMethod();
    }
}