首页 > 代码库 > 在Eclipse中调用Algs4库

在Eclipse中调用Algs4库

首先下载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库