1、二级 JAVA 机试-204 及答案解析(总分:100.00,做题时间:90 分钟)一、1基本操作题(总题数:1,分数:30.00)1.本题程序的功能是读入运行程序时所传入的参数(一个或多个),并将参数依次显示出来。例如运行程序“lava basic par1 par2”,则打印输出为“par1 par2”。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。public class basicpublic static void main(String args)int i = 0;while (_)System.out.print(_+ “);_;System.out.pri
2、ntln();(分数:30.00)_二、2简单应用题(总题数:1,分数:40.00)2.本题程序的功能是利用单选按钮选择性别,并在下面显示用户的选择结果。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。import java.awt.*;_;public class simplepublic static void main (String args)Frame f = new Frame (“simple“);f.setLayout (new GridLayout(3,1);f.addWindowListener(new WindowAdapter() public voi
3、d windowClosing(WindowEvent e)System.exit (0););CheckboxGroup cg = new CheckboxGroup();Checkbox male = new Checkbox(“男“,cg,false);Checkbox female = new Checkbox(“女“,cg,false);final Label la = new Label(“请选择你的性别“);male.addItemListener(new ItemListener() public void itemStateChanged(ItemEvent e)la.set
4、Text (“你是个男生“););female.addItemListener(new ItemListener() public void itemStateChanged(ItemEvent e)la.setText (“你是个女生“););f.add(male);f.add(female);f.add(la);f.setSize(200,200);_;(分数:40.00)_三、3综合应用题(总题数:1,分数:30.00)3.本题程序的功能是监听对于列表项的操作。窗口中有一个列表和“添加”、“删除”和“关闭”三个按钮。单击“添加”按钮,会在当前所选列表项后添加一个名为“新增表项”的列表项,
5、同时后台输入列表中的表项数量。单击“删除”按钮,如果未选中表项,则弹出提示消息框“请选择表项”,否则将选中的表项删除,同时后台输出删除表项的内容和列表中的表项数量。单击“关闭”按钮退出程序。程序中存在若干错误,请找出并改正(注意:不得改动程序的结构,不得增行或删行)。import java.awt.*;import java.awt.event.*;import javax.swing.JOptionPane;public class advancepublic static void main(String args)final Frame frmFrame = new Frame();Pa
6、nel pnlPanel1 = new Panel();Panel pnlPanel2 = new Panel();final List istList = new List(8);for(int i = 0; i 10; i+)String strName = “表项“ + (new Integer(i+1).toString();istList.add( strName );Button btnButton1 = new Button(“添加“);Button btnButton2 = new Button(“删除“);Button btnButton3 = new Button(“关闭“
7、);btnButton1.addActionListener( new ActionListener() publiC void actionPerformed(ActionEvent e)lstList.add(“新增表项“,istList.getSelected()+1);System.out.println(“列表中的表项数量“ + istList,getItemCount() ););btnButton2.addActionListener (new ActionListener () public void actionPerformed(ActionEvent e)if(istLi
8、st.getSelected() = null)JOptionPane.showMessageDialog(frmFrame,“请选择表项“);return;System.out.println(“删除表项的内容:“+ istList,getSelectedItem();istList.delete(istList.getSelectedIndex();System.out.println(“列表中的表项数量: “ + istList,getItemCount (););btnButton3.addActionListener(new ActionListener () public void
9、 actionPerformed(ActionEvent e)System.exit (0););pnlPanel1 .add (istList);pnlPanel2 .add (btnButton1);pnlPanel2 .add (btnButton2);pnlPanel2,add (btnButton3);frmFrame.add(“North“,pnlPanel1);frmFrame.add(“South“,pnlPanel2);frmFrame,setTitle (“advance“);frmFrame.pack ();frmFrame,show ();(分数:30.00)_二级 J
10、AVA 机试-204 答案解析(总分:100.00,做题时间:90 分钟)一、1基本操作题(总题数:1,分数:30.00)1.本题程序的功能是读入运行程序时所传入的参数(一个或多个),并将参数依次显示出来。例如运行程序“lava basic par1 par2”,则打印输出为“par1 par2”。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。public class basicpublic static void main(String args)int i = 0;while (_)System.out.print(_+ “);_;System.out.println(
11、);(分数:30.00)_正确答案:(iargs.length。argsi。i+。)解析:二、2简单应用题(总题数:1,分数:40.00)2.本题程序的功能是利用单选按钮选择性别,并在下面显示用户的选择结果。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。import java.awt.*;_;public class simplepublic static void main (String args)Frame f = new Frame (“simple“);f.setLayout (new GridLayout(3,1);f.addWindowListener(new
12、 WindowAdapter() public void windowClosing(WindowEvent e)System.exit (0););CheckboxGroup cg = new CheckboxGroup();Checkbox male = new Checkbox(“男“,cg,false);Checkbox female = new Checkbox(“女“,cg,false);final Label la = new Label(“请选择你的性别“);male.addItemListener(new ItemListener() public void itemStat
13、eChanged(ItemEvent e)la.setText (“你是个男生“););female.addItemListener(new ItemListener() public void itemStateChanged(ItemEvent e)la.setText (“你是个女生“););f.add(male);f.add(female);f.add(la);f.setSize(200,200);_;(分数:40.00)_正确答案:(importjava.awt.event.*。f.setVisible(true)。)解析:三、3综合应用题(总题数:1,分数:30.00)3.本题程序
14、的功能是监听对于列表项的操作。窗口中有一个列表和“添加”、“删除”和“关闭”三个按钮。单击“添加”按钮,会在当前所选列表项后添加一个名为“新增表项”的列表项,同时后台输入列表中的表项数量。单击“删除”按钮,如果未选中表项,则弹出提示消息框“请选择表项”,否则将选中的表项删除,同时后台输出删除表项的内容和列表中的表项数量。单击“关闭”按钮退出程序。程序中存在若干错误,请找出并改正(注意:不得改动程序的结构,不得增行或删行)。import java.awt.*;import java.awt.event.*;import javax.swing.JOptionPane;public class a
15、dvancepublic static void main(String args)final Frame frmFrame = new Frame();Panel pnlPanel1 = new Panel();Panel pnlPanel2 = new Panel();final List istList = new List(8);for(int i = 0; i 10; i+)String strName = “表项“ + (new Integer(i+1).toString();istList.add( strName );Button btnButton1 = new Button
16、(“添加“);Button btnButton2 = new Button(“删除“);Button btnButton3 = new Button(“关闭“);btnButton1.addActionListener( new ActionListener() publiC void actionPerformed(ActionEvent e)lstList.add(“新增表项“,istList.getSelected()+1);System.out.println(“列表中的表项数量“ + istList,getItemCount() ););btnButton2.addActionLis
17、tener (new ActionListener () public void actionPerformed(ActionEvent e)if(istList.getSelected() = null)JOptionPane.showMessageDialog(frmFrame,“请选择表项“);return;System.out.println(“删除表项的内容:“+ istList,getSelectedItem();istList.delete(istList.getSelectedIndex();System.out.println(“列表中的表项数量: “ + istList,g
18、etItemCount (););btnButton3.addActionListener(new ActionListener () public void actionPerformed(ActionEvent e)System.exit (0););pnlPanel1 .add (istList);pnlPanel2 .add (btnButton1);pnlPanel2 .add (btnButton2);pnlPanel2,add (btnButton3);frmFrame.add(“North“,pnlPanel1);frmFrame.add(“South“,pnlPanel2);
19、frmFrame,setTitle (“advance“);frmFrame.pack ();frmFrame,show ();(分数:30.00)_正确答案:(第 23 行的“lstList.getSelected()+1“改为“lstList.getSelectedIndex()+1“。第 30 行的“lstList.getSelected()=null“改为“lstList.getSelectedItem()=null“。第 36 行的“lstList.delete(lstList.getSelectedIndex()“改为“lstList.remove(lstList.get SelectedIndex()“。)解析: