问答题
下面的程序中定义了两个方法求自然数1~100的和。具体如下:int suml(int n);利用循环求1~n的和,int sum2(int n);利用递归方法求和1~n的和;在main()方法中调用这两个方法求1~100的和并显示。在程序的每条横线处填写一个适当的语句,使程序的功能完整。
public class Sum
public static void main(String args[])
//1.调用suml(int n),求1~100的和
System.out.println("1~100的和:"+sum1(100));
//2,调用sum2(int n),求1~100的和
System.out.println("1~100的和:"+sum2(100));
static int suml(int n)
int result=0;
for(int i=1;i<=n;i++)
________________
retrun result;
static int sum2(int n)
if(______________)
return 1;
else
_____________
【参考答案】
result+=i;
n==1
return n+sum2(n-1);
点击查看答案&解析
<上一题
目录
下一题>
热门
试题
问答题
下面是一个Applet程序,程序的功能是显示一个圆心在Applet的中心,半径从0到100像素的不断变大的蓝色球的动画。请改正程序中的错误(有下划线的语句),使程序执行后,能得到预期的结果。 注意:不改动程序的结构,不得增行或删行。 程序的执行结果为: import java.awt.*; import java.applet.*; * <applet code=XiaoQiu width=800 height=600> < applet> * public class XiaoQiu extends Applet implements Runnable int currentX=0,currentY=0; Thread m_Draw=null; public void init() m_Draw=new Thread (super); public void paint(Graphics g) g.setColor(Color.blue); mycircle(g,getSize().width 2,getSize().height 2,currentX,currentY); public void start() m_Draw.start(); try Thread.sleep(500); catch(InterruptedException e) public void stop() if(m_Draw.isAlive()) m_Draw. stop(); public void run() try while(true) currentX++; currentY++; if(currentX>i00&&(currentY>100)) currentX=0; currentY=0; paint(); Thread.sleep(100); catch(InterruptedException e) public void mycircle(Graphics g,int x,int y,int rx, int ry) g.filloval(x-rx,y-ry,2*rx,2*ry); ex39_3. html: <html> <head> <title>A Simple Program< title> < head> <body> <applet cede= XiaoQiu.class width=800 height=400> < applet> < body> < html>
点击查看答案&解析
问答题
请完成下列Java程序。程序的执行结果是生成一个具有一个按钮的窗体,并且按钮的标签是“欢迎参加全国计算机等级考试—Java部分!”字样。 注意:请勿改动main()主方法和其他已有的语句内容,仅在下划线处填入适当的语句。 import java.awt.*; import java.awt.event.*; public class TestActionEvent public static void main(String args[]) Frame f=new Frame( Test ); Button b=new Button( 欢迎参加Java考试! ); TestListener testmonitor=new TestListener(); Monitor bh=new Monitor(); b.addActionListener(bh); f.addwindowListener(testmonitor); f.add(b); f.setSize(150,100); f.setVisible(true); class Monitor ___________ ActionListener public void actionPerformed(ActionEvent e) System.out.println( a button has been pressed ); class TestListener__________WindowAdapter public void windowClosing(WindowEvent e) System.exit(1);
点击查看答案&解析
相关试题
下面是一个Applet程序,程序的功能是显示一...
请完成下列Java程序。程序的执行结果是生成...