Java二级操作题第18套

基本操作

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

本题的要求是:
求下列矩阵中逆对角线上的元素(21 17 13 9 5)之和。
1 2 3 4 5
6 7 6 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
运行结果为:
65

public class Java_1 {
    //*********Found********
    public static void ________(String args[]) {
        int arr[][] = {{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}, {16, 17, 18, 19, 20}, {21, 22, 23, 24, 25}};
        //*********Found********
        int i, j, ____________;
        for (i = 0; i < 5; i++) 
            for (j = 0; j < 5; j++) 
                //*********Found********
                if (____________==4) 
                    sum += arr[i][j];
        System.out.println(sum);
    }
}

本题考查的是for循环语句。
具体程序如下:

public class Java_1 {
    //*********Found********
    public static void main(String args[]) {
        int arr[][] = {{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}, {16, 17, 18, 19, 20}, {21, 22, 23, 24, 25}};
        //*********Found********
        int i, j, sum=0;
        for (i = 0; i < 5; i++) 
            for (j = 0; j < 5; j++) 
                //*********Found********
                if (i+j==4) 
                    sum += arr[i][j];
        System.out.println(sum);
    }
}

简单应用

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

本题的要求是:
将四句歌词分行写入到test.txt文件中,然后从该文件读出所有内容并显示。
运行结果为:
第1行内容:在那山的那边海的那边有一群蓝精灵
第2行内容:它们活泼又聪明它们调皮又灵敏
第3行内容:它们自由自在生活在那绿色的大森林
第4行内容:它们善良勇敢相互都欢喜!

import java.io.*;

public class Java_2 {
    public static void main(String args[]) {
        String ShowMes[] = {"在那山的那边海的那边有一群蓝精灵", "它们活泼又聪明它们调皮又灵敏", "它们自由自在生活在那绿色的大森林", "它们善良勇敢相互都欢喜!"};
        try {
            //*********Found********
            FileWriter out = new FileWriter(____________);
            BufferedWriter outBW = new BufferedWriter(out);
            for (int i = 0; i < ShowMes.length; i++) {
                outBW.write(ShowMes[i]);
                outBW.newLine();
            }
            //*********Found********
            outBW.______________();
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            //*********Found********
            FileReader in = new _____________(new File("test.txt"));
            BufferedReader inBR = new BufferedReader(in);
            String stext = null;
            int j = 1;
            while ((stext = inBR.readLine()) != null) {
                System.out.println("第" + j + "行内容:" + stext);
                //*********Found********
                _______________;
            }
            inBR.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

本题考查的是流文件。
具体程序如下:

import java.io.*;

public class Java_2 {
    public static void main(String args[]) {
        String ShowMes[] = {"在那山的那边海的那边有一群蓝精灵", "它们活泼又聪明它们调皮又灵敏", "它们自由自在生活在那绿色的大森林", "它们善良勇敢相互都欢喜!"};
        try {
            //*********Found********
            FileWriter out = new FileWriter("test.txt");
            BufferedWriter outBW = new BufferedWriter(out);
            for (int i = 0; i < ShowMes.length; i++) {
                outBW.write(ShowMes[i]);
                outBW.newLine();
            }
            //*********Found********
            outBW.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            //*********Found********
            FileReader in = new FileReader(new File("test.txt"));
            BufferedReader inBR = new BufferedReader(in);
            String stext = null;
            int j = 1;
            while ((stext = inBR.readLine()) != null) {
                System.out.println("第" + j + "行内容:" + stext);
                //*********Found********
                j++;
            }
            inBR.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

综合应用

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

本题的要求是:
本题采用Swing编写了一个窗体,窗体包含“文件”菜单(其中还包含“打开图片”和“退出图片”两个子菜单)。当点击“打开图片”子菜单后,选择要显示的图片并将其显示在窗体中。点击“退出图片”子菜单后,系统退出。当前文件夹下包含了一个hello.jpg图片供使用。程序运行结果如下图所示。

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

public class Java_3 extends JFrame {
    private JLabel label;
    private JFileChooser fileChooser;
    private ImagePanel panel;
    public Java_3() {
        setTitle("图片浏览器");
        setSize(500, 400);
        fileChooser = new JFileChooser();
        fileChooser.setCurrentDirectory(new File("."));//设置默认路径为当前目录
        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);
        JMenu menu = new JMenu("文件");
        menuBar.add(menu);
        JMenuItem openItem = new JMenuItem("打开图片");
        menu.add(openItem);
        panel = new ImagePanel();
        add(panel);
        //*********Found********
        openItem._________________(new ActionListener(){
            public void actionPerformed(ActionEvent event){
                int result = fileChooser.showOpenDialog(null);
                if(result==JFileChooser.APPROVE_OPTION){
                    String name = fileChooser.getSelectedFile().getPath();
                    //*********Found********
                    panel.setImage(_____________);
                    panel.repaint();
                }
            }
        });
        JMenuItem exitItem = new JMenuItem("退出图片");
        menu.add(exitItem);
        exitItem.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent event){
                System.exit(0);
            }
        });        
    }

    public static void main(String[] args) {
        //*********Found********
        Java_3 frame = __________ Java_3 ();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //*********Found********
        frame.____________(true);
    }
}


//*********Found********
class ImagePanel extends ___________ {
    private Image image;
    private int showWidth;
    private int showHeight;
    public void setImage(String fileName) {
        try {
            image = ImageIO.read(new File(fileName));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (image == null)
            return;
        int imageWidth = image.getWidth(this);
        int imageHeight = image.getHeight(this);
        int width = getWidth();
        int height = getHeight();
        if(imageWidth>width){
            this.showWidth = width;
        }else{
            this.showWidth = imageWidth;
        }
        if(imageHeight>height){
            this.showHeight = height;
        }else{
            this.showHeight = imageHeight;
        }
        g.drawImage(image, 0, 0, showWidth, showHeight, null, null);
    }
}

本题考查的是窗体图形文件。
具体程序如下:

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

public class Java_3 extends JFrame {
    private JLabel label;
    private JFileChooser fileChooser;
    private ImagePanel panel;
    public Java_3() {
        setTitle("图片浏览器");
        setSize(500, 400);
        fileChooser = new JFileChooser();
        fileChooser.setCurrentDirectory(new File("."));//设置默认路径为当前目录
        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);
        JMenu menu = new JMenu("文件");
        menuBar.add(menu);
        JMenuItem openItem = new JMenuItem("打开图片");
        menu.add(openItem);
        panel = new ImagePanel();
        add(panel);
        //*********Found********
        openItem.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent event){
                int result = fileChooser.showOpenDialog(null);
                if(result==JFileChooser.APPROVE_OPTION){
                    String name = fileChooser.getSelectedFile().getPath();
                    //*********Found********
                    panel.setImage(name);
                    panel.repaint();
                }
            }
        });
        JMenuItem exitItem = new JMenuItem("退出图片");
        menu.add(exitItem);
        exitItem.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent event){
                System.exit(0);
            }
        });        
    }

    public static void main(String[] args) {
        //*********Found********
        Java_3 frame = new Java_3 ();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //*********Found********
        frame.setVisible(true);
    }
}


//*********Found********
class ImagePanel extends JPanel {
    private Image image;
    private int showWidth;
    private int showHeight;
    public void setImage(String fileName) {
        try {
            image = ImageIO.read(new File(fileName));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (image == null)
            return;
        int imageWidth = image.getWidth(this);
        int imageHeight = image.getHeight(this);
        int width = getWidth();
        int height = getHeight();
        if(imageWidth>width){
            this.showWidth = width;
        }else{
            this.showWidth = imageWidth;
        }
        if(imageHeight>height){
            this.showHeight = height;
        }else{
            this.showHeight = imageHeight;
        }
        g.drawImage(image, 0, 0, showWidth, showHeight, null, null);
    }
}
posted @   槑孒  阅读(350)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
点击右上角即可分享
微信分享提示