java - 内存计算器困惑java

标签 java

在我的类里面,我们需要用 Java 制作一个内存计算器。我对 Java 很陌生,并且在制作该程序时得到了帮助。交上去,老师说“请将MemoryCalculator类与main()方法的类分开。目前你创建类的方式,没有理由创建类的实例。但是作业的重点就是使用单独的类和对象。”这是 super 漫长的一周和期中考试,而此时却输了。任何帮助都会很棒。

import java.util.Scanner;

public class MemoryCalculator {


        private double currentValue;
        //Methods
        //Scanner
        public static int displayMenu(){
                    Scanner input = new Scanner(System.in);
                    System.out.print("Lets do some math! \nMenu \n1. Add \n2. Subtract \n3. Multiply \n4. Divide \n"
                                    + "5. Clear \n6. Quit \n\nWhat would you like to do? ");
                    int menuChoice = input.nextInt();
                    return menuChoice;
        }

        public static double getOperand(String prompt) {
            Scanner input = new Scanner(System. in );
            double operand;
            System.out.println(prompt);
            operand = input.nextDouble();
            return operand;
        }

        //Current Value
        //Gets
        public double getCurrentValue() {
            return currentValue;
        }
        //Setter
        public void setCurrentValue(double currentValue) {  
            this.currentValue = currentValue;  
        }
        //Add
        public void add(double operand2) {
            currentValue += operand2;
        }
        //Subtract
        public void subtract(double operand2) {
            currentValue -= operand2;
        }
        //Multiply
        public void multiply(double operand2) {
            currentValue *= operand2;
        }
        //Divide
        public void divide(double operand2) {
            if (operand2==0){
                            setCurrentValue(0);    
                    }
                    currentValue /=operand2;
        }
        //Clear
        public void clear() {
            currentValue = 0;
        }



        //Main part of the calculator
        public static void main(String[] args) {

            MemoryCalculator instance = new MemoryCalculator();
            double operand;
            boolean repeat = true;


            while (repeat) {
                System.out.println("The current value is: " + instance.getCurrentValue() + "\n");
                int menuChoice;
                menuChoice = displayMenu();

                if (menuChoice > 6 || menuChoice < 1){
                                    System.out.println("I'm sorry, " + menuChoice + " wasn't one of the options\n");
                }

            switch(menuChoice){
                case 1:
                    operand = getOperand("What is the second number?");
                    instance.add(operand);
                    break;
                case 2:
                    operand = getOperand("What is the second number?");
                    instance.subtract(operand);
                    break;
                case 3:
                    operand = getOperand("What is the second number?");
                    instance.multiply(operand);
                    break;
                case 4:
                    operand = getOperand("What is the second number?");
                    instance.divide(operand);
                    break;
                    case 5:
                    instance.clear();
                    break;
                    case 6:
                            System.out.println("Goodbye have a great day");
                            System.exit(0);
                            break;

            }
        }    
    }
}

最佳答案

看起来您对程序所做的是创建一个单一的类,该类包含计算器程序的所有代码,您在其中实例化了同一类的对象。

您的老师想要的是让您有两个单独的类,一个包含使计算器工作的代码,另一个类在其中实例化第一个类的对象,并调用该类中包含的方法.

对于您的作业,我建议创建一个新类,可能称为 Main,您的程序的 Main() 方法将在其中,并保留所有MemoryCalculator 类中计算器程序的代码。从那里,您可以实例化 MemoryCalculator 类的对象(您已经这样做了,称为 instance),并使用方法调用来引用 MemoryCalculator 中的方法和属性 类。

考虑到您将从 MemoryCalculator 类的对象调用大部分代码,这可能需要重新编写您的部分代码才能正常运行,但这应该是可行的。

关于java - 内存计算器困惑java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33006591/

相关文章:

java - 如何强制在 Java 1.5.0_14 JRE 而不是 JRE 1.7 中打开 JNLP 文件

java - 尝试对 Java 对象数组进行排序

java - Activity 到 Activity 回调监听器

java - 如何在 Java 中为 Camellia 密码和加密提供正确的 key

java - 使用 AlarmManager 测试 BroadcastReceiver

Java-Jpa-Hibernate 传递分离实体以在合并时保留

java - Spring JdbcTemplate 的 query() 方法的实现到底是如何工作的?

java - 为什么我不能将 Integer.toBinaryString() 连接到 Java 中的另一个字符串?

java - Spring数据库连接配置流程

java - 如何动态填充二维数组