java程序设计简明教程上机练习题参考答案Word格式文档下载.docx
《java程序设计简明教程上机练习题参考答案Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《java程序设计简明教程上机练习题参考答案Word格式文档下载.docx(91页珍藏版)》请在冰豆网上搜索。
=9;
i++){
for(j=1;
j<
j++){
str=i+"
*"
+j+"
="
+i*j;
if(i<
j)
System.out.print("
);
else
System.out.print(str.length()==5?
str+"
:
}
System.out.println("
"
}
//文件XiTi1_1_2.java
publicclassXiTi1_1_2{
if(i>
//文件XiTi1_1_3.java
publicclassXiTi1_1_3{
2.定义两个数组,首先将第一个数组中的元素复制到第二个数组中,然后将第二个数数组进行从大到小的排序,最后将两个数组中的对应元素进行比较,试统计两个数组中对应位置上相同元素的个数。
//文件XiTi1_2.java
publicclassXiTi1_2{
publicstaticvoidmain(String[]args){
inta[]={278,109,063,930,589,184,505,269,8,83};
intb[]=newint[10];
intt,cnt=0;
for(inti=0;
10;
i++)
b[i]=a[i];
for(inti=1;
i++)
for(intj=0;
10-i;
j++)
if(b[j]<
b[j+1]){
t=b[j];
b[j]=b[j+1];
b[j+1]=t;
if(a[i]==b[i])
cnt++;
*********************a**********************"
System.out.print(a[i]+"
*********************b**********************"
System.out.print(b[i]+"
对应元素相同的个数为:
+cnt);
3.编写一个简单的考试程序。
//文件XiTi1_3.java
importjava.util.Date;
publicclassXiTi1_3{
publicstaticvoidmain(String[]args){
Stringyes="
Y"
ok="
intx,x1,y,opr=2,s,h,m,t1,t2;
Stringop,str="
re="
a="
A"
do{
x=newLong(Math.round(Math.random()*99+1)).intValue();
y=newLong(Math.round(Math.random()*99+1)).intValue();
opr=newLong(Math.round(Math.random()*3+1)).intValue();
s=newDate().getSeconds();
h=newDate().getHours();
m=newDate().getMinutes();
t1=h*3600+m*60+s;
switch(opr){
case1:
str=x+"
+"
+y+"
=?
re=Integer.toString(x+y);
break;
case2:
-"
re=Integer.toString(x-y);
case3:
re=Integer.toString(x*y);
case4:
x1=Math.max(x,y);
y=Math.min(x,y);
x=x1;
x=x-x%y;
/"
re=Integer.toString(x/y);
System.out.print(str);
try{
BufferedReaderin
=newBufferedReader(newInputStreamReader(System.in));
ok=in.readLine();
}catch(IOExceptione){};
t2=h*3600+m*60+s;
if(ok.equals(re.trim()))
正确!
用时:
+(t2-t1)+"
秒"
elseif(a.equals(ok.toUpperCase()))
答案:
+re+"
请多努力!
else
错误!
+re);
继续吗(Y/N)?
}while(yes.equals(ok.toUpperCase()));
上机练习题2
1.编写程序,包括一个文本域和三个命令按钮,按钮标题分别为红色、黄色和蓝色,单击相应的按钮,将文本域的前景色设置为红色、黄色和蓝色。
importjavax.swing.*;
importjavax.swing.event.*;
importjava.awt.event.*;
importjava.awt.*;
publicclassxiti2_1extendsJFrameimplementsActionListener{
JButtonred,yellow,blue;
JTextAreata=newJTextArea(5,20);
xiti2_1(){
red=newJButton("
红色"
yellow=newJButton("
黄色"
blue=newJButton("
蓝色"
red.addActionListener(this);
yellow.addActionListener(this);
blue.addActionListener(this);
JPaneljp=newJPanel();
jp.setLayout(newFlowLayout(FlowLayout.CENTER));
jp.add(red);
jp.add(yellow);
jp.add(blue);
Containercon=getContentPane();
con.add(ta,BorderLayout.CENTER);
con.add(jp,BorderLayout.SOUTH);
setSize(300,200);
setTitle("
上机练习题2_1"
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
validate();
publicvoidactionPerformed(ActionEvente){
JButtonbtn=(JButton)e.getSource();
if(btn==red){
ta.setForeground(Color.red);
if(btn==yellow){
ta.setForeground(Color.yellow);
if(btn==blue){
ta.setForeground(Color.blue);
newxiti2_1();
2.编写程序,包括一个文本框、一个密码框和两个按钮。
按钮标题分别为确定和取消,用户在密码框中输入密码,单击