Java二级操作题第26套
基本操作
在考生文件夹中存有文件名为Java_1.java的文件,该程序是不完整的,请在注释行"//Found"下一行语句的下划线地方填入正确内容,然后删除下划线,请勿删除注释行或改动其他已有语句内容。存盘时文件必须存放在考生文件夹下,不得改变原有文件的文件名。
本题的要求是:
对该程序进行调试,使程序的输出结果如下:
abc
public class Java_1
{
//*********Found**********
public static void main(________________ []args)
{
char ch='d';
//*********Found**********
switch(_______________)
{
case 'a': System.out.print("a");break;
case 'b': System.out.print("b");
case 'c': System.out.print("c");break;
//*********Found**********
___________________: System.out.print("abc");
}
}
}
本题考查的是switch。
主函数参数为字符串,所以第一空填写"String"。
Switch的语法中,参数为需要选择判断的对象,本题需要判断的是ch变量,所以第二空填写"ch"。
Switch中当所有的case都不满足时,可以加入一个默认的结果default,所以第三空填写"default"。
具体程序如下:
public class Java_1
{
//*********Found**********
public static void main(String []args)
{
char ch='d';
//*********Found**********
switch(ch)
{
case 'a': System.out.print("a");break;
case 'b': System.out.print("b");
case 'c': System.out.print("c");break;
//*********Found**********
default: System.out.print("abc");
}
}
}
简单应用
在考生文件夹中存有文件名为Java_2.java的文件,该程序是不完整的,请在注释行"//Found"下一行语句的下划线地方填入正确内容,然后删除下划线,请勿删除注释行或改动其他已有语句内容。存盘时文件必须存放在考生文件夹下,不得改变原有文件的文件名。
本题的要求是:
1.以行的方式读入每个用户名及其密码信息,例如:user1 123456 (用户名和密码之间用一个空格隔开);
2.循环读入,直到用户输入“quit”或者“QUIT”结束;
3.程序结束前提示用户输入一个文件名来保存前面输入的所有用户名和密码。
//*********Found**********
import java.__________________.*;
import java.util.Vector;
public class Java_2
{
public static void main(String args[])
{
Vector v=new Vector();
try
{
//*********Found**********
BufferedReader in = new BufferedReader(new InputStreamReader(___________________));
String str = "";
System.out.println("请输入用户和密码信息,中间用空格隔开,输入quit退出:");
//*********Found**********
while (!(str.equals("_________________")||str.equals("QUIT")))
{
str = in.readLine();
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("请输入保存到的文件名:");
str=in.readLine();
String curDir = System.getProperty("user.dir");
File savedfile=new File(curDir+"\\"+ str );
BufferedWriter out = new BufferedWriter(new FileWriter(savedfile));
for(int i=0; i<v.size(); i++)
{
String tmp=(String)v.elementAt(i);
//*********Found**********
out.write(__________________);
out.write("\n");
}
out.close();
}
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;
}
}
本题考查的是IO。
由InputStreamReader对象得知,本题应引入java.io下的类,所以第一空填写"io"。
InputStreamReader是字节流通向字符流的桥梁,构造器为一个参数时,即填入System.in,所以第二空填写"System.in"。
根据题意得知,当用户输入quit或其大写时,退出程序,所以第三空填写"quit"。
out.write是将内容写入文件,根据上下文可得知变量tmp即为需要写入的内容,所以第四空填写"tmp"。
具体程序如下:
//*********Found**********
import java.io.*;
import java.util.Vector;
public class Java_2
{
public static void main(String args[])
{
Vector v=new Vector();
try
{
//*********Found**********
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String str = "";
System.out.println("请输入用户和密码信息,中间用空格隔开,输入quit退出:");
//*********Found**********
while (!(str.equals("quit")||str.equals("QUIT")))
{
str = in.readLine();
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("请输入保存到的文件名:");
str=in.readLine();
String curDir = System.getProperty("user.dir");
File savedfile=new File(curDir+"\\"+ str );
BufferedWriter out = new BufferedWriter(new FileWriter(savedfile));
for(int i=0; i<v.size(); i++)
{
String tmp=(String)v.elementAt(i);
//*********Found**********
out.write(tmp);
out.write("\n");
}
out.close();
}
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"下一行语句的下划线地方填入正确内容,然后删除下划线,请勿删除注释行或改动其他已有语句内容。存盘时文件必须存放在考生文件夹下,不得改变原有文件的文件名。
本题的要求是:
1.单击鼠标右键,实现弹出式多级菜单,通过“color”选择颜色;
2.在文本框中根据选择的颜色设置输出字符的颜色;
3.单击鼠标右键,选择“exit”可以退出程序。
程序运行上述第一项功能后,界面如下: 
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
//*********Found**********
public class Java_3 extends ______________________ implements ActionListener
{
//*********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");
//*********Found**********
red=new JMenuItem("______________________");
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);
frame=new JFrame("popup frame");
textArea=new JTextArea("",10,10);
textArea.addMouseListener(this);
//*********Found**********
frame.getContentPane().add(____________________);
frame.setSize(300,300);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent event)
{
if(event.getSource()==red)
{
//*********Found**********
textArea.setForeground(Color._____________________);
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。
根据题意得知需要在窗口进行点击操作,所以该例中的类需要继承MouseAdapter类,所以第一空填写"MouseAdapter"。
根据代码上下文得知,pop对象是一个含有退出功能的菜单,而JPopupMenu则是鼠标右击时产生的菜单栏,所以第二空填写"JPopupMenu"。
根据题意得知,本例中将会出现两种颜色,红色和蓝色,蓝色在代码中已经体现,所以第三空填写"red"。
textArea对象是一个文本域,根据题意得知,该文本域需要加入到容器框架中,所以第四空填写"textArea"。
在点击事件变色的功能中,第一行判断了是否等于红色,所以第五空填写"red"。
具体程序如下:
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");
//*********Found**********
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);
frame=new JFrame("popup frame");
textArea=new JTextArea("",10,10);
textArea.addMouseListener(this);
//*********Found**********
frame.getContentPane().add(textArea);
frame.setSize(300,300);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent event)
{
if(event.getSource()==red)
{
//*********Found**********
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首个预览版发布:重大改进与新特性概览!