2015-04-21 232 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沒有將它顯示爲錯誤。 –

相關問題