实验十八  总复习

实验时间 2018-12-30

1、实验目的与要求

(1) 综合掌握java基本程序结构;

(2) 综合掌握java面向对象程序设计特点;

(3) 综合掌握java GUI 程序设计结构;

(4) 综合掌握java多线程编程模型;

(5) 综合编程练习。

2、实验内容和步骤

任务1:填写课程课后调查问卷,网址:https://www.wjx.cn/jq/33108969.aspx。

马凯军201771010116《面向对象与程序设计Java》

任务2:综合编程练习

练习1:设计一个用户信息采集程序,要求如下:

    (1) 用户信息输入界面如下图所示:

 马凯军201771010116《面向对象与程序设计Java》

(1)用户点击提交按钮时,用户输入信息显示控制台界面;

(2)用户点击重置按钮后,清空用户已输入信息;

(3)点击窗口关闭,程序退出。

程序如下:

按 Ctrl+C 复制代码
按 Ctrl+C 复制代码
按 Ctrl+C 复制代码
按 Ctrl+C 复制代码
按 Ctrl+C 复制代码
按 Ctrl+C 复制代码

程程序运行结果如下:

马凯军201771010116《面向对象与程序设计Java》

马凯军201771010116《面向对象与程序设计Java》

练习2:采用GUI界面设计以下程序:

编制一个程序,将身份证号.txt 中的信息读入到内存中;

按姓名字典序输出人员信息;

查询最大年龄的人员信息;

查询最小年龄人员信息;

输入你的年龄,查询身份证号.txt中年龄与你最近人的姓名、身份证号、年龄、性别和出生地;

查询人员中是否有你的同乡。

输入身份证信息,查询所提供身份证号的人员信息,要求输入一个身份证数字时,查询界面就显示满足查询条件的查询结果,且随着输入的数字的增多,查询匹配的范围逐渐缩小。

马凯军201771010116《面向对象与程序设计Java》
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;

public class Main{
    private static ArrayList<Student> studentlist;
    public static void main(String[] args) {
        studentlist = new ArrayList<>();
        Scanner scanner = new Scanner(System.in);
        File file = new File("D:\\text");
        try {
            FileInputStream fis = new FileInputStream(file);
            BufferedReader in = new BufferedReader(new InputStreamReader(fis));
            String temp = null;
            while ((temp = in.readLine()) != null) {
                
                Scanner linescanner = new Scanner(temp);
                
                linescanner.useDelimiter(" ");    
                String name = linescanner.next();
                String number = linescanner.next();
                String sex = linescanner.next();
                String age = linescanner.next();
                String province =linescanner.nextLine();
                Student student = new Student();
                student.setName(name);
                student.setnumber(number);
                student.setsex(sex);
                int a = Integer.parseInt(age);
                student.setage(a);
                student.setprovince(province);
                studentlist.add(student);

            }
        } catch (FileNotFoundException e) {
            System.out.println("学生信息文件找不到");
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("学生信息文件读取错误");
            e.printStackTrace();
        }
        boolean isTrue = true;
        while (isTrue) {
            System.out.println("选择你的操作,输入正确格式的选项");
            System.out.println("A.字典排序");
            System.out.println("B.输出年龄最大和年龄最小的人");
            System.out.println("C.寻找老乡");
            System.out.println("D.寻找年龄相近的人");
            System.out.println("F.退出");
            String m = scanner.next();
            switch (m) {
            case "A":
                Collections.sort(studentlist);              
                System.out.println(studentlist.toString());
                break;
            case "B":
                 int max=0,min=100;
                 int j,k1 = 0,k2=0;
                 for(int i=1;i<studentlist.size();i++)
                 {
                     j=studentlist.get(i).getage();
                 if(j>max)
                 {
                     max=j; 
                     k1=i;
                 }
                 if(j<min)
                 {
                   min=j; 
                   k2=i;
                 }
                 
                 }  
                 System.out.println("年龄最大:"+studentlist.get(k1));
                 System.out.println("年龄最小:"+studentlist.get(k2));
                break;
            case "C":
                 System.out.println("老家?");
                 String find = scanner.next();        
                 String place=find.substring(0,3);
                 for (int i = 0; i <studentlist.size(); i++) 
                 {
                     if(studentlist.get(i).getprovince().substring(1,4).equals(place)) 
                         System.out.println("老乡"+studentlist.get(i));
                 }             
                 break;
                 
            case "D":
                System.out.println("年龄:");
                int yourage = scanner.nextInt();
                int near=agenear(yourage);
                int value=yourage-studentlist.get(near).getage();
                System.out.println(""+studentlist.get(near));
                break;
            case "F":
                isTrue = false;
                System.out.println("退出程序!");
                break;
                default:
                System.out.println("输入有误");

            }
        }
    }
        public static int agenear(int age) {      
        int j=0,min=53,value=0,k=0;
         for (int i = 0; i < studentlist.size(); i++)
         {
             value=studentlist.get(i).getage()-age;
             if(value<0) value=-

相关文章:

  • 2021-06-04
  • 2022-02-23
  • 2021-08-26
  • 2021-09-01
  • 2021-09-27
  • 2021-06-07
  • 2021-11-09
  • 2021-10-10
猜你喜欢
  • 2022-01-20
  • 2021-11-14
  • 2022-02-09
  • 2021-10-20
  • 2021-11-22
  • 2021-11-18
  • 2021-07-25
相关资源
相似解决方案