zoukankan      html  css  js  c++  java
  • 杨其菊/常惠琢《面向对象程序设计(java)》第十一周学习总结

                                                                                                                                 《面向对象程序设计》第十一周学习总结

    第一部分:理论知识

    JAVA的集合框架
     JAVA的集合框架实现对各种数据结构的封装,以降低对数据管理与处理的难度。
     所谓框架就是一个类库的集合,框架中包含很多超类,编程者创建这些超类的子类可较方便的设计设计程序所需的类。例如:Swing类包
     集合(Collection或称为容器)是一种包含多个元素并提供对所包含元素操作方法的类,其包含的元素可以由同一类型的对象组成,也可以由不同类型的对象组成。
     集合框架:JAVA集合类库的统一架构

    集合类的作用
     集合类的作用:
    – Java的集合类提供了一些基本数据结构的支持。
    – 例如Vector、Hashtable、Stack等。集合类的使用:
    – Java的集合类包含在java.util包中。
    – import java.util.*;

    集合类的特点
     特点一:
    – 只容纳对象。
    注意:数组可以容纳基本数据类型数据和对象。
    – 如果集合类中想使用基本数据类型,又想利用集合类的灵活性,可以把基本数据类型数据封装成该数据类型的包装器对象,然后放入集合中处理。

    集合类的特点
     特点一:
    – 只容纳对象。
    注意:数组可以容纳基本数据类型数据和对象。
    – 如果集合类中想使用基本数据类型,又想利用集合类的灵活性,可以把基本数据类型数据封装成该数据类型的包装器对象,然后放入集合中处理。

    特点二:
    – 集合类容纳的对象都是Object类的实例,一旦把一个对象置入集合类中,它的类信息将丢失,这样设计的目的是为了集合类的通用性。
    – 因为Object类是所有类的祖先,所以可以在这些集合中存放任何类的对象而不受限制,但切记在使用集合成员之前必须对它重新造型。

    Vector类
     Vector类类似长度可变的数组。
     Vector中只能存放对象。
     Vector的元素通过下标进行访问。
     Vector类关键属性:
    – capacity表示集合最多能容纳的元素个数。
    – capacityIncrement表示每次增加多少容量。
    – size表示集合当前元素个数。
    Vector v = new Vector(100)

     Vector类的关键方法:
    – void addElement(Object obj)
    – void add(int index, Object element)
    – Object elementAt(int index)
    – void insertElementAt(Object obj, int index)

    Hashtable类
     Hashtable通过键来查找元素。
     Hashtable用散列码(hashcode)来确定键。所有对象都有一个散列码,可以通过Object类的hashCode()方法获得。

    集合框架中的基本接口
     Collection:集合层次中的根接口,JDK未提供这个接口的直接实现类。
     Set:不能包含重复的元素。对象可能不是按存放的次序存放,也就是说不能像数组一样按索引的方式进行访问,SortedSet是一个按照升序排列元素的Set。
     List:是一个有序的集合,可以包含重复的元素。提供了按索引访问的方式。
     Map:包含了key-value对。Map不能包含重复的key。
     SortedMap是一个按照升序排列key的Map。

    List(教材361页)
     List的明显特征是它的元素都有一个确定的顺序。
     实现它的类有ArrayList和LinkedList。
    – ArrayList中的元素在内存中是顺序存储的。
    – LinkedList中的元素在内存中是以链表方式存储的。

    ArrayList(见教材178页)和linkedList (见教材362页)
     ArrayList:可以将其看作是能够自动增长容量的数组。利用ArrayList的toArray()返回一个数组。
     Arrays.asList()返回一个列表。
     LinkedList是采用双向循环链表实现的。
     利用LinkedList实现栈(stack)、队列(queue)、双向队列(double-ended queue )。
     ArrayList底层采用数组完成,而LinkedList则是以一般的 双向链表(double-linked list)完成,其内每个对象除了数据 本身外,还有两个引用,分别指向前一个元素和后一个元 素。
     如果经常在 List 中进行插入和删除操作,应该使用LinkedList,否则,使用ArrayList将更加快速。

    Set
     Set中的元素必须唯一。
     添加到Set中的对象元素必须定义equals方法,以提供算法来判断欲添加进来的对象是否与已经存在的某对象相等,从而建立对象的唯一性。
     实 现 Set 接口的类有HashSet,TreeSet

    HashSet(教材365页)
    TreeSet(教材369页)
     TreeSet是一个有序集合,TreeSet中元素将按照升序排列,缺省是按照自然顺序进行排列,意味着TreeSet中元素要实现Comparable接口。
     可以在构造 TreeSet 对象时,传递实现了Comparator接口的比较器对象。
     HashSet是基于Hash算法实现的,其性能通常都优于TreeSet。通常使用HashSet,需要排序的功能时,使用TreeSet。

    Map定义
     映射(map)是一个存储关键字和值的关联或关键字/值对的对象。给定一个关键字,可以得到它的值。关键字和值都是对象。关键字必须是唯一的。但值是 可以被复制的。
     Map接口映射唯一关键字到值。关键字(key)是以 后用于检索值的对象。给定一个关键字和一个值,可 以存储这个值到一个Map对象中。当这个值被存储以 后,就可以使用它的关键字来检索它
     Map循环使用两个基本操作:get( )和put( )。使用put( )方法可以将一个指定了关键字和值的值加入映 射。为了得到值,可以通过将关键字作为参数来调用get( )方法。调用返回该值。


    实验十一   集合

    实验时间 2018-11-8

    1、实验目的与要求

    (1) 掌握Vetor、Stack、Hashtable三个类的用途及常用API;

    (2) 了解java集合框架体系组成;

    (3) 掌握ArrayList、LinkList两个类的用途及常用API。

    (4) 了解HashSet类、TreeSet类的用途及常用API。

    (5)了解HashMap、TreeMap两个类的用途及常用API;

    (6) 结对编程(Pair programming)练习,体验程序开发中的两人合作。

    2、实验内容和步骤

    实验1: 导入第9章示例程序,测试程序并进行代码注释。

    测试程序1:

    l 使用JDK命令运行编辑、运行以下三个示例程序,结合运行结果理解程序;

    l 掌握Vetor、Stack、Hashtable三个类的用途及常用API。 

    //示例程序1

    import java.util.Vector;

    class Cat {

    private int catNumber;

    Cat(int i) {

    catNumber = i;

    }

    void print() {

    System.out.println("Cat #" + catNumber);

    }

    }

    class Dog {

    private int dogNumber;

    Dog(int i) {

    dogNumber = i;

    }

    void print() {

    System.out.println("Dog #" + dogNumber);

    }

    }

    public class CatsAndDogs {

    public static void main(String[] args) {

    Vector cats = new Vector();

    for (int i = 0; i < 7; i++)

    cats.addElement(new Cat(i));

    cats.addElement(new Dog(7));

    for (int i = 0; i < cats.size(); i++)

    ((Cat) cats.elementAt(i)).print();

    }

    }

    //示例程序2

    import java.util.*;

    public class Stacks {

    static String[] months = { "1", "2", "3", "4" };

    public static void main(String[] args) {

    Stack stk = new Stack();

    for (int i = 0; i < months.length; i++)

    stk.push(months[i]);

    System.out.println(stk);

    System.out.println("element 2=" + stk.elementAt(2));

    while (!stk.empty())

    System.out.println(stk.pop());

    }

    }

    //示例程序3

    import java.util.*;

    class Counter {

    int i = 1;

    public String toString() {

    return Integer.toString(i);

    }

    }

    public class Statistics {

    public static void main(String[] args) {

    Hashtable ht = new Hashtable();

    for (int i = 0; i < 10000; i++) {

    Integer r = new Integer((int) (Math.random() * 20));

    if (ht.containsKey(r))

    ((Counter) ht.get(r)).i++;

    else

    ht.put(r, new Counter());

    }

    System.out.println(ht);

    }

    }

    示例程序1:

     1 package first;
     2 //示例程序1
     3 import java.util.Vector;
     4 
     5 class Cat {
     6     private int catNumber;
     7 
     8     Cat(int i) {
     9         catNumber = i;
    10     }
    11 
    12     void print() {
    13         System.out.println("Cat #" + catNumber);
    14     }
    15 }
    16 
    17 class Dog {
    18     private int dogNumber;
    19 
    20     Dog(int i) {
    21         dogNumber = i;
    22     }
    23 
    24     void print() {
    25         System.out.println("Dog #" + dogNumber);
    26     }
    27 }
    28 
    29 public class CatsAndDogs {
    30     public static void main(String[] args) {
    31         Vector cats = new Vector();
    32         for (int i = 0; i < 7; i++)
    33             cats.addElement(new Cat(i));
    34         cats.addElement(new Dog(7));
    35         for (int i = 0; i < cats.size(); i++)
    36             {
    37             if(cats.elementAt(i) instanceof Cat) {
    38             
    39                   ((Cat) cats.elementAt(i)).print();}
    40                         
    41             else
    42             {
    43                  ((Dog) cats.elementAt(i)).print();
    44             }
    45             }
    46     }
    47 }

     

    示例程序2:

    示例程序3

    测试程序2:

    l 使用JDK命令编辑运行ArrayListDemo和LinkedListDemo两个程序,结合程序运行结果理解程序;

    import java.util.*;

    public class ArrayListDemo {

    public static void main(String[] argv) {

    ArrayList al = new ArrayList();

    // Add lots of elements to the ArrayList...

    al.add(new Integer(11));

    al.add(new Integer(12));

    al.add(new Integer(13));

    al.add(new String("hello"));

    // First print them out using a for loop.

    System.out.println("Retrieving by index:");

    for (int i = 0; i < al.size(); i++) {

    System.out.println("Element " + i + " = " + al.get(i));

    }

    }

    }

    import java.util.*;

    public class LinkedListDemo {

        public static void main(String[] argv) {

            LinkedList l = new LinkedList();

            l.add(new Object());

            l.add("Hello");

            l.add("zhangsan");

            ListIterator li = l.listIterator(0);

            while (li.hasNext())

                System.out.println(li.next());

            if (l.indexOf("Hello") < 0)   

                System.err.println("Lookup does not work");

            else

                System.err.println("Lookup works");

       }

    }

    (1)

    (2)

    l 在Elipse环境下编辑运行调试教材360页程序9-1,结合程序运行结果理解程序;

    l 掌握ArrayList、LinkList两个类的用途及常用API。

     1 package A;
     2 import java.util.*;
     3 
     4 /**
     5  * This program demonstrates operations on linked lists.
     6  * @version 1.11 2012-01-26
     7  * @author Cay Horstmann
     8  */
     9 public class LinkedListTest
    10 {
    11    public static void main(String[] args)
    12    {
    13       List<String> a = new LinkedList<>();
    14       a.add("Amy");
    15       a.add("Carl");
    16       a.add("Erica");
    17 
    18       List<String> b = new LinkedList<>();
    19       b.add("Bob");
    20       b.add("Doug");
    21       b.add("Frances");
    22       b.add("Gloria");
    23 
    24       // merge the words from b into a
    25 
    26       ListIterator<String> aIter = a.listIterator();
    27       Iterator<String> bIter = b.iterator();
    28 
    29       while (bIter.hasNext())
    30       {
    31          if (aIter.hasNext()) aIter.next();
    32          aIter.add(bIter.next());
    33       }
    34 
    35       System.out.println(a);
    36 
    37       // remove every second word from b
    38 
    39       bIter = b.iterator();
    40       while (bIter.hasNext())
    41       {
    42          bIter.next(); // skip one element
    43          if (bIter.hasNext())
    44          {
    45             bIter.next(); // skip next element
    46             bIter.remove(); // remove that element
    47          }
    48       }
    49 
    50       System.out.println(b);
    51 
    52       // bulk operation: remove all words in b from a
    53 
    54       a.removeAll(b);
    55 
    56       System.out.println(a);
    57    }
    58 }

    测试程序3:

    l 运行SetDemo程序,结合运行结果理解程序;

    import java.util.*;

    public class SetDemo {

        public static void main(String[] argv) {

            HashSet h = new HashSet(); //也可以 Set h=new HashSet()

            h.add("One");

            h.add("Two");

            h.add("One"); // DUPLICATE

            h.add("Three");

            Iterator it = h.iterator();

            while (it.hasNext()) {

                 System.out.println(it.next());

            }

        }

    }

    (1)SetDemo:

     在Elipse环境下调试教材365页程序9-2,结合运行结果理解程序;了解HashSet类的用途及常用API。

    (2)

     1 package C;
     2 
     3 import java.util.*;
     4 
     5 /**
     6  * This program uses a set to print all unique words in System.in.
     7  * @version 1.12 2015-06-21
     8  * @author Cay Horstmann
     9  */
    10 public class SetTest
    11 {
    12    public static void main(String[] args)
    13    {
    14       Set<String> words = new HashSet<>(); // HashSet implements Set
    15       long totalTime = 0;
    16 
    17       try (Scanner in = new Scanner(System.in))
    18       {
    19          while (in.hasNext())
    20          {
    21             String word = in.next();
    22             long callTime = System.currentTimeMillis();
    23             words.add(word);
    24             callTime = System.currentTimeMillis() - callTime;
    25             totalTime += callTime;
    26          }
    27       }
    28 
    29       Iterator<String> iter = words.iterator();
    30       for (int i = 1; i <= 20 && iter.hasNext(); i++)
    31          System.out.println(iter.next());
    32       System.out.println(". . .");
    33       System.out.println(words.size() + " distinct words. " + totalTime + " milliseconds.");
    34    }
    35 }

     l 在Elipse环境下调试教材367页-368程序9-3、9-4,结合程序运行结果理解程序;了解TreeSet类的用途及常用API。

    (3)TreeSet

     1 package B;
     2 
     3 import java.util.*;
     4 
     5 /**
     6  * This program sorts a set of item by comparing their descriptions.
     7  * @version 1.12 2015-06-21
     8  * @author Cay Horstmann
     9  */
    10 public class TreeSetTest
    11 {
    12    public static void main(String[] args)
    13    {
    14       SortedSet<Item> parts = new TreeSet<>();
    15       parts.add(new Item("Toaster", 1234));
    16       parts.add(new Item("Widget", 4562));
    17       parts.add(new Item("Modem", 9912));
    18       System.out.println(parts);
    19 
    20       NavigableSet<Item> sortByDescription = new TreeSet<>(
    21             Comparator.comparing(Item::getDescription));
    22 
    23       sortByDescription.addAll(parts);
    24       System.out.println(sortByDescription);
    25    }
    26 }
     1 package B;
     2 
     3 import java.util.*;
     4 
     5 /**
     6  * An item with a description and a part number.
     7  */
     8 public class Item implements Comparable<Item>
     9 {
    10    private String description;
    11    private int partNumber;
    12 
    13    /**
    14     * Constructs an item.
    15     * 
    16     * @param aDescription
    17     *           the item's description
    18     * @param aPartNumber
    19     *           the item's part number
    20     */
    21    public Item(String aDescription, int aPartNumber)
    22    {
    23       description = aDescription;
    24       partNumber = aPartNumber;
    25    }
    26 
    27    /**
    28     * Gets the description of this item.
    29     * 
    30     * @return the description
    31     */
    32    public String getDescription()
    33    {
    34       return description;
    35    }
    36 
    37    public String toString()
    38    {
    39       return "[description=" + description + ", partNumber=" + partNumber + "]";
    40    }
    41 
    42    public boolean equals(Object otherObject)
    43    {
    44       if (this == otherObject) return true;
    45       if (otherObject == null) return false;
    46       if (getClass() != otherObject.getClass()) return false;
    47       Item other = (Item) otherObject;
    48       return Objects.equals(description, other.description) && partNumber == other.partNumber;
    49    }
    50 
    51    public int hashCode()
    52    {
    53       return Objects.hash(description, partNumber);
    54    }
    55 
    56    public int compareTo(Item other)
    57    {
    58       int diff = Integer.compare(partNumber, other.partNumber);
    59       return diff != 0 ? diff : description.compareTo(other.description);
    60    }
    61 }

    测试程序4:

    l 使用JDK命令运行HashMapDemo程序,结合程序运行结果理解程序;

    import java.util.*;

    public class HashMapDemo {

       public static void main(String[] argv) {

          HashMap h = new HashMap();

          // The hash maps from company name to address.

          h.put("Adobe", "Mountain View, CA");

          h.put("IBM", "White Plains, NY");

          h.put("Sun", "Mountain View, CA");

          String queryString = "Adobe";

          String resultString = (String)h.get(queryString);

          System.out.println("They are located in: " +  resultString);

      }

    }

    l 在Elipse环境下调试教材373页程序9-6,结合程序运行结果理解程序;

    l 了解HashMap、TreeMap两个类的用途及常用API。

     1 package second;
     2 
     3 import java.util.*;
     4 
     5 /**
     6  * This program demonstrates the use of a map with key type String and value type Employee.
     7  * @version 1.12 2015-06-21
     8  * @author Cay Horstmann
     9  */
    10 public class MapTest
    11 {
    12    public static void main(String[] args)
    13    {
    14       Map<String, Employee> staff = new HashMap<>();
    15       staff.put("144-25-5464", new Employee("Amy Lee"));
    16       staff.put("567-24-2546", new Employee("Harry Hacker"));
    17       staff.put("157-62-7935", new Employee("Gary Cooper"));
    18       staff.put("456-62-5527", new Employee("Francesca Cruz"));
    19 
    20       // print all entries
    21 
    22       System.out.println(staff);
    23 
    24       // remove an entry
    25 
    26       staff.remove("567-24-2546");
    27 
    28       // replace an entry
    29 
    30       staff.put("456-62-5527", new Employee("Francesca Miller"));
    31 
    32       // look up a value
    33 
    34       System.out.println(staff.get("157-62-7935"));
    35 
    36       // iterate through all entries
    37 
    38       staff.forEach((k, v) -> 
    39          System.out.println("key=" + k + ", value=" + v));
    40    }
    41 }
     1 package second;
     2 
     3 /**
     4  * A minimalist employee class for testing purposes.
     5  */
     6 public class Employee
     7 {
     8    private String name;
     9    private double salary;
    10 
    11    /**
    12     * Constructs an employee with $0 salary.
    13     * @param n the employee name
    14     */
    15    public Employee(String name)
    16    {
    17       this.name = name;
    18       salary = 0;
    19    }
    20 
    21    public String toString()
    22    {
    23       return "[name=" + name + ", salary=" + salary + "]";
    24    }
    25 }

    实验2:结对编程练习:

    l 关于结对编程:以下图片是一个结对编程场景:两位学习伙伴坐在一起,面对着同一台显示器,使用着同一键盘,同一个鼠标,他们一起思考问题,一起分析问题,一起编写程序。

    l 关于结对编程的阐述可参见以下链接:

    http://www.cnblogs.com/xinz/archive/2011/08/07/2130332.html

    http://en.wikipedia.org/wiki/Pair_programming

    l 对于结对编程中代码设计规范的要求参考:

    http://www.cnblogs.com/xinz/archive/2011/11/20/2255971.html

    以下实验,就让我们来体验一下结对编程的魅力。

    l 确定本次实验结对编程合作伙伴;

    l 各自运行合作伙伴实验九编程练习1,结合使用体验对所运行程序提出完善建议;

    l 各自运行合作伙伴实验十编程练习2,结合使用体验对所运行程序提出完善建议;

    l 采用结对编程方式,与学习伙伴合作完成实验九编程练习1;

    l 采用结对编程方式,与学习伙伴合作完成实验十编程练习2。

     (1)实验九编程练习1

    我的(杨其菊)

      1 import java.io.BufferedReader;
      2         import java.io.File;
      3         import java.io.FileInputStream;
      4         import java.io.FileNotFoundException;
      5         import java.io.IOException;
      6         import java.io.InputStreamReader;
      7         import java.util.ArrayList;
      8         import java.util.Arrays;
      9         import java.util.Collections;
     10         import java.util.Scanner;
     11 
     12 
     13 public class Search{
     14 
     15       private static ArrayList<Person> Personlist1;
     16        public static void main(String[] args) {
     17          
     18           Personlist1 = new ArrayList<>();
     19          
     20           Scanner scanner = new Scanner(System.in);
     21           File file = new File("E:\面向对象程序设计Java\实验\实验六\身份证号.txt");
     22    
     23                 try {
     24                      FileInputStream F = new FileInputStream(file);
     25                      BufferedReader in = new BufferedReader(new InputStreamReader(F));
     26                      String temp = null;
     27                      while ((temp = in.readLine()) != null) {
     28                         
     29                         Scanner linescanner = new Scanner(temp);
     30                         
     31                         linescanner.useDelimiter(" ");    
     32                         String name = linescanner.next();
     33                         String id = linescanner.next();
     34                         String sex = linescanner.next();
     35                         String age = linescanner.next();
     36                         String place =linescanner.nextLine();
     37                         Person Person = new Person();
     38                         Person.setname(name);
     39                         Person.setid(id);
     40                         Person.setsex(sex);
     41                         int a = Integer.parseInt(age);
     42                         Person.setage(a);
     43                         Person.setbirthplace(place);
     44                         Personlist1.add(Person);
     45 
     46                     }
     47                 } catch (FileNotFoundException e) {
     48                     System.out.println("查找不到信息");
     49                     e.printStackTrace();
     50                 } catch (IOException e) {
     51                     System.out.println("信息读取有误");
     52                     e.printStackTrace();
     53                 }
     54                 boolean isTrue = true;
     55                 while (isTrue) {
     56                     System.out.println("******************************************");
     57                     System.out.println("1:按姓名字典顺序输出信息;");
     58                     System.out.println("2:查询最大年龄与最小年龄人员信息;");
     59                     System.out.println("3:按省份找你的同乡;");
     60                     System.out.println("4:输入你的年龄,查询年龄与你最近人的信息;");
     61                     System.out.println("5:退出");
     62                     System.out.println("******************************************");
     63                     int type = scanner.nextInt();
     64                     switch (type) {
     65                     case 1:
     66                         Collections.sort(Personlist1);
     67                         System.out.println(Personlist1.toString());
     68                         break;
     69                     case 2:
     70                         
     71                         int max=0,min=100;int j,k1 = 0,k2=0;
     72                         for(int i=1;i<Personlist1.size();i++)
     73                         {
     74                             j=Personlist1.get(i).getage();
     75                            if(j>max)
     76                            {
     77                                max=j; 
     78                                k1=i;
     79                            }
     80                            if(j<min)
     81                            {
     82                                min=j; 
     83                                k2=i;
     84                            }
     85 
     86                         }  
     87                         System.out.println("年龄最大:"+Personlist1.get(k1));
     88                         System.out.println("年龄最小:"+Personlist1.get(k2));
     89                         break;
     90                     case 3:
     91                         System.out.println("place?");
     92                         String find = scanner.next();        
     93                         String place=find.substring(0,3);
     94                         String place2=find.substring(0,3);
     95                         for (int i = 0; i <Personlist1.size(); i++) 
     96                         {
     97                             if(Personlist1.get(i).getbirthplace().substring(1,4).equals(place)) 
     98                             {
     99                                 System.out.println("你的同乡:"+Personlist1.get(i));
    100                             }
    101                         } 
    102 
    103                         break;
    104                     case 4:
    105                         System.out.println("年龄:");
    106                         int yourage = scanner.nextInt();
    107                         int close=ageclose(yourage);
    108                         int d_value=yourage-Personlist1.get(close).getage();
    109                         System.out.println(""+Personlist1.get(close));
    110                   
    111                         break;
    112                     case 5:
    113                    isTrue = false;
    114                    System.out.println("再见!");
    115                         break;
    116                     default:
    117                         System.out.println("输入有误");
    118                     }
    119                 }
    120             }
    121             public static int ageclose(int age) {
    122                    int m=0;
    123                 int    max=53;
    124                 int d_value=0;
    125                 int k=0;
    126                 for (int i = 0; i < Personlist1.size(); i++)
    127                 {
    128                     d_value=Personlist1.get(i).getage()-age;
    129                     if(d_value<0) d_value=-d_value; 
    130                     if (d_value<max) 
    131                     {
    132                        max=d_value;
    133                        k=i;
    134                     }
    135 
    136                  }    return k;
    137                 
    138              }
    139 
    140    
    141 
    142  }
    143 
    144 
    145 
    146 
    147 
    148 
    149 
    150 
    151 //jiekouwenjiaan
    152 
    153 
    154 public class Person implements Comparable<Person> {
    155             private String name;
    156             private String id;
    157             private int age;
    158             private String sex;
    159             private String birthplace;
    160 
    161     public String getname() {
    162         return name;
    163         }
    164     public void setname(String name) {
    165         this.name = name;
    166     }
    167     public String getid() {
    168         return id;
    169     }
    170     public void setid(String id) {
    171         this.id= id;
    172     }
    173     public int getage() {
    174     
    175         return age;
    176     }
    177     public void setage(int age) {
    178         // int a = Integer.parseInt(age);
    179         this.age= age;
    180     }
    181     public String getsex() {
    182         return sex;
    183     }
    184     public void setsex(String sex) {
    185         this.sex= sex;
    186     }
    187     public String getbirthplace() {
    188         return birthplace;
    189     }
    190     public void setbirthplace(String birthplace) {
    191         this.birthplace= birthplace;
    192 }
    193 
    194     public int compareTo(Person o) {
    195         return this.name.compareTo(o.getname());
    196 
    197 }
    198 
    199     public String toString() {
    200         return  name+"	"+sex+"	"+age+"	"+id+"	";
    201 
    202 }
    203 
    204 
    205 
    206 }
    Search

    伙伴(常惠琢):

      1  import java.io.BufferedReader;
      2         import java.io.File;
      3         import java.io.FileInputStream;
      4         import java.io.FileNotFoundException;
      5         import java.io.IOException;
      6         import java.io.InputStreamReader;
      7         import java.util.ArrayList;
      8         import java.util.Arrays;
      9         import java.util.Collections;
     10         import java.util.Scanner;
     11 
     12 public class Test {
     13     private static ArrayList<Student> studentlist;
     14     public static void main(String[] args) {
     15 
     16                 studentlist = new ArrayList<>();
     17                 Scanner scanner = new Scanner(System.in);
     18                 File file = new File("F:\java\身份证号.txt");
     19                 try {
     20                     FileInputStream fis = new FileInputStream(file);
     21                     BufferedReader in = new BufferedReader(new InputStreamReader(fis));
     22                     String temp = null;
     23                     while ((temp = in.readLine()) != null) {
     24                         
     25                         Scanner linescanner = new Scanner(temp);
     26                         
     27                         linescanner.useDelimiter(" ");    
     28                         String name = linescanner.next();
     29                         String number = linescanner.next();
     30                         String sex = linescanner.next();
     31                         String age = linescanner.next();
     32                         String province =linescanner.nextLine();
     33                         Student student = new Student();
     34                         student.setName(name);
     35                         student.setnumber(number);
     36                         student.setsex(sex);
     37                         int a = Integer.parseInt(age);
     38                         student.setage(a);
     39                         student.setprovince(province);
     40                         studentlist.add(student);
     41 
     42                     }
     43                 } catch (FileNotFoundException e) {
     44                     System.out.println("找不到学生的信息文件");
     45                     e.printStackTrace();
     46                 } catch (IOException e) {
     47                     System.out.println("学生信息文件读取错误");
     48                     e.printStackTrace();
     49                 }
     50                 boolean isTrue = true;
     51                 while (isTrue) {
     52                     System.out.println("选择你的操作, ");
     53                     System.out.println("1.字典排序  ");
     54                     System.out.println("2.输出年龄最大和年龄最小的人  ");
     55                     System.out.println("3.寻找同乡  ");
     56                     System.out.println("4.寻找年龄相近的人  ");
     57                     System.out.println("5.退出 ");
     58                     String m = scanner.next();
     59                     switch (m) {
     60                     case "1":
     61                         Collections.sort(studentlist);              
     62                         System.out.println(studentlist.toString());
     63                         break;
     64                     case "2":
     65                          int max=0,min=100;
     66                          int j,k1 = 0,k2=0;
     67                          for(int i=1;i<studentlist.size();i++)
     68                          {
     69                              j=studentlist.get(i).getage();
     70                          if(j>max)
     71                          {
     72                              max=j; 
     73                              k1=i;
     74                          }
     75                          if(j<min)
     76                          {
     77                            min=j; 
     78                            k2=i;
     79                          }
     80                          
     81                          }  
     82                          System.out.println("年龄最大:"+studentlist.get(k1));
     83                          System.out.println("年龄最小:"+studentlist.get(k2));
     84                         break;
     85                     case "3":
     86                          System.out.println("地址?");
     87                          String find = scanner.next();        
     88                          String place=find.substring(0,3);
     89                          for (int i = 0; i <studentlist.size(); i++) 
     90                          {
     91                              if(studentlist.get(i).getprovince().substring(1,4).equals(place)) 
     92                                  System.out.println("同乡"+studentlist.get(i));
     93                          }             
     94                          break;
     95                          
     96                     case "4":
     97                         System.out.println("年龄:");
     98                         int yourage = scanner.nextInt();
     99                         int near=agenear(yourage);
    100                         int value=yourage-studentlist.get(near).getage();
    101                         System.out.println(""+studentlist.get(near));
    102                         break;
    103                     case "5 ":
    104                         isTrue = false;
    105                         System.out.println("退出程序!");
    106                         break;
    107                         default:
    108                         System.out.println("输入有误");
    109 
    110                     }
    111                 }
    112             }
    113                 public static int agenear(int age) {      
    114                 int j=0,min=53,value=0,ok=0;
    115                  for (int i = 0; i < studentlist.size(); i++)
    116                  {
    117                      value=studentlist.get(i).getage()-age;
    118                      if(value<0) value=-value; 
    119                      if (value<min) 
    120                      {
    121                         min=value;
    122                          ok=i;
    123                      } 
    124                   }    
    125                  return ok;         
    126               }
    127         }
    Main
      1  import java.io.BufferedReader;
      2         import java.io.File;
      3         import java.io.FileInputStream;
      4         import java.io.FileNotFoundException;
      5         import java.io.IOException;
      6         import java.io.InputStreamReader;
      7         import java.util.ArrayList;
      8         import java.util.Arrays;
      9         import java.util.Collections;
     10         import java.util.Scanner;
     11 
     12 public class Test {
     13     private static ArrayList<Student> studentlist;
     14     public static void main(String[] args) {
     15 
     16                 studentlist = new ArrayList<>();
     17                 Scanner scanner = new Scanner(System.in);
     18                 File file = new File("F:\java\身份证号.txt");
     19                 try {
     20                     FileInputStream fis = new FileInputStream(file);
     21                     BufferedReader in = new BufferedReader(new InputStreamReader(fis));
     22                     String temp = null;
     23                     while ((temp = in.readLine()) != null) {
     24                         
     25                         Scanner linescanner = new Scanner(temp);
     26                         
     27                         linescanner.useDelimiter(" ");    
     28                         String name = linescanner.next();
     29                         String number = linescanner.next();
     30                         String sex = linescanner.next();
     31                         String age = linescanner.next();
     32                         String province =linescanner.nextLine();
     33                         Student student = new Student();
     34                         student.setName(name);
     35                         student.setnumber(number);
     36                         student.setsex(sex);
     37                         int a = Integer.parseInt(age);
     38                         student.setage(a);
     39                         student.setprovince(province);
     40                         studentlist.add(student);
     41 
     42                     }
     43                 } catch (FileNotFoundException e) {
     44                     System.out.println("找不到学生的信息文件");
     45                     e.printStackTrace();
     46                 } catch (IOException e) {
     47                     System.out.println("学生信息文件读取错误");
     48                     e.printStackTrace();
     49                 }
     50                 boolean isTrue = true;
     51                 while (isTrue) {
     52                     System.out.println("选择你的操作, ");
     53                     System.out.println("1.字典排序  ");
     54                     System.out.println("2.输出年龄最大和年龄最小的人  ");
     55                     System.out.println("3.寻找同乡  ");
     56                     System.out.println("4.寻找年龄相近的人  ");
     57                     System.out.println("5.退出 ");
     58                     String m = scanner.next();
     59                     switch (m) {
     60                     case "1":
     61                         Collections.sort(studentlist);              
     62                         System.out.println(studentlist.toString());
     63                         break;
     64                     case "2":
     65                          int max=0,min=100;
     66                          int j,k1 = 0,k2=0;
     67                          for(int i=1;i<studentlist.size();i++)
     68                          {
     69                              j=studentlist.get(i).getage();
     70                          if(j>max)
     71                          {
     72                              max=j; 
     73                              k1=i;
     74                          }
     75                          if(j<min)
     76                          {
     77                            min=j; 
     78                            k2=i;
     79                          }
     80                          
     81                          }  
     82                          System.out.println("年龄最大:"+studentlist.get(k1));
     83                          System.out.println("年龄最小:"+studentlist.get(k2));
     84                         break;
     85                     case "3":
     86                          System.out.println("地址?");
     87                          String find = scanner.next();        
     88                          String place=find.substring(0,3);
     89                          for (int i = 0; i <studentlist.size(); i++) 
     90                          {
     91                              if(studentlist.get(i).getprovince().substring(1,4).equals(place)) 
     92                                  System.out.println("同乡"+studentlist.get(i));
     93                          }             
     94                          break;
     95                          
     96                     case "4":
     97                         System.out.println("年龄:");
     98                         int yourage = scanner.nextInt();
     99                         int near=agenear(yourage);
    100                         int value=yourage-studentlist.get(near).getage();
    101                         System.out.println(""+studentlist.get(near));
    102                         break;
    103                     case "5 ":
    104                         isTrue = false;
    105                         System.out.println("退出程序!");
    106                         break;
    107                         default:
    108                         System.out.println("输入有误");
    109 
    110                     }
    111                 }
    112             }
    113                 public static int agenear(int age) {      
    114                 int j=0,min=53,value=0,ok=0;
    115                  for (int i = 0; i < studentlist.size(); i++)
    116                  {
    117                      value=studentlist.get(i).getage()-age;
    118                      if(value<0) value=-value; 
    119                      if (value<min) 
    120                      {
    121                         min=value;
    122                          ok=i;
    123                      } 
    124                   }    
    125                  return ok;         
    126               }
    127         }
    Test

    完善:

    1.文件输出字体格式不一致,在整体美观上尚可修改。
    2.编程过程存在变量使用混乱不清的问题。

    (2)实验十编程练习2

    我的:

     1 package a;
     2 
     3 import java.io.FileNotFoundException;
     4 import java.io.PrintWriter;
     5 import java.util.Scanner;
     6 
     7 import org.w3c.dom.css.Counter;
     8 
     9 
    10 public class Main{
    11     public static void main(String[] args) {
    12 
    13         Scanner in = new Scanner(System.in);
    14         counter counter=new  counter();
    15         PrintWriter output = null;
    16         try {
    17             output = new PrintWriter("result.txt");
    18         } catch (Exception e) {
    19             //e.printStackTrace();
    20         }
    21         int sum = 0;
    22 
    23         for (int i = 1; i < 11; i++) {
    24             int a = (int) Math.round(Math.random() * 100);
    25             int b = (int) Math.round(Math.random() * 100);
    26             int type = (int) Math.round(Math.random() * 4);
    27 
    28             
    29            switch(type)
    30            {
    31            case 1:
    32                System.out.println(i+": "+a+"/"+b+"=");
    33                while(b==0){  
    34                    b = (int) Math.round(Math.random() * 100); 
    35                    }
    36                double c = in.nextDouble();
    37                output.println(a+"/"+b+"="+c);
    38                if (c == counter.Chu(a, b)) 
    39                {
    40                    sum += 10;
    41                    System.out.println("恭喜答案正确!");
    42                }
    43                else {
    44                    System.out.println("答案错误!");
    45                } break;
    46             
    47            case 2:
    48                System.out.println(i+": "+a+"*"+b+"=");
    49                int c1 = in.nextInt();
    50                output.println(a+"*"+b+"="+c1);
    51                if (c1 == counter.Cheng(a, b)) {
    52                    sum += 10;
    53                    System.out.println("恭喜答案正确!");
    54                }
    55                else {
    56                    System.out.println("答案错误!");
    57                }break;
    58            case 3:
    59                System.out.println(i+": "+a+"+"+b+"=");
    60                int c2 = in.nextInt();
    61                output.println(a+"+"+b+"="+c2);
    62                if (c2 ==  counter.Jia(a, b)) {
    63                    sum += 10;
    64                    System.out.println("恭喜答案正确!");
    65                }
    66                else {
    67                    System.out.println("答案错误!");
    68                }break ;
    69            case 4:
    70                System.out.println(i+": "+a+"-"+b+"=");
    71                int c3 = in.nextInt();
    72                output.println(a+"-"+b+"="+c3);
    73                if (c3 ==  counter.Jian(a, b)) {
    74                    sum += 10;
    75                    System.out.println("恭喜答案正确!");
    76                }
    77                else {
    78                    System.out.println("答案错误!");
    79                }break ;
    80 
    81              } 
    82     
    83           }
    84         System.out.println("成绩"+sum);
    85         output.println("成绩:"+sum);
    86         output.close();
    87          
    88     }
    89 }    
    90 
    91 Main
    Main
    package a;
    
    
    public class counter {
           private int a;
           private int b;
    
                   public int  Jia(int a,int b)
                   {
                       return a+b;
                   }
                   public int   Jian(int a,int b)
                   {
                       if((a-b)<0)
                           return 0;
                       else
                       return a-b;
                   }
                   public int   Cheng(int a,int b)
                   {
                       return a*b;
                   }
                   public int   Chu(int a,int b)
                   {
                       if(b!=0)
                       return a/b;    
                       else
                   return 0;
                   }
    
                   
           }
    
    counter
    counter

    伙伴:

     1 java.io.FileNotFoundException;
     2 import java.io.IOException;
     3 import java.io.PrintWriter;
     4 import java.util.Scanner;
     5 public class Fine {
     6 
     7     public static void main(String[] args) {
     8                 Scanner in = new Scanner(System.in);
     9                 Min min=new Min();
    10                 PrintWriter out = null;
    11                 try {
    12                     out = new PrintWriter("test.txt");
    13                     int sum = 0;
    14                     for (int i = 1; i <=10; i++) {
    15                         int a = (int) Math.round(Math.random() * 100);
    16                         int b = (int) Math.round(Math.random() * 100);
    17                         int menu = (int) Math.round(Math.random() * 3);
    18                         switch (menu) {
    19                         case 0:
    20                             System.out.println(i+":"+a + "+" + b + "=");
    21                             int c1 = in.nextInt();
    22                             out.println(a + "+" + b + "=" + c1);
    23                             if (c1 == (a + b)) {
    24                                 sum += 10;
    25                                 System.out.println("恭喜答案正确");
    26                             } else {
    27                                 System.out.println("抱歉,答案错误");
    28                             }
    29                             break;
    30                         case 1:
    31                             while (a < b) {
    32                                 b = (int) Math.round(Math.random() * 100);
    33                             }
    34                             System.out.println(i+":"+a + "-" + b + "=");
    35                             int c2 = in.nextInt();
    36                             out.println(a + "-" + b + "=" + c2);
    37                             if (c2 == (a - b)) {
    38                                 sum += 10;
    39                                 System.out.println("恭喜答案正确");
    40                             } else {
    41                                 System.out.println("抱歉,答案错误");
    42                             }
    43 
    44                             break;
    45                         case 2:
    46                             System.out.println(i+":"+a + "*" + b + "=");
    47                             int c3 = in.nextInt();
    48                             out.println(a + "*" + b + "=" + c3);
    49                             if (c3 == a * b) {
    50                                 sum += 10;
    51                                 System.out.println("恭喜答案正确");
    52                             } else {
    53                                 System.out.println("抱歉,答案错误");
    54                             }
    55 
    56                             break;
    57                         case 3:
    58                              while(b == 0){
    59                                     b = (int) Math.round(Math.random() * 100);
    60                                 }
    61                                 while(a % b != 0){
    62                                     a = (int) Math.round(Math.random() * 100);
    63                                     
    64                                 }
    65                             System.out.println(i+":"+a + "/" + b + "=");
    66                             int c4 = in.nextInt();
    67                             if (c4 == a / b) {
    68                                 sum += 10;
    69                                 System.out.println("恭喜,答案正确");
    70                             } else {
    71                                 System.out.println("抱歉,答案错误");
    72                             }
    73 
    74                             break;
    75                         }
    76                     }
    77                     System.out.println("你的得分为" + sum);
    78                     out.println("你的得分为" + sum);
    79                     out.close();
    80                 } catch (FileNotFoundException e) {
    81                     e.printStackTrace();
    82                 }
    83             }
    84         }
    Main
     1  public class Min<T> {
     2     private T a;
     3     private T b;
     4     public Min() {
     5         a=null;
     6         b=null;
     7     }
     8     public Min(T a,T b) {
     9         this.a=a;
    10         this.b=b;
    11     }
    12     public int count1(int a,int b) {
    13         return a+b;
    14     }
    15     public int count2(int a,int b) {
    16         return a-b;
    17     }
    18     public int count3(int a,int b) {
    19         return a*b;
    20     }
    21     public int count4(int a,int b) {
    22         return a/b;
    23     }
    24 }
    View Code

    完善:

          1.没有全面考虑小学生的实际状况:减法算法结果可能出现负数的情况,以及除法的运算结果直接取整输出,都不符合小学生的学习范围;                
         2.编程过程中,关于变量的使用混乱问题;                      
          3.结果没有输出到文件中;

    (3)合作实验九编程练习1;

      1 package Second;
      2 import java.io.BufferedReader;
      3 import java.io.File;
      4 import java.io.FileInputStream;
      5 import java.io.FileNotFoundException;
      6 import java.io.IOException;
      7 import java.io.InputStreamReader;
      8 import java.util.ArrayList;
      9 import java.util.Arrays;
     10 import java.util.Collections;
     11 import java.util.Scanner;
     12 
     13 
     14 public class Search{
     15 
     16 private static ArrayList<Person> Personlist1;
     17 public static void main(String[] args) {
     18  
     19   Personlist1 = new ArrayList<>();
     20  
     21   Scanner scanner = new Scanner(System.in);
     22   File file = new File("E:\面向对象程序设计Java\实验\实验六\身份证号.txt");
     23 
     24         try {
     25              FileInputStream F = new FileInputStream(file);
     26              BufferedReader in = new BufferedReader(new InputStreamReader(F));
     27              String temp = null;
     28              while ((temp = in.readLine()) != null) {
     29                 
     30                 Scanner linescanner = new Scanner(temp);
     31                 
     32                 linescanner.useDelimiter(" ");    
     33                 String name = linescanner.next();
     34                 String id = linescanner.next();
     35                 String sex = linescanner.next();
     36                 String age = linescanner.next();
     37                 String place =linescanner.nextLine();
     38                 Person Person = new Person();
     39                 Person.setname(name);
     40                 Person.setid(id);
     41                 Person.setsex(sex);
     42                 int a = Integer.parseInt(age);
     43                 Person.setage(a);
     44                 Person.setbirthplace(place);
     45                 Personlist1.add(Person);
     46 
     47             }
     48         } catch (FileNotFoundException e) {
     49             System.out.println("查找不到信息");
     50             e.printStackTrace();
     51         } catch (IOException e) {
     52             System.out.println("信息读取有误");
     53             e.printStackTrace();
     54         }
     55         boolean isTrue = true;
     56         while (isTrue) {
     57             System.out.println("******************************************");
     58             System.out.println("1:按姓名字典顺序输出信息;");
     59             System.out.println("2:查询最大年龄与最小年龄人员信息;");
     60             System.out.println("3:按省份找你的同乡;");
     61             System.out.println("4:输入你的年龄,查询年龄与你最近人的信息;");
     62             System.out.println("5:退出");
     63             System.out.println("******************************************");
     64             int type = scanner.nextInt();
     65             switch (type) {
     66             case 1:
     67                 Collections.sort(Personlist1);
     68                 System.out.println(Personlist1.toString());
     69                 break;
     70             case 2:
     71                 
     72                 int max=0,min=100;int j,k1 = 0,k2=0;
     73                 for(int i=1;i<Personlist1.size();i++)
     74                 {
     75                     j=Personlist1.get(i).getage();
     76                    if(j>max)
     77                    {
     78                        max=j; 
     79                        k1=i;
     80                    }
     81                    if(j<min)
     82                    {
     83                        min=j; 
     84                        k2=i;
     85                    }
     86 
     87                 }  
     88                 System.out.println("年龄最大:"+Personlist1.get(k1));
     89                 System.out.println("年龄最小:"+Personlist1.get(k2));
     90                 break;
     91             case 3:
     92                 System.out.println("place?");
     93                 String find = scanner.next();        
     94                 String place=find.substring(0,3);
     95                 String place2=find.substring(0,3);
     96                 for (int i = 0; i <Personlist1.size(); i++) 
     97                 {
     98                     if(Personlist1.get(i).getbirthplace().substring(1,4).equals(place)) 
     99                     {
    100                         System.out.println("你的同乡:"+Personlist1.get(i));
    101                     }
    102                 } 
    103 
    104                 break;
    105             case 4:
    106                 System.out.println("年龄:");
    107                 int yourage = scanner.nextInt();
    108                 int close=ageclose(yourage);
    109                 int d_value=yourage-Personlist1.get(close).getage();
    110                 System.out.println(""+Personlist1.get(close));
    111           
    112                 break;
    113             case 5:
    114            isTrue = false;
    115            System.out.println("再见!");
    116                 break;
    117             default:
    118                 System.out.println("输入有误");
    119             }
    120         }
    121     }
    122     public static int ageclose(int age) {
    123            int m=0;
    124         int    max=53;
    125         int d_value=0;
    126         int k=0;
    127         for (int i = 0; i < Personlist1.size(); i++)
    128         {
    129             d_value=Personlist1.get(i).getage()-age;
    130             if(d_value<0) d_value=-d_value; 
    131             if (d_value<max) 
    132             {
    133                max=d_value;
    134                k=i;
    135             }
    136 
    137          }    return k;
    138         
    139      }
    140 
    141 
    142 
    143 }
    Main
     1 package Second;
     2 
     3 //jiekouwenjiaan
     4 
     5 
     6 public class Person implements Comparable<Person> {
     7   private String name;
     8   private String id;
     9   private int age;
    10   private String sex;
    11   private String birthplace;
    12 
    13 public String getname() {
    14 return name;
    15 }
    16 public void setname(String name) {
    17 this.name = name;
    18 }
    19 public String getid() {
    20 return id;
    21 }
    22 public void setid(String id) {
    23 this.id= id;
    24 }
    25 public int getage() {
    26 
    27 return age;
    28 }
    29 public void setage(int age) {
    30 //int a = Integer.parseInt(age);
    31 this.age= age;
    32 }
    33 public String getsex() {
    34 return sex;
    35 }
    36 public void setsex(String sex) {
    37 this.sex= sex;
    38 }
    39 public String getbirthplace() {
    40 return birthplace;
    41 }
    42 public void setbirthplace(String birthplace) {
    43 this.birthplace= birthplace;
    44 }
    45 
    46 public int compareTo(Person o) {
    47 return this.name.compareTo(o.getname());
    48 
    49 }
    50 
    51 public String toString() {
    52 return  name+"	"+sex+"	"+age+"	"+id+"	";
    53 
    54 }
    55 
    56 
    57 
    58 }
    Person

    (4)合作实验十编程练习2

     1 package A;
     2 
     3 import java.io.FileNotFoundException;
     4 import java.io.IOException;
     5 import java.io.PrintWriter;
     6 import java.util.Scanner;
     7 public class Main {
     8 
     9     public static void main(String[] args) {
    10                 Scanner in = new Scanner(System.in);
    11                 counter min=new counter();
    12                 PrintWriter out = null;
    13                 try {
    14                     out = new PrintWriter("result.txt");
    15                     int sum = 0;
    16                     for (int i = 1; i <=10; i++) {
    17                         int a = (int) Math.round(Math.random() * 100);
    18                         int b = (int) Math.round(Math.random() * 100);
    19                         int menu = (int) Math.round(Math.random() * 3);
    20                         switch (menu) {
    21                         case 0:
    22                             System.out.println(i+":"+a + "+" + b + "=");
    23                             int c1 = in.nextInt();
    24                             out.println(a + "+" + b + "=" + c1);
    25                             if (c1 == (a + b)) {
    26                                 sum += 10;
    27                                 System.out.println("恭喜答案正确");
    28                             } else {
    29                                 System.out.println("抱歉,答案错误");
    30                             }
    31                             break;
    32                         case 1:
    33                             while (a < b) {
    34                                 b = (int) Math.round(Math.random() * 100);
    35                             }
    36                             System.out.println(i+":"+a + "-" + b + "=");
    37                             int c2 = in.nextInt();
    38                             out.println(a + "-" + b + "=" + c2);
    39                             if (c2 == (a - b)) {
    40                                 sum += 10;
    41                                 System.out.println("恭喜答案正确");
    42                             } else {
    43                                 System.out.println("抱歉,答案错误");
    44                             }
    45 
    46                             break;
    47                         case 2:
    48                             System.out.println(i+":"+a + "*" + b + "=");
    49                             int c3 = in.nextInt();
    50                             out.println(a + "*" + b + "=" + c3);
    51                             if (c3 == a * b) {
    52                                 sum += 10;
    53                                 System.out.println("恭喜答案正确");
    54                             } else {
    55                                 System.out.println("抱歉,答案错误");
    56                             }
    57 
    58                             break;
    59                         case 3:
    60                              while(b == 0){
    61                                     b = (int) Math.round(Math.random() * 100);
    62                                 }
    63                                 while(a % b != 0){
    64                                     a = (int) Math.round(Math.random() * 100);
    65                                     
    66                                 }
    67                             System.out.println(i+":"+a + "/" + b + "=");
    68                             int c4 = in.nextInt();
    69                             if (c4 == a / b) {
    70                                 sum += 10;
    71                                 System.out.println("恭喜,答案正确");
    72                             } else {
    73                                 System.out.println("抱歉,答案错误");
    74                             }
    75 
    76                             break;
    77                         }
    78                     }
    79                     System.out.println("你的得分为" + sum);
    80                     out.println("你的得分为" + sum);
    81                     out.close();
    82                 } catch (FileNotFoundException e) {
    83                     e.printStackTrace();
    84                 }
    85             }
    86         }
    Main
     1 package A;
     2 
     3 public class counter<T> {
     4     private T a;
     5     private T b;
     6     public counter() {
     7         a=null;
     8         b=null;
     9     }
    10     public counter(T a,T b) {
    11         this.a=a;
    12         this.b=b;
    13     }
    14     public int count1(int a,int b) {
    15         return a+b;
    16     }
    17     public int count2(int a,int b) {
    18         return a-b;
    19     }
    20     public int count3(int a,int b) {
    21         return a*b;
    22     }
    23     public int count4(int a,int b) {
    24         return a/b;
    25     }
    26 }
    counter


    第三部分:总结

             1.没有看清楚题目就粗心地在Elipse环境下调试了所有程序,对于命令行交作业时才发现,这一直是我的一大缺点,如影随形,不离不弃,关于还有很多问题不懂;

             2.另外本周我的学习表现整体不是很良好,总感觉知识没消化掉,还有种要被“噎死”的感觉;

             3.本周的学习特色是末尾的合作部分,这让我感到有趣且愉快,我的伙伴常惠琢很默契,不需多解释,但我有时更期待思维碰撞激烈的火花 ♦♦♦;

          

  • 相关阅读:
    点对点PSCV
    开机启动文件夹
    SpringBoot占用端口
    停止8080端口
    java JDK下载与安装教程
    JRebel热部署
    取消ctrl+alt+箭头 旋转
    空指针调试
    xdebug 断点调试,时间过长会出现超时如何解决
    vue 无法加载文件 CProgram Filesnodejsnpm.ps1,因为在此系统上禁止运行脚本
  • 原文地址:https://www.cnblogs.com/yqj-yf-111/p/9930038.html
Copyright © 2011-2022 走看看