java程序设计简明教程上机练习题参考答案Word下载.docx
- 文档编号:20554643
- 上传时间:2023-01-24
- 格式:DOCX
- 页数:91
- 大小:49.76KB
java程序设计简明教程上机练习题参考答案Word下载.docx
《java程序设计简明教程上机练习题参考答案Word下载.docx》由会员分享,可在线阅读,更多相关《java程序设计简明教程上机练习题参考答案Word下载.docx(91页珍藏版)》请在冰豆网上搜索。
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.编写程序,包括一个文本框、一个密码框和两个按钮。
按钮标题分别为确定和取消,用户在密码框中输入密码,单击确定按钮,将在密码框中输入的字符显示在文本框中。
publicclassxiti2_2extendsJFrameimplementsActionListener{
JPanelp1=newJPanel();
JPanelp2=newJPanel();
JPanelp3=newJPanel();
JPanelp4=newJPanel();
JPanelp5=newJPanel();
JTextFieldtext=newJTextField(15);
JPasswordFieldpassword=newJPasswordField(15);
JButtonok=newJButton("
确定"
JButtoncancel=newJButton("
取消"
xiti2_2(){
con.setLayout(newGridLayout(5,1));
p2.add(newJLabel("
密码:
));
p2.add(password);
p3.add(newJLabel("
文本:
p3.add(text);
p4.add(ok);
p4.add(cancel);
ok.addActionListener(this);
cancel.addActionListener(this);
con.add(p1);
con.add(p2);
con.add(p3);
con.add(p4);
con.add(p5);
上机练习题2_2"
if(e.getSource()==ok){
text.setText(password.getText());
if(e.getSource()==cancel){
dispose();
System.exit(0);
}
newxiti2_2();
}
3.编写程序,建立一个带有菜单的窗体。
当用户选择“Color”或“Style”菜单的相关选项时,标签中文字的字体和颜色会发生相应的变化。
classxiti2_3extendsJFrameimplementsActionListener{
JMenuBarmenubar=newJMenuBar();
JTextAreata=newJTextArea("
HelloJava!
!
xiti2_3(){
setJMenuBar(menubar);
JMenumenu,submenu;
JMenuItemmenuItem;
menu=newJMenu("
Color"
menu.setMnemonic(KeyEvent.VK_C);
menubar.add(menu);
menuItem=newJMenuItem("
Yellow"
menuItem.setMnemonic(KeyEvent.VK_Y);
menuItem.addActionListener(this);
menu.add(menuItem);
Orange"
menuItem.setMnemonic(KeyEvent.VK_O);
Pink"
KeyEvent.VK_P);
Blue"
menuItem.setMnemonic(KeyEvent.VK_B);
Style"
menu.setMnemonic(KeyEvent.VK_S);
Plain"
menuItem.setMnemonic(KeyEvent.VK_P);
Bold"
Italic"
menuItem.setMnemonic(KeyEvent.VK_I);
Exit"
menu.setMnemonic(KeyEvent.VK_E);
CloseWindow"
menuItem.setMnemonic(KeyEvent.VK_W);
Containercp=getContentPane();
cp.add(ta,BorderLayout.SOUTH);
菜单演示"
if(e.getActionCommand()=="
)
ta.setForeground(Color.orange);
ta.setForeground(Color.pink);
ta.setFont(newFont("
Dialog"
Font.PLAIN,24));
Font.BOLD,24));
Font.ITALIC,24));
publicstaticvoidmain(Stringargs[]){
xiti2_3menuexmple=newxiti2_3();
4.编写程序,将窗口尺寸设置为不可更改,并处理窗口事件,使得点击窗口关闭按钮时,会弹出对话框,提示用户是否确定要关闭窗口。
classMyFrameextendsJFrame{
privateJLabellabel;
publicMyFrame(){
setTitle("
我的窗口"
label=newJLabel("
请点击关闭按钮!
JLabel.CENTER);
getContentPane().add(label);
//为窗口添加关闭事件
addWindowListener(newWindowCloseListener(this));
//将点击窗口关闭按钮默认的关闭窗口功能去掉
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
//设置窗口大小不可更改
setResizable(false);
classWindowCloseListenerextendsWindowAdapter{
JFramef;
publicWindowCloseListener(JFrameframe){
f=frame;
//f是对话框的父窗口
publicvoidwindowClosing(WindowEvente){
inti=JOptionPane.showConfirmDialog(f,"
确定要关闭窗口吗?
"
JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE);
if(i==JOptionPane.OK_OPTION){
//点击确定退出程序
publicstaticvoidmain(String[]args){
JFrameframe=newMyFrame();
5.编写程序,用列表框列出一些选项,设置一个按钮,点击按钮就会将所选的选项添加到另一个列表框中,其中第二个列表框中显示的已选项目可以删除。
程序运行界面如图2-16所示。
importjava.util.*;
classMyListextendsJFrame{
JPanelpanel;
JButtona
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java 程序设计 简明 教程 上机 练习题 参考答案