操作题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(new File("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){
        });        
    }
    

    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);
    }
}
  • 6
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值