简单的Java Math Applet

时间:2012-10-05 03:45:43

标签: java applet

如何创建应用程序 可以显示作为参数提交的数字的平方根吗?到目前为止,我有:

import java.awt.*;

public class RootApplet extends javax.swing.JApplet {
int number; 

public void init() {
number = 225;
}

public void paint(Graphics screen) {
Graphics2D screen2D = (Graphics2D) screen; 
screen2D.drawString("The square root of " + arguments[0] +

" is"
Math.sqrt(number));
}
}

但是我显然根据运行时错误遗漏了一些东西。 `错误。

1 个答案:

答案 0 :(得分:2)

您的绘画方法不正确。用下面的替换。 drawString接受三个参数Stringx posy pos

@Override
public void paint(Graphics screen) {
    Graphics2D screen2D = (Graphics2D) screen;
    screen2D.drawString("The square root of "+number+" - "+(int) Math
            .sqrt(number),10,10);
}