我不知道为什么我的代码不会运行

时间:2015-04-21 18:52:43

标签: java

我正在制作一个计算器程序,我得到了所有设置并且它工作得更早,但是在我添加了一个方法后,当我在调试模式下运行时,Eclipse说我的main方法出错了。我不知道为什么它不会跑。

我得到的错误: 线程" main"中的例外情况java.lang.Error:未解决的编译问题:

at com.molotuff.main.Calculator.main(Calculator.java:13)

这是我的代码:

package com.molotuff.main;

import java.util.ArrayList;
import java.util.Scanner;

public class Calculator {
    private static Scanner reader = new Scanner(System.in);
    private static boolean running = true;
    private static boolean calcRunning = true;
    private static String command;
    private static ArrayList<Integer> nums = new ArrayList<Integer>();

    public static void main(String[] args) {
        System.out.println("*****************************");
        System.out.println("* Welcome to The Calculator *");
        System.out.println("*****************************");
        menu("help");

        while(running) {
            System.out.println("Enter a command:");
            command = reader.nextLine();
            menu(command);
            if(command.equalsIgnoreCase("quit")) {
                running = false;
            }
            if(command.equalsIgnoreCase("add")) {
                getNums();
                int answer = Calculations.sum(nums);
                System.out.println("The sum is: " + answer);
            }
        }
    }

    public static void menu(String command) {
        if(command.equalsIgnoreCase("help")) {
        System.out.println("Commands: ");
        System.out.println("Quit");
        System.out.println("Help");
        System.out.println("Add");
        System.out.println("Subtract");
        System.out.println("Divide");
        System.out.println("Multiply");
        System.out.println("Type help [command] for more info on that command");
        }
        if(command.equalsIgnoreCase("help quit")) {
            System.out.println("Quit: quits the program.");
        }
        if(command.equalsIgnoreCase("help help")) {
            System.out.println("Help: prints the help menu.");
        }
        if(command.equalsIgnoreCase("help add")) {
            System.out.println("Add: takes numbers inputed and adds them together.");
        }
        if(command.equalsIgnoreCase("help Subtract")) {
            System.out.println("Subtract: takes a set of numbers and subtracts     them \n (note: "
                    + "subtracts in this order [first num entered] - [second num entered] "
                    + "etc.)");
        }
        if(command.equalsIgnoreCase("help multiply")) {
            System.out.println("Add: takes numbers inputed and multiplies them     together.");
        }
        if(command.equalsIgnoreCase("help divide")) {
            System.out.println("Subtract: takes a set of numbers and divides     them \n (note: "
                    + "divides in this order [first num entered] / [second num     entered] " 
                    + "etc.)");
        }

    }
}

    public static void getNums() {
        while(calcRunning) {
            String userInput = reader.nextLine();
            int userNums;
            if(userInput.isEmpty()) {
                calcRunning = false;
            } else {
                userNums = Integer.parseInt(userInput);
                nums.add(userNums);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

}方法之前删除右括号getNums()。您在menu()之后关闭了课程,这就是为什么getNums()未包含在课程正文中给您错误的原因。