Java二级操作题第28套
基本操作
在考生文件夹中存有文件名为Java_1.java的文件,该程序是不完整的,请在注释行"//Found"下一行语句的下划线地方填入正确内容,然后删除下划线,请勿删除注释行或改动其他已有语句内容。存盘时文件必须存放在考生文件夹下,不得改变原有文件的文件名。
本题的要求是:
该程序的功能是打印100~200之间能被9整除的数,并按照5个一行的格式输出。
完成程序并正常运行,运行结果为:
108 117 126 135 144
153 162 171 180 189
198
public class Java_1 {
public static void main(String args[]) {
int x,n;
//*********Found********
________________;
for( x = 100 ; x <= 200 ; x++)
if ( x % 9 == 0 ) {
//*********Found********
System.out.______________(" " + x);
n++;
//*********Found********
if ( ________________ ) System.out.println( );
}
}
}
本题考查的是余数。
根据题意可知,本例需要一个来控制每五个数字换行的变量,本例中循环内有一个n变量,且每次循环后做了自增操作,那么n即为控制换行的变量,所以第一空填写"n=0"。
Java中输出到控制台并且不换行的方法为print,所以第二空填写"print"。
本例中,if语句如果成立,则会执行pringln方法,即换行操作,为了使每五行完成一次换行操作,则需要用到第一空定义的变量,用此变量除以5求其余数,如果余数为0则表示正好是第五个数字,所以第三空填写"n%5==0"。
具体程序如下:
public class Java_1 {
public static void main(String args[]) {
int x,n;
//*********Found********
n=0;
for( x = 100 ; x <= 200 ; x++)
if ( x % 9 == 0 ) {
//*********Found********
System.out.print(" " + x);
n++;
//*********Found********
if ( n%5==0 ) System.out.println( );
}
}
}
简单应用
在考生文件夹中存有文件名为Java_2.java的文件,该程序是不完整的,请在注释行"//Found"下一行语句的下划线地方填入正确内容,然后删除下划线,请勿删除注释行或改动其他已有语句内容。存盘时文件必须存放在考生文件夹下,不得改变原有文件的文件名。
本题的要求是:
在考生文件夹中存有文件名为java_2.java文件,该程序功能是用于从键盘读入用户的个人信息,并存储到磁盘文件中去。具体要求:
1.以行的方式读入每个用户名及其密码信息,例如:user1 123456(用户和密码中间用一个空格隔开);
2.循环读入,直到用户输入“quit”或者“QUIT”结束;
3.程序结束前提示用户输入一个文件名来保存前面输入的所有用户名和密码信息。例如:请输入存储到的文件名:userlist.txt;
4.在整个上述过程中,要做例外处理;如果文件创建成功,则给出提示信息。
import java.io.*;
import java.util.Vector;
public class Java_2{
public static void main(String s[]){
Vector v=new Vector();
try{
//*********Found**********
BufferedReader in = new ____________________(new InputStreamReader(System.in)); //键盘输入
String str = "";
System.out.println("请输入用户和密码信息,中间用空格隔开,输入quit退出:");
while (!(str.equals("quit")||str.equals("QUIT"))){
str = in.readLine();
//*********Found**********
if(isValid(______________________)) //验证输入是否有空格
v.add(str);
else{
if(!(str.equals("quit")||str.equals("QUIT")))
System.out.println("The string is NOT valid!");
}
}
System.out.println("请输入保存到的文件名:");
//*********Found**********
str=_______________.readLine();
String curDir = System.getProperty("user.dir");
File savedfile=new File(curDir+"\\"+str);
//*********Found**********
BufferedWriter out = new BufferedWriter(new FileWriter(___________________));
for(int i=0; i<v.size(); i++){
String tmp=(String)v.elementAt(i);
out.write(tmp);
//*********Found**********
out.write(____________________); //换行
}
out.close();
}
//*********Found**********
________________(Exception e){
System.out.print("ERROR:"+e.getMessage());
}
}
/**
* 判定输入的字符串是否符合规范
* @param s 输入的待校验的字符串
* @return 校验的结果,正确则返回为真
*/
public static boolean isValid(String s){
if(s.indexOf(" ")>0) return true;
else return false;
}
}
本题考查的是IO缓存流。
BufferedReader是一个缓存阅读器类,并非接口,可以实例化,所以第一空填写"BufferedReader"。
根据题意得知,需要输入用户名和密码,中间用空格隔开,isValid的方法可以看出是验证是否有空格,没有空格则会判断是否退出,所以需要验证的字符串为str,即用户自己输入的信息,所以第二空填写"str"。
判断完成后接着给str赋值下一行的内容,所以第三空填写"in"。
FileWriter是一个文件写入类,根据题目上下文得知,这里需要保存一个文件,所以第四空填写"savedfile"。
文本换行符为"\n",所以第五空填写:""\n""。
捕捉异常的关键字是catch,所以第六空填写"catch"。
具体程序如下:
import java.io.*;
import java.util.Vector;
public class Java_2{
public static void main(String s[]){
Vector v=new Vector();
try{
//*********Found**********
BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); //键盘输入
String str = "";
System.out.println("请输入用户和密码信息,中间用空格隔开,输入quit退出:");
while (!(str.equals("quit")||str.equals("QUIT"))){
str = in.readLine();
//*********Found**********
if(isValid(str)) //验证输入是否有空格
v.add(str);
else{
if(!(str.equals("quit")||str.equals("QUIT")))
System.out.println("The string is NOT valid!");
}
}
System.out.println("请输入保存到的文件名:");
//*********Found**********
str=in.readLine();
String curDir = System.getProperty("user.dir");
File savedfile=new File(curDir+"\\"+str);
//*********Found**********
BufferedWriter out = new BufferedWriter(new FileWriter(savedfile));
for(int i=0; i<v.size(); i++){
String tmp=(String)v.elementAt(i);
out.write(tmp);
//*********Found**********
out.write("\n"); //换行
}
out.close();
}
//*********Found**********
catch(Exception e){
System.out.print("ERROR:"+e.getMessage());
}
}
/**
* 判定输入的字符串是否符合规范
* @param s 输入的待校验的字符串
* @return 校验的结果,正确则返回为真
*/
public static boolean isValid(String s){
if(s.indexOf(" ")>0) return true;
else return false;
}
}
综合应用
在考生文件夹中存有文件名为Java_3.java的文件,该程序是不完整的,请在注释行"//Found"下一行语句的下划线地方填入正确内容,然后删除下划线,请勿删除注释行或改动其他已有语句内容。存盘时文件必须存放在考生文件夹下,不得改变原有文件的文件名。
本题的要求是:
在考生文件夹中存有文件名为java_3.java文件,该程序功能是完成:
1.单击鼠标右键,实现弹出式多级菜单,通过“color”选择颜色;
2.在文本框中根据选择的颜色设置输出字符的颜色;
3.单击鼠标右键,选择“exit”可以退出程序。
程序运行并选择红颜色所得结果如下: 
//*********Found**********
import javax.___________________.*;
import java.awt.event.*;
import java.awt.*;
//*********Found**********
public class Java_3 extends MouseAdapter implements _____________________
{
//*********Found**********
private __________________ pop;
private JMenu subPop;
private JMenuItem color;
private JMenuItem exit;
private JMenuItem red;
private JMenuItem blue;
private JTextArea textArea;
private JFrame frame;
public void initGUI()
{
pop=new JPopupMenu();
subPop=new JMenu("color");
red=new JMenuItem("red");
red.addActionListener(this);
blue=new JMenuItem("blue");
blue.addActionListener(this);
subPop.add(red);
subPop.add(blue);
exit=new JMenuItem("exit");
exit.addActionListener(this);
pop.add(subPop);
pop.add(exit);
//*********Found**********
frame=new JFrame("____________________");
textArea=new JTextArea("",10,10);
textArea.addMouseListener(this);
frame.getContentPane().add(textArea);
frame.setSize(300,300);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter()
{
//*********Found**********
public void windowClosing(_____________________ e)
{
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent event)
{
if(event.getSource()==red)
{
textArea.setForeground(Color.red);
textArea.setText("red menu is selected");
}
else if(event.getSource()==blue)
{
textArea.setForeground(Color.blue);
textArea.setText("blue menu is selected");
}
else if(event.getSource()==exit)
{
frame.setVisible(false);
System.exit(0);
}
}
public void mousePressed(MouseEvent e)
{
if(e.getModifiers()==e.BUTTON3_MASK)
{
pop.show(e.getComponent(),e.getX(),e.getY());
}
}
public static void main(String args[])
{
Java_3 example=new Java_3();
example.initGUI();
}
}
本题考查的是JavaSwing。
本题中,窗口的输出等用到了javax.swing包下的类,所以第一空填写"swing"。
实现ActionListener接口,所以第二空填写"ActionListener"。
JPopupMenu是一个右击弹出菜单选项的组件,所以pop应为JPopupMenu的实例化对象,所以第三空填写"JPopupMenu"。
JFrame的构造器中,传入字符串意为窗口标题名,由题目得知标题为popup frame,所以第四空填写"popup frame"。
在fram的监听事件中,windowClosing的参数为WindowEvent对象,所以第五空填写"WindowEvent"。
具体程序如下:
``java
//Found*
import javax.swing.;
import java.awt.event.;
import java.awt.*;
//Found*
public class Java_3 extends MouseAdapter implements ActionListener
{
//Found*
private JPopupMenu pop;
private JMenu subPop;
private JMenuItem color;
private JMenuItem exit;
private JMenuItem red;
private JMenuItem blue;
private JTextArea textArea;
private JFrame frame;
public void initGUI()
{
pop=new JPopupMenu();
subPop=new JMenu("color");
red=new JMenuItem("red");
red.addActionListener(this);
blue=new JMenuItem("blue");
blue.addActionListener(this);
subPop.add(red);
subPop.add(blue);
exit=new JMenuItem("exit");
exit.addActionListener(this);
pop.add(subPop);
pop.add(exit);
//*********Found**********
frame=new JFrame("popup frame");
textArea=new JTextArea("",10,10);
textArea.addMouseListener(this);
frame.getContentPane().add(textArea);
frame.setSize(300,300);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter()
{
//*********Found**********
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent event)
{
if(event.getSource()red)
{
textArea.setForeground(Color.red);
textArea.setText("red menu is selected");
}
else if(event.getSource()blue)
{
textArea.setForeground(Color.blue);
textArea.setText("blue menu is selected");
}
else if(event.getSource()==exit)
{
frame.setVisible(false);
System.exit(0);
}
}
public void mousePressed(MouseEvent e)
{
if(e.getModifiers()==e.BUTTON3_MASK)
{
pop.show(e.getComponent(),e.getX(),e.getY());
}
}
public static void main(String args[])
{
Java_3 example=new Java_3();
example.initGUI();
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!