本文介绍了如何正确导入stddraw?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:IntelliJ CE

Editor: IntelliJ CE

我想要的:能够写出

setCanvas(500,500);

而不是

StdDraw.setcanvas(500,500);

问题:我无法弄清楚如何正确导入Stddraw库。
如果我只是做

Problem: I can't figure out how to correctly import the Stddraw library. If i simply do

import StdDraw;

IntelliJ告诉我StdDraw符号无法解析。
如果我发表评论我可以从StdDraw调用方法,但我必须写
StdDraw.setcanvas(500,500);

IntelliJ tells me "StdDraw" symbol cannot be resolved.If I comment it out I can call methods from StdDraw but I have to write StdDraw.setcanvas(500,500);

StdDraw.java是与Solver.java在同一目录中。

StdDraw.java is in the same directory as Solver.java.

代码:

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.ArrayList;
    import java.util.Scanner;
//    import StdDraw;//StdDraw is in the same directory as Solver

public class Solver {

    public static void main(String[] args) {
        System.out.println("Solver main is running.");

        StdDraw.setCanvasSize(500, 500);
        StdDraw.setPenColor(StdDraw.RED);
        StdDraw.filledRectangle(0,0,10,10);
     }
}

我已经尝试过:
- 确保Stddraw.java与我正在编译并运行
的文件位于同一目录中 - 查看
- 搜索完整的代码示例,即。代码,显示如何导入库
- 搜索YouTube教程
- 阅读
- 摆弄在StdDraw面前添加东西,例如。 stblib.StdDraw

I've already tried:- Making sure Stddraw.java is in the same directory as the file I'm compiling and running- Looking at http://introcs.cs.princeton.edu/java/stdlib/javadoc/StdDraw.html- Searching for COMPLETE code examples, ie. code that shows how to import the library- Searching YouTube tutorials- Reading https://www.jetbrains.com/idea/help/library.html- Fiddling around with adding stuff in front of StdDraw, eg. stblib.StdDraw

推荐答案

你说:

 What I want: Be able to write

     setCanvas(500,500);

 Instead of

     StdDraw.setcanvas(500,500);






这不符合Java的基本规则?


Isn't that against the basic rules of Java?

你不能写

    setCanvas(500,500);

除非你在StdDraw类中,StdDraw类的其他方法调用 setCanvasmethod。

unless you are inside the "StdDraw" class where other methods of the "StdDraw" class call the "setCanvas" method.

否则,您必须首先创建StdDraw类的实例:

Otherwise, either you have to create an instance of the "StdDraw" class first:

    e.g. StdDraw stdDraw = new StdDraw();

然后使用该实例调用方法:

and then use that instance to call the method:

    e.g. stdDraw.setCanvas(500,500); 

或者你用这种方式调用方法:

or you call the method this way:

    StdDraw.setcanvas(500,500);

这是Java的基本知识,对吧?

This is the basic knowledge of Java, right?

顺便说一句,如果StdDraw类与Solver类位于同一目录中,则不必导入它即可使用它。

By the way, if the "StdDraw" class is in the same directory as class "Solver", you don't have to import it to use it.

我使用eclipse。我将类StdDraw与其他类放在同一个包中。这样,我就不必使用导入关键字来导入StdDraw。我只是使用静态方式StdDraw的方法。只有当它不在同一个包中时才导入它。

I use eclipse. I put class "StdDraw" in the same package with other classes. This way, I don't have to use the "import" key word to import "StdDraw". I just use the methods of "StdDraw" the static way. You import it only when it is not in the same package.

FYI:我正在阅读Robert Sedgewick的算法,其中我从未见过任何直接调用方法如你所愿:

FYI: I'm reading Robert Sedgewick's "Algorithms", in which I've never seen any direct calls to methods like the way you want:

 uniform(N-i); or 
 printf("%.2f\n", x); or
 point(x0, y0); or
 line(x0, y0, x1, y1); or
 circle(x, y, r); or
 square(x, y, r); or
 polygon(x, y); etc. etc....

相反,它总是:

 StdRandom.uniform(N-i); or 
 StdOut.printf("%.2f\n", x); or
 StdDraw.point(x0, y0); or
 StdDraw.line(x0, y0, x1, y1); or
 StdDraw.circle(x, y, r); or
 StdDraw.square(x, y, r); or
 StdDraw.polygon(x, y); etc. etc....

我希望这会有所帮助。

I hope this helps.

这篇关于如何正确导入stddraw?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 17:00