0

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

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));
}
}

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

4

1 回答 1

2

你的绘画方法不正确。用下面的替换它。drawString接受三个参数String, x pos,y pos

@Override
public void paint(Graphics screen) {
    Graphics2D screen2D = (Graphics2D) screen;
    screen2D.drawString("The square root of "+number+" - "+(int) Math
            .sqrt(number),10,10);
}
于 2012-10-05T03:55:47.703 回答