Java二级操作题第44套

基本操作

在考生文件夹中存有文件名为Java_1.java的文件,该程序是不完整的,请在注释行"//Found*"下一行语句的下划线地方填入正确内容,然后删除下划线,请勿删除注释行或其他已有语句内容。存盘时文件必须存放在考生文件夹下,不得改变原有文件的文件名。

下列程序的功能是:求从0到10的阶乘,并计算它们的和。程序的运行结果如下:
0!=1
1!=1
2!=2
3!=6
4!=24
5!=120
6!=720
7!=5040
8!=40320
9!=362880
10!=3628800
sum=4037914

import java.util.*;
import java.io.*;
public class Java_1 {
        //**********found**********
        static long _________ =0;
        public static void main(String[] args) {
                 //**********found**********
                 for (int ___________ ; counter <= 10; counter++){
                       System.out.printf("%d! = %d\n", counter, factorial(counter));
                       sum=sum+factorial(counter);
                 }
                 System.out.println("sum="+sum);
        }
        //**********found**********
        public static long factorial(long __________ ) {
                 if (number <= 1)
                       return 1;
                 else
                       return number * factorial(number - 1);
        }
}

本题中的第一个空格:题目中要求的是阶乘的和sum,由下面的代码可知没有定义变量sum,此处填写sum;
本题中的第二个空格:程序中的for循环的作用是输出从0到10的阶乘,所以该循环的开始条件是0,结束条件是10,此处填写counter=0;
本题中的第三个空格:factorial()函数缺少参数,而函数主体中的参数number未定义,可知此处填写number;

本评析仅作参考。

import java.util.*;
import java.io.*;
public class Java_1 {
        //**********found**********
        static long sum =0;
        public static void main(String[] args) {
                 //**********found**********
                 for (int counter = 0 ; counter <= 10; counter++){
                       System.out.printf("%d! = %d\n", counter, factorial(counter));
                       sum=sum+factorial(counter);
                 }
                 System.out.println("sum="+sum);
        }
        //**********found**********
        public static long factorial(long number ) {
                 if (number <= 1)
                       return 1;
                 else
                       return number * factorial(number - 1);
        }
}

简单应用

在考生文件夹中存有文件名为Java_2.java的文件,该程序是不完整的,请在注释行"//Found*"下一行语句的下划线地方填入正确内容,然后删除下划线,请勿删除注释行或其他已有语句内容。存盘时文件必须存放在考生文件夹下,不得改变原有文件的文件名。

下列程序的功能是:基于Java文件输入输出流技术,实现将in.txt文件的内容复制到out.txt文件中。

import java.io.*;
public class Java_2 {
    public static void main(String[] args) {
         try {
            //**********found**********
            FileInputStream in = new FileInputStream("____________");
            FileOutputStream out = new FileOutputStream("out.txt");
            BufferedInputStream bufferedIn = new BufferedInputStream(in);
            //**********found**********
            BufferedOutputStream bufferedOut = new ____________(out);
            //**********found**********
            byte[] data = new _____________[1];
            //**********found**********
            _____________ (bufferedIn.read(data) != -1) {
                  bufferedOut.write(data);
            }
            bufferedOut.flush();
            bufferedIn.close();
            //**********found**********
            ___________.close();
        //**********found**********
        } _____________ (ArrayIndexOutOfBoundsException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

本题中的第一个空格:使用FileInputStream类读取硬盘中的文件in.txt,将其写入FileOutputStream类定义的对象out中,所以第一空填写"in.txt";
本题中的第二个空格:使用缓冲输入流和缓冲输出流实现文件的复制,将输入流中的数据通过缓冲输出流写入目标文件"out.txt"中,所以第二空填写"BufferedOutputStream";
本题中的第三空:新建一个byte型数组data,存放读取的数据,所以第三空填写"byte";
本题中的第四空:BufferedInputStream类中的read()方法一次读取一个数组data,所以需要使用循环结构来判断是否到达文件的末尾,也就是当读取到的数据不为空时,执行循环将读取到的内容利用缓冲输出流写入文件,所以第四空填写"while";
本题中的第五空:使用close()方法关闭缓冲输入输出流,所以第五空填写"bufferedOut";
本题中的第六空:在Java中可以通过try-catch-finally结构对异常进行捕获和处理,其中finally块是个可选项,所以第六空填写"catch"。

本评析仅作参考。

import java.io.*;
public class Java_2 {
    public static void main(String[] args) {
         try {
            //**********found**********
            FileInputStream in = new FileInputStream("in.txt");
            FileOutputStream out = new FileOutputStream("out.txt");
            BufferedInputStream bufferedIn = new BufferedInputStream(in);
            //**********found**********
            BufferedOutputStream bufferedOut = new BufferedOutputStream(out);
            //**********found**********
            byte[] data = new byte[1];
            //**********found**********
            while (bufferedIn.read(data) != -1) {
                  bufferedOut.write(data);
            }
            bufferedOut.flush();
            bufferedIn.close();
            //**********found**********
            bufferedOut.close();
        //**********found**********
        } catch (ArrayIndexOutOfBoundsException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

综合应用

在考生文件夹中存有文件名为Java_3.java的文件,该程序是不完整的,请在注释行"//Found*"下一行语句的下划线地方填入正确内容,然后删除下划线,请勿删除注释行或其他已有语句内容。存盘时文件必须存放在考生文件夹下,不得改变原有文件的文件名。

下列程序的功能是:实现了QQ登录的界面。当输入用户名和密码并点击“登录”按钮后,显示确认对话框
确认输入的用户名是否正确。程序运行结果如下两图所示。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

//**********found**********
public class Java_3 extends ___________ {
    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正式版");
        //**********found**********
        _____________();
        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);
        this.setVisible(true);
    }

    public void init() {
        //**********found**********
        Container con = this.______________();
        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);

        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);
        //**********found**********
        bu1 = new ______________("登录");
        bu1.setBounds(250, 200, 65, 20);
        //**********found**********
        bu1._____________________(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String str=e.getActionCommand();
                if("登录".equals(str)){
                     String getName =username.getText();
                     JOptionPane.showConfirmDialog(null, "您输入的用户名是"+getName);
                }
            }
        });
        bu2 = new JButton("多账号");
        bu2.setBounds(25, 200, 75, 20);
        bu3 = new JButton("设置");
        bu3.setBounds(140, 200, 65, 20);
        // 所有组件用容器装载
        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);
    }
    public static void main(String[] args) {
        Java_3 qq = new Java_3();
    }
}

本题中的第一个空格:类Java_3()构造函数中有代码:"setTitle(“QQ2022正式版”);“用于设置应用程序窗体的标题"QQ2022正式版”,说明类Java_3是类JFrame的子类,此处填写JFrame;
本题中的第二个空格:构造函数Java_3()中设置了窗体的标题、大小等,缺少了相关的组件,所以这里需要调用init()函数,此处填写init;
本题中的第三个空格:JFrame不能直接添加组件,需先将组件添加到ContentPane中,然后使用add方法将组件显示到JFrame面板。JFrame提供了两个方法getContentPane和setContentPane就是用于获取和设置其ContentPane的。此处填写getContentPane;
本题中的第四个空格:将定义好的JButton类对象bu1实例化,此处填写JButton;
本题中的第五个空格:JButton类对象bu1添加监听事件,此处填写addActionListener。

本评析仅作参考。

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正式版");
        //**********found**********
        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);
        this.setVisible(true);
    }

    public void init() {
        //**********found**********
        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);

        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);
        //**********found**********
        bu1 = new JButton("登录");
        bu1.setBounds(250, 200, 65, 20);
        //**********found**********
        bu1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String str=e.getActionCommand();
                if("登录".equals(str)){
                     String getName =username.getText();
                     JOptionPane.showConfirmDialog(null, "您输入的用户名是"+getName);
                }
            }
        });
        bu2 = new JButton("多账号");
        bu2.setBounds(25, 200, 75, 20);
        bu3 = new JButton("设置");
        bu3.setBounds(140, 200, 65, 20);
        // 所有组件用容器装载
        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);
    }
    public static void main(String[] args) {
        Java_3 qq = new Java_3();
    }
}
posted @   槑孒  阅读(454)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
点击右上角即可分享
微信分享提示