四、程序填空题(本大题共5小题,每空2分,共20分) 在每小题的空格中填上正确答案,完全代码,使它能正确实现题意的功能。错填、不填均无分。 27.方法int tenDigNumlnArray(int []a,int d)的功能是统计数组中十位数是d的整数的个数。 提示:对于一个整数n,运算n/10的作用是求得去掉n的个位数后的整数,运算n%10的作用是求得n的个位数。 static int tenDigNumlnArray(int []a,int d){ int c=0; for(int i=0;__________;i++){ if(__________)c++; } return c; } 28.某小应用程序的类实现了接口ActionListener,在其窗口有一个文本区对象text和一个标上文字“输入一个整数”的按钮对象,并正确为该按钮对象注册了监视器。按钮处理程序要实现的功能是当点击这个按钮时,程序弹出一个输入信息的对话框,输入一个整数n,程序接受n后,将2至n的所有偶数输出在文本区text中。以下是其中处理按钮事件的方法。 public void actionPerformed(ActionEvent e){ if(e.____________.equals("输入一个整数")){ String res=(String) JOptionPane.showInputDialog(null, "输入一个正整数", "输入对话框",JOptionPane.PLAIN_MESSAGE,null,null,null); n=Integer.parseInt(res); text.setText("");. for(int k=2;k <= n;k+=2) text._____________; } } 29.一个示意选择框选择的类CheckBoxWin,类的构造方法根据给定的选择项目表构造一组选择框,这组选择框允许多选,当这组选择框中的某个选择项的选择状态有改变(从选中变成未选中,或从未选中变成选中)时,选择框的监视程序在一个文本区中输出那个状态有改变的选择项目。以下是类CheckBoxWin的定义。 class CheckBoxWin extends JFrame implements ItemListener{ JPanel p=new JPanel(); JTextArea text; String[]nameList; JCheckBox boxArray[]; CheckB oxWin(String[]sp){ Container con=getContentPane(); con.setBackground(Color.BLUE); con.setLayout(new FlowLayout()); p.setSize(90,(sp.1ength)*20); p.setLayout(new GridLayout(sp.1ength,1)); nameList=sp; boxArray=new JCheckB ox[sp.1ength]; for(int i=0;i<nameList.1ength;i++){ JCheckBox box=new JCheckBox(sp[i]); box._________________; p.add(box); boxArray[i]=box; } con.add(p); text=new JTextArea(3,13); text.setText(""); JScrollPane jsp=new JScrollPane(text); jsp.setSize(100,60); con.add(jsp); setSize(100,(sp.1ength)*20+120); setLocation(100,100); setVisible(true); } public void itemStateChanged(ItemEvent e){ for(int i=0;i<nameList.1ength;i++){ if(e.getItemSelectable()==_______________________ ){ if(boxArray[i].isSelected()) text.append(nameList[i]+".从未选中变成被选中\n"); else text.append(nameList[i]+":从选中变成未被选中\n"); return; } } } }
|