2013-03-15 51 views
1

我剛開始在iTunes上學習斯坦福CS106a課程,但我遇到了Eclipse的問題。這裏是我的代碼:需要幫助在Eclipse中使用ACM Java庫來運行程序

/* 
* File: Add2Integers.java 
* ----------------------- 
* A simple ConsoleProgram to add two integers 
* and display their total. 
*/ 

import acm.program.*; 

public class Add2Integers extends ConsoleProgram { 

public void run() { 
    /* So all y'all in the back can see! */ 
    setFont("DejaVuSerif-BOLD-24"); 

    println("This program adds two numbers."); 
    int n1 = readInt("Enter n1: "); 
    int n2 = readInt("Enter n2: "); 
    int total = n1 + n2; 
    println("The total is " + total + "."); 
} 

} 

當我嘗試運行它,我得到了部分不包含小應用程序的消息。我認爲這與import acm.program有關。

我下載了acm工具包,並嘗試將program.java文件添加到我的根文件夾中,構建路徑,對整個acm文件夾執行相同操作,沒有任何效果。

我只需要幫助讓這個簡單的程序啓動和運行,以便我可以開始學習。

我正在運行OSX 10.8。

回答

1

要運行一個Java應用程序,你需要一個主要方法:

public static void main(String[] args) { 
    Add2Integers add2Integers = new Add2Integers(); 
    add2Integers.run(); 
} 
+0

當我添加這個時沒有改變。我看到教授運行這個相同的程序,在Eclipse中不需要額外的代碼。如果你可以給我一步一步的指示,我需要做些什麼來實現這個目標? – Kris 2013-03-15 17:32:30

0

當我嘗試運行時,收到該部分不包含小程序的消息。

這是因爲它不是一個小程序。這是一個普通的Java應用程序。

做圖形的例子是小程序。但是這是純文本的 - 它擴展了ConsoleProgram - 所以它不是一個小程序。