注意:请勿改动main()主方法和其他已有语句内容,仅在下划线处填入适当的语句。
源程序文件代码清单如下:
import java.awt.*;
import java.awt.event.*;
public class ex7_2 extends Frame. implements ActionListener
{
private Label 1;
private TextField tf,tf2;
public static void main(String args[])
{
ex7_2 obj7_2=new ex7_2();
}
public ex7_2()
{
setTitle("ex7_2");
Panel p1;
Button b=new Button("OK");
p1=new Panel();
tf=new TextField(8);
p1.add(tf);
tf2=new TextField(8);
p1.add(tf2);
b.addActionListener(this);
p1.add(b);
b=new Button("Exit");
b.addActionListener(this);
p1.add(b);
add("North",p1);
Panel p2=new Panel();
l=new Label("最大公因数:");
p2.add(l);
add("Center",p2);
setsize(500,300);
show();
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getActionCommand().equals("Exit"))
//比较ae对象的标识名,是否为Exit
System.exit(0);
else if(ae.getActlonCommand().equals("OK"))
{
try
{
int a=Integer.parseInt(tf.getText());
int b=Integer.parseInt(tf2.getText());
int r,i;
while(b>0){
______;
______;
b=r;
}
l.setText("最大公约数: "+Integer.toString(a));
}catch{NumberFormatException nfe)
{
l.setText("请正确输入!");
}
}
}
}
第1题
句子不再随着方向键移动。运行结果如下图所示
注意:请勿改动其他已有语句内容,仅在横线处填入适当语句。
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Example2_8 extends Applet implements KeyListener
{
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
Button button;
Button stopButton;
Label out;
int x,y;
public void _______ ()
{
button = new Button("移动");
button.addActionListener(new AddMoveListener(this));
stopButton = new Button("停止移动");
stopButton.addActionListener(new RemoveListener(this));
stopButton.setEnabled(false);
out = new nabel("按下按钮以后我可以随方向键移动");
add(button);
add(stopButton);
add (out);
}
public void start()
{
super, start ();
}
public void keyPressed(KeyEvent e)
{
x=out.getBounds().x;
y=out.getBounds().y;
if(e.getKeyCode()==KeyEvent.VK_UP)
{
y=y-2;
if(y<=0) y=0;
out. setLocation (x, y);
}
else if(e.getKeyCode()==KeyEvent.VK_DOWN)
{
y=y+2;
if (y>=300) y=300;
out. setLocation (x, y);
}
else if(e.getKeyCode()==KeyEvent.VK_LEFT)
{
x=x-2;
if(x<=0) x=0;
out. setLocation (x, y);
}
else if(e.getKeyCode()==KeyEvent.VK_RiGHT)
{
第2题
过。每位同学的成绩可以通过随机数产生(0~100)。请在程序的每条横线处填写一条语句,是程序的功能完整。
注意:请勿改动main()主方法和其他已有的语句内容,仅在横线处填入适当的语句。
源程序文件代码清单如下:
______;
public class While
{
public static void main(String args[])
{
int fenshu=60;
int sumfenshu=0;
int i=1;
while((fenshu>=60)&&(i<=10) )
{
fenshu= (int)(__________);
System. out. print in (fenshu+" ");
sumfenshu+=fenshu;
i++;
}
System.out.println();
if(_________)
System. out. println ( "团队通过")
else
System. out.println ("团队测试不通过");
}
}
第3题
和等于该数自身。如28=1+2+4+7+14就是一个完全数。请在程序的每条横线处填写一个语句,使程序的功能完整。
注意:请勿改动main()主方法和其他已有的语句内容,仅在横线处填入适当的语句。
public class QuanShu{
public static void main(String args[]){
for(int n=l;n<9999;n++)
if(______________)System.out.println(n);
}
public static iht divsum(int n){//该方法功能是求一个数的所有约数
int s=0;
for(int i=l;i<n;i++)
if(_________________)
__________________
return s;
}
}
第4题
通过数学方法sin(),cos()画出一个圆环。请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。
注意:不改动程序的结构,不得增行或删行。程序的执行结果:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class YuanHuan extends Applet
{
public void paint(Graphics g)
{
double w=getSize().width/2;
double h=getSize().height/2;
g.getColor(Color.green);
for(double th=0;th<10;th+=0.00003)
{
double r=Math.cos(16*th)+th;
double x=r*Math.cos(th)+w;
double y=r*Math.sin(th)+h;
g.drawOval((int)x-1,(int)y-1,3,3);
}
}
public static void main(String args[ ])
{
Frame. f=new Frame("Draw");
YuanHuan p=new YuanHuan();
p.inti();
p.start();
f.add(p);
f.setSize(400,300);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.out.exit(0);
}
});
f. show ( );
}
}
ex3 3_3. htm1:
<html>
<head>
<title>A Simple Program</title>
</head>
<body>
<applet code=" YuanHuan.class" width=800 height=400>
</applet>
</body>
</html>
第5题
之前的字符)显示在屏幕上。请将程序补充完整。
程序运行结果如下:
Keep typing,it will stop while enter 'e'...
ddfsofkjlvncozieowdsfadsf
ddfsofkjlvncozi
import java.io.*;
public class ex4_1 {
public static void main(String[] args) {
char ch;
System.out.println("Keep typing,it will stop while enter 'e'...");
try{
while((ch= _______)!='e'){
System._______;
}
}catch(IOException Joe){
System._______;
}
}
}
第6题
以下程序运行后输出的结果是______。
public class exl7
{
public static void main(String args [])
{
int a = 0;
for(int j = 1; j <= 20; j++)
if(j%5 != 0)
a = a + j;
System.out.println (a);
第9题
l( public static void main(String[]args){ ; str="China"; int n=0: ; char C; ; System.out.println("字符串中共有"+n+"个字 符,最后一个字符是:"+c); } }
第10题
标签中文本的颜色为黄色、蓝色和红色,单击任意一个按钮,文字标签中的文本就变成按钮对应的颜色。 import java.awt.*; import java.awt.event.*; import javflx.swing.*; class ButtonPanel extends JPanel implements ActionL- istener{ public ButtonPanel{ yellowButton=new J Button("Yellow"); blueButton=new JButton("Blue"); redButton=new JButton("Red"); j1=new JLabel("I am from China!"); add(yellowButton); add(blueButton); add(redButton); add(j1); yellowButtofl.addActionListener(this); blueButton.addActionListener(this); redButton.addActionListener(this); } public void actionPerformed(ActionEvent evt){ 0bject source=evt.getSource; Color color=getForeground; if(source= =yellowButton)color=Color. yellow; else if(source= =blueButton)color=Color. blue; else if(source= =redButton)color= Color.red; ; ; } private JButton yellowButton; private JButton blueButton; private JButton redButton; private JLabel jl; } class ButtonFrame. extends JFrame{ public ButtonFrame{ setTitle("exam l6"); setSize(300,200); addWindowListener(new WindowAdapter{ public void windowClosing(WindowEvent e){ System.exit(O); } }); Container contentPane=getContentPane; contentPane.add(new ButtonPanel); } } public class java2{ public static void main(String[]args){ JFrame. frame=new ButtonFrame; frame.show; } }
为了保护您的账号安全,请在“上学吧”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!