Java二级操作题第39套
基本操作
在考生文件夹中存有文件名为Java_1.java的文件,该程序是不完整的,请在注释行“//Found”下一行语句的下划线地方填入正确内容,然后删除下划线,请勿删除注释行或改动其他已有语句内容。存 盘时文件必须存放在考生文件夹下,不得改变原有文件的文件名。
本题的要求是:
完善该程序并进行调试,程序先显示一个输入窗口如下: 
import javax.swing.JOptionPane;
public class Java_1{
public static void main( String args[] ){
int x, result;
String xVal;
//*********Found**********
xVal = JOptionPane._____________________(
"输入1个整数:" );
//*********Found**********
x = Integer.___________________( xVal );
//*********Found**********
result = ______________;
JOptionPane.showMessageDialog(null,
"该数的平方是" + result );
System.exit( 0 );
}
}
本题着重考察考生对题目要求的理解情况。
本题中的第一个空格:程序要求弹出第一个对话框,因此使用showInputDialog()方法,因此此处空格填入showInputDialog;
本题中的第二个空格:此处未对非整数类型的数据作输入限制,因此将输入的数据作强制类型转换为整数,因此此处空格填入parseInt;
本题中的第三个空格:程序要求输出的是平方值,输出结果为result,因此此处空格填入x*x。
本评析仅作参考。
import javax.swing.JOptionPane;
public class Java_1{
public static void main( String args[] ){
int x, result;
String xVal;
//*********Found**********
xVal = JOptionPane.showInputDialog(
"输入1个整数:" );
//*********Found**********
x = Integer.parseInt( xVal );
//*********Found**********
result = x*x;
JOptionPane.showMessageDialog(null,
"该数的平方是" + result );
System.exit( 0 );
}
}
简单应用
在考生文件夹中存有文件名为Java_2.java的文件,该程序是不完整的,请在注释行“//Found”下一行语句的下划线地方填入正确内容,然后删除下划线,请勿删除注释行或改动其他已有语句内容。存盘时文件必须存放在考生文件夹下,不得改变原有文件的文件名。
本题的要求是:
对字符串进行缓存处理,程序运行的某次结果如下: 
import javax.swing.*;
public class Java_2{
public static void main( String args[] ){
StringBuffer buf = new StringBuffer( "你好!祝你成功!" );
String output = "buf = " + buf.toString() +
"\nCharacter at 0: " + buf.charAt( 0 ) +
"\nCharacter at 4: " + buf.charAt( 4 );
//*********Found**********
char charArray[] = _____________________ char[ buf.length() ];
//*********Found**********
____________.getChars( 0, buf.length(), charArray, 0 );
output += "\n\n在字符串缓存中的字符是: ";
//*********Found**********
for ( int i = 0; i < ______________________; ++i )
output += charArray[ i ];
buf.setCharAt( 0, '您' );
buf.setCharAt( 6, '材' );
//*********Found**********
__________ += "\n\nbuf = " + buf.toString();
buf.reverse( );
output += "\n\nbuf = " + buf.toString();
JOptionPane.showMessageDialog( null, output,
"字符串缓存的字符相关方法示范",
JOptionPane.INFORMATION_MESSAGE );
System.exit( 0 );
}
}
本题着重考察考生对题目要求的理解情况。
本题中的第一个空格:数组使用之前先对数组进行初始化,因此此处空格填入new;
本题中的第二个空格:要查看当前字符串的缓存字符,查看程序中只有对buf作了缓存,因此此处空格填入buf;
本题中的第三个空格:根据输出结果要求将第1位和第7位的字符进行更改,因此此处空格填入charArray.length;
本题中的第四个空格:output是最终输出的字符串,因此要将更改好的字符串放到output中,因此此处空格填入output。
本评析仅作参考。
import javax.swing.*;
public class Java_2{
public static void main( String args[] ){
StringBuffer buf = new StringBuffer( "你好!祝你成功!" );
String output = "buf = " + buf.toString() +
"\nCharacter at 0: " + buf.charAt( 0 ) +
"\nCharacter at 4: " + buf.charAt( 4 );
//*********Found**********
char charArray[] = new char[ buf.length() ];
//*********Found**********
buf.getChars( 0, buf.length(), charArray, 0 );
output += "\n\n在字符串缓存中的字符是: ";
//*********Found**********
for ( int i = 0; i < charArray.length; ++i )
output += charArray[ i ];
buf.setCharAt( 0, '您' );
buf.setCharAt( 6, '材' );
//*********Found**********
output += "\n\nbuf = " + buf.toString();
buf.reverse( );
output += "\n\nbuf = " + buf.toString();
JOptionPane.showMessageDialog( null, output,
"字符串缓存的字符相关方法示范",
JOptionPane.INFORMATION_MESSAGE );
System.exit( 0 );
}
}
综合应用
在考生文件夹中存有文件名为Java_3.java的文件,该程序是不完整的,请在注释行“//*Found”下一行语句的下划线地方填入正确内容,然后删除下划线,请勿删除注释行或改动其他已有语句内容。存盘时文件必须存放在考生文件夹下,不得改变原有文件的文件名。
下列程序的功能是:实现了QQ登录的界面。当输入用户名和密码并点击“设置”按钮后,如果“记住密码”框被选中,则显示确认对话框询问是否确认记住密码。程序运行结果如下两图所示。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//**********found**********
public class _________ extends JFrame {
private JTextField username;
private JPasswordField password;
private JLabel jl1;
private JLabel jl2;
private JLabel jl3;
private JLabel jl4;
private JButton bu1;
private JButton bu2;
private JButton bu3;
private JCheckBox jc1;
private JCheckBox jc2;
private JComboBox jcb;
public Java_3() {
this.setTitle("QQ2022正式版");
init();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 设置布局方式为绝对定位
this.setLayout(null);
this.setBounds(0, 0, 355, 265);
// 设置窗体的标题图标
Image image = new ImageIcon("a.png").getImage();
this.setIconImage(image);
// 窗体大小不能改变
this.setResizable(false);
// 居中显示
this.setLocationRelativeTo(null);
//**********found**********
this._______(true);
}
public void init() {
Container con = this.getContentPane();
jl1 = new JLabel();
// 设置背景图片
Image image1 = new ImageIcon("background.jpg").getImage();
jl1.setIcon(new ImageIcon(image1));
jl1.setBounds(0, 0, 355, 265);
jl2 = new JLabel();
Image image2 = new ImageIcon("a.gif").getImage();
jl2.setIcon(new ImageIcon(image2));
jl2.setBounds(40, 95, 50, 60);
//**********found**********
username = new ____________();
username.setBounds(50, 50, 150, 20);
jl3 = new JLabel("注册账号");
jl3.setBounds(210, 50, 70, 20);
password = new JPasswordField();
password.setBounds(50, 80, 150, 20);
jl4 = new JLabel("找回密码");
jl4.setBounds(210, 80, 70, 20);
jc1 = new JCheckBox("记住密码");
jc1.setBounds(125, 135, 80, 15);
jc2 = new JCheckBox("自动登录");
jc2.setBounds(215, 135, 80, 15);
jcb = new JComboBox();
jcb.addItem("在线");
jcb.addItem("隐身");
jcb.addItem("离开");
jcb.setBounds(40, 135, 55, 20);
bu1 = new JButton("登录");
bu1.setBounds(250, 200, 65, 20);
bu2 = new JButton("多账号");
bu2.setBounds(25, 200, 75, 20);
bu3 = new JButton("设置");
bu3.setBounds(140, 200, 65, 20);
bu3.addActionListener(new ActionListener() {
//**********found**********
public void ____________(ActionEvent e) {
if (jc1.isSelected()==true)
JOptionPane.showConfirmDialog(null,"确定记住密码吗?");
}
});
// 所有组件用容器装载
jl1.add(jl2);
jl1.add(jl3);
jl1.add(jl4);
jl1.add(jc1);
jl1.add(jc2);
jl1.add(jcb);
jl1.add(bu1);
jl1.add(bu2);
jl1.add(bu3);
con.add(jl1);
con.add(username);
con.add(password);
}
//**********found**********
public static void _________ (String[] args) {
Java_3 qq = new Java_3();
}
}
有一说一,还真挺不错的 hah
本题着重考察考生对题目要求的理解情况。
本题中的第一个空格:在java语言中类名和构造函数名称一致,因此此处空格填入Java_3;
本题中的第二个空格:JFrame创建完成后需要设置为可见才能显示出来,因此此处空格填入setVisible;
本题中的第三个空格:username对应的是一个普通的文本输入框,因此此处空格填入JTextField;
本题中的第四个空格:按钮的点击事件需要写在actionPerformed函数中,因此此处空格填入actionPerformed;
本题中的第五个空格:程序中缺少main函数,因此此处空格填入main。
本评析仅作参考。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//**********found**********
public class Java_3 extends JFrame {
private JTextField username;
private JPasswordField password;
private JLabel jl1;
private JLabel jl2;
private JLabel jl3;
private JLabel jl4;
private JButton bu1;
private JButton bu2;
private JButton bu3;
private JCheckBox jc1;
private JCheckBox jc2;
private JComboBox jcb;
public Java_3() {
this.setTitle("QQ2022正式版");
init();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 设置布局方式为绝对定位
this.setLayout(null);
this.setBounds(0, 0, 355, 265);
// 设置窗体的标题图标
Image image = new ImageIcon("a.png").getImage();
this.setIconImage(image);
// 窗体大小不能改变
this.setResizable(false);
// 居中显示
this.setLocationRelativeTo(null);
//**********found**********
this.setVisible(true);
}
public void init() {
Container con = this.getContentPane();
jl1 = new JLabel();
// 设置背景图片
Image image1 = new ImageIcon("background.jpg").getImage();
jl1.setIcon(new ImageIcon(image1));
jl1.setBounds(0, 0, 355, 265);
jl2 = new JLabel();
Image image2 = new ImageIcon("a.gif").getImage();
jl2.setIcon(new ImageIcon(image2));
jl2.setBounds(40, 95, 50, 60);
//**********found**********
username = new JTextField();
username.setBounds(50, 50, 150, 20);
jl3 = new JLabel("注册账号");
jl3.setBounds(210, 50, 70, 20);
password = new JPasswordField();
password.setBounds(50, 80, 150, 20);
jl4 = new JLabel("找回密码");
jl4.setBounds(210, 80, 70, 20);
jc1 = new JCheckBox("记住密码");
jc1.setBounds(125, 135, 80, 15);
jc2 = new JCheckBox("自动登录");
jc2.setBounds(215, 135, 80, 15);
jcb = new JComboBox();
jcb.addItem("在线");
jcb.addItem("隐身");
jcb.addItem("离开");
jcb.setBounds(40, 135, 55, 20);
bu1 = new JButton("登录");
bu1.setBounds(250, 200, 65, 20);
bu2 = new JButton("多账号");
bu2.setBounds(25, 200, 75, 20);
bu3 = new JButton("设置");
bu3.setBounds(140, 200, 65, 20);
bu3.addActionListener(new ActionListener() {
//**********found**********
public void actionPerformed(ActionEvent e) {
if (jc1.isSelected()==true)
JOptionPane.showConfirmDialog(null,"确定记住密码吗?");
}
});
// 所有组件用容器装载
jl1.add(jl2);
jl1.add(jl3);
jl1.add(jl4);
jl1.add(jc1);
jl1.add(jc2);
jl1.add(jcb);
jl1.add(bu1);
jl1.add(bu2);
jl1.add(bu3);
con.add(jl1);
con.add(username);
con.add(password);
}
//**********found**********
public static void main (String[] args) {
Java_3 qq = new Java_3();
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!