问题 问答题

下面是一段java代码,运行结果如图11-8所示。

程序代码如下:

(1)Java.awt.Graphics;

(2)Java.applet.Applet;

// ////////////////////////////////////////////

// Display y=sin(x)

public class J_DrawSin (2) Applet

{

public void paint(Graphics g)

{

double d, tx;

int x, y, x0, y0;

d=Math.PI/100; // Set step(Set the unit in x direction)

x0=y0=0;

(3)(tx=0, x=20; tx<2*Math.PI; tx+=d,x++)

{

y=120-(int)(Math.sin(tx)*50+60);

if(x>20)

g.drawLine(x0, y0, x, y);

x0=x;

y0=y;

}

g.drawstring("y=sin(x)", 10, 70);

} //End of method: paint

} //End of class: J_HelloApplet

<!-----------------------AppletExample.html------------------->

<HTML>

<HEAD>

<TITLE>

An applet Example ---- Hello Applet!

</TITLE>

</HEAD>

<BODY>

<(4)CODE="J_DrawSin.class" WIDTH=300 HEIGHT=120>

</APPLET>

</BODY>

</HTML>

搜索

此段代码中运用了程序设计结构中的循环结构中的哪一种。是否还使用了选择结构。请简述之。

答案

参考答案:

运用了for循环语句,for语句的一般形式为: for(表达式1;表达式2;表达式3)语句 各部分对应的含义可理解为: for([变量赋初值];[循环继续条件];[循环变量增值]){循环体语句} 此代码中还运用了if语句,它是选择结构中的一种,if语句的一般格式为: if(表达式) {语句组1;) [else {语句组2;}]

单项选择题
多项选择题