首先下载Eclipse,我选择的是Eclipse IDE for Java Developers64位版本,下载下来之后解压缩到喜欢的位置然后双击Eclipse.exe启动
然后开始新建项目,File -> New Java Project,项目名随便写,如下图
右键src文件夹,Add -> New Java Class,这里需要注意Name一栏里填写的内容就是类名,这里我写了TestAlgs4,为了测试「算法 第四版」作者给的那个测试样例
代码如下:
import edu.princeton.cs.algs4.StdDraw;
public class TestAlgs4 {
public static void main (String[] args) {
StdDraw.setScale(-1, 1);
StdDraw.clear(StdDraw.BLACK);
StdDraw.setPenColor(StdDraw.WHITE);
StdDraw.square(0, 0, 1);
// write some text
StdDraw.setPenColor(StdDraw.WHITE);
StdDraw.text(0, +0.95, "Hello, world! This is a test Java program.");
StdDraw.text(0, -0.95, "Close this window to finish the installation.");
// draw the bullseye
StdDraw.setPenColor(StdDraw.BOOK_BLUE);
StdDraw.filledCircle(0, 0, 0.9);
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.filledCircle(0, 0, 0.8);
StdDraw.setPenColor(StdDraw.BOOK_BLUE);
StdDraw.filledCircle(0, 0, 0.7);
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.filledCircle(0, 0, 0.6);
// draw a picture of the textbook
StdDraw.picture(0, 0, "cover.jpg", 0.65, 0.80);
}
}
这时候Eclipse应该会报错无数,不要急,添加对教材库Algs4.jar的引用:
右键工程名(我这里是HelloWorld)-> Properties -> Java Build Path -> Libraries -> Add Extenernal JARs,选中C:\Users\username\algs4\algs4.jar 即可,如图:
点击OK退出,报错信息消失,编译运行可以看到跟安装Algs4.exe一样的结果:
转自http://shuxiao.wang/2016/04/21/algs4-Eclipse.html
时间: 2024-10-29 10:46:54