2015-04-21 147 views
-1

我正在制作一个计算器程序,并且我已经安装了所有东西,并且它工作得更早,但是在我添加一个方法后,当我在调试模式下运行时,Eclipse说:我的主要方法有错误。我不知道为什么它不会运行。我不知道为什么我的代码不会运行

我得到的错误:在线程 异常 “主要” 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

'Eclipse说我在主要方法中有错误' - 什么错误?这是一个编译器错误?运行时错误? – copeg

+0

在getNums方法之前,您有一个额外的大括号('}')。 –

+0

这是它告诉我的异常在线程“main”java.lang.Error:未解决的编译问题: \t at com.molotuff.main.Calculator.main(Calculator.java:13) –

回答

1

删除右括号}getNums()方法之前。您已经在menu()之后关闭了课程,这就是为什么getNums()未包含在课程体内会给您带来错误的原因。

+0

谢谢,我不知道我是如何错过的,因为我认为我经历了多次括号。这也很奇怪,在我编写代码时,Eclipse没有将它显示为错误。 –

相关问题