11201771010119窮吉
理論知識:
一般將數據結構分為兩大類:線性數據結構和非線性數據結構
線性數據結構:線性表、棧、隊列、串、數組和文件
非線性數據結構:樹和圖。
線性表:1.所有數據元素在同一個線性表中必須是相同的數據類型。
2. 線性表按其存儲結構可分為順序表和鏈表
3. 用順序存儲結構存儲的線性表稱為順序表
4. 順序表將線性表中的數據元素依次存放在某個存儲區域中。一維數組就是用順序方式存儲的線性表。
5. 用鏈式存儲結構存儲的線性表稱為鏈表
棧:1. 棧(Stack)也是一種特殊的線性表,是一種後進先出的結構
2. 棧是限定僅在表尾進行插入和刪除運算的線性表,表尾稱為棧頂(top),表頭稱為棧底(bottom)。
3. 棧的物理存儲可以用順序存儲結構,也可以用鏈式存儲結構
集合:JAVA的集合框架實現對各種數據結構的封裝,以降低對數據管理與處理的難度。
是一種包含多個元素並提供對所包含元素操作方法的類,其包含的元素可以由同一類型的對象組成,也可以由不同類型的對象組成。
集合類的作用:
1. Java的集合類提供了一些基本數據結構的支持。
2.例如Vector、Hashtable、Stack等。
集合類的特點:1. 只容納對象
2.集合類容納的對象都是Object類的實例(一旦把一個對象置入集合類中,它的類信息將丟失)
Vector類:類似長度可變的數組,只能存放對象,元素通過下標進行訪問。
Stack類:它描述堆棧數據結構。(所有對象都有一個散列碼,可以通過Object類的hashCode方法獲得。)
實驗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();//進行強制類型轉化 } }
更改後程序如下:
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++) if (cats.elementAt(i) instanceof Cat) //判斷是否能進行強制類型轉換 { ((Cat) cats.elementAt(i)).print();//能進行強制類型轉換,輸出為Cat型 } else { ((Dog) cats.elementAt(i)).print();//不能進行強制類型轉化,輸出為Dog型 } } }
示例程序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;//不加權限修飾符:friendly型 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));//生成0到20(不包括20)的整型隨機數 if (ht.containsKey(r))//判斷r是否是哈希表中一個元素的鍵值 ((Counter) ht.get(r)).i++;//通過get方法獲得其值 else ht.put(r, new Counter());//ht不存在 } System.out.println(ht); } }
測試程序2:
使用JDK命令編輯運行ArrayListDemo和LinkedListDemo兩個程序,結合程序運行結果理解程序;
ArrayListDemo:
import java.util.*; public class ArrayListDemo//ArrayList使用了數組的實現 { public static void main(String[] argv) { ArrayList al = new ArrayList(); //在ArrayList中添加大量元素 al.add(new Integer(11)); al.add(new Integer(12)); al.add(new Integer(13)); al.add(new String("hello"));//下標從0開始,添加4個元素 // 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)); } } }
LinkedListDemo:
程序運行結果如下:
l 在Elipse環境下編輯運行調試教材360頁程序9-1,結合程序運行結果理解程序;
l 掌握ArrayList、LinkList兩個類的用途及常用API。
程序如下:
import java.util.*; /** * This program demonstrates operations on linked lists. * @version 1.11 2012-01-26 * @author Cay Horstmann */ public class LinkedListTest { public static void main(String[] args) { //創建a和b兩個鏈表 List<String> a = new LinkedList<>();//泛型 a.add("Amy"); a.add("Carl"); a.add("Erica"); List<String> b = new LinkedList<>();//泛型 b.add("Bob"); b.add("Doug"); b.add("Frances"); b.add("Gloria"); //合並a和b中的詞 ListIterator<String> aIter = a.listIterator(); Iterator<String> bIter = b.iterator(); while (bIter.hasNext()) { if (aIter.hasNext()) aIter.next(); aIter.add(bIter.next()); } System.out.println(a); //從第二個鏈表中每隔一個元素刪除一個元素 bIter = b.iterator(); while (bIter.hasNext()) { bIter.next(); // skip one element if (bIter.hasNext()) { bIter.next(); // skip next element bIter.remove(); // remove that element } } System.out.println(b); // bulk operation: remove all words in b from a a.removeAll(b); System.out.println(a);//通過AbstractCollection類中的toString方法打印出鏈表a中的所有元素 } }
測試程序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"); // 復制 h.add("Three"); Iterator it = h.iterator(); while (it.hasNext()) { System.out.println(it.next()); } } }
程序運行結果如下:
在Elipse環境下調試教材367頁-368程序9-3、9-4,結合程序運行結果理解程序;了解TreeSet類的用途及常用API。
9—3:
import java.util.*; /** * An item with a description and a part number. */ public class Item implements Comparable<Item>//接口(泛型) { private String description; private int partNumber; /** * Constructs an item. * * @param aDescription * the item‘s description * @param aPartNumber * the item‘s part number */ public Item(String aDescription, int aPartNumber)//構造器 { description = aDescription; partNumber = aPartNumber; } /** * Gets the description of this item. * * @return the description */ public String getDescription() { return description; } public String toString() { return "[description=" + description + ", partNumber=" + partNumber + "]"; } public boolean equals(Object otherObject) { if (this == otherObject) return true; if (otherObject == null) return false; if (getClass() != otherObject.getClass()) return false; Item other = (Item) otherObject; return Objects.equals(description, other.description) && partNumber == other.partNumber; } public int hashCode() { return Objects.hash(description, partNumber); } public int compareTo(Item other)//排序 { int diff = Integer.compare(partNumber, other.partNumber); return diff != 0 ? diff : description.compareTo(other.description); } }
9—4:
import java.util.*; /** * This program sorts a set of item by comparing their descriptions. * @version 1.12 2015-06-21 * @author Cay Horstmann */ public class TreeSetTest { public static void main(String[] args) { SortedSet<Item> parts = new TreeSet<>(); parts.add(new Item("Toaster", 1234)); parts.add(new Item("Widget", 4562)); parts.add(new Item("Modem", 9912)); System.out.println(parts); NavigableSet<Item> sortByDescription = new TreeSet<>( Comparator.comparing(Item::getDescription));//把自定義類對象存入TreeSet進行排序 sortByDescription.addAll(parts); System.out.println(sortByDescription); } }
測試程序4:
l 使用JDK命令運行HashMapDemo程序,結合程序運行結果理解程序;
程序如下:
import java.util.*; public class HashMapDemo //基於哈希表的 Map接口的實現,提供所有可選的映射操作 { public static void main(String[] argv) { HashMap h = new HashMap(); // 哈希映射從公司名稱到地址 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); } }
在Elipse環境下調試教材373頁程序9-6,結合程序運行結果理解程序;
了解HashMap、TreeMap兩個類的用途及常用API。
程序如下:
import java.util.*; /** * This program demonstrates the use of a map with key type String and value type Employee. * @version 1.12 2015-06-21 * @author Cay Horstmann */ public class MapTest//Map在有映射關系時,可以優先考慮 { public static void main(String[] args) { Map<String, Employee> staff = new HashMap<>(); staff.put("144-25-5464", new Employee("Amy Lee")); staff.put("567-24-2546", new Employee("Harry Hacker")); staff.put("157-62-7935", new Employee("Gary Cooper")); staff.put("456-62-5527", new Employee("Francesca Cruz")); // 打印所有條目 System.out.println(staff); // 刪除一個條目 staff.remove("567-24-2546"); // 替換一個條目 staff.put("456-62-5527", new Employee("Francesca Miller")); // 查找一個值 System.out.println(staff.get("157-62-7935")); // 遍歷所有條目 staff.forEach((k, v) -> System.out.println("key=" + k + ", value=" + v)); } }
程序運行結果如下:
實驗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,結合使用體驗對所運行程序提出完善建議;
package 異常;
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
// 用戶的答案要從鍵盤輸入,因此需要一個鍵盤輸入流
Scanner in = new Scanner(System.in);
// 定義一個變量用來統計得分
int sum = 0;
for (int i = 0; i < 10; i++) {
// 隨機生成兩個10以內的隨機數作為被除數和除數
int a = (int) Math.round(Math.random() * 100);
int b = (int) Math.round(Math.random() * 100);
System.out.println(a + "/" + b + "=");
// 定義一個整數用來接收用戶輸入的答案
int c = in.nextInt();
// 判斷用戶輸入的答案是否正確,正確給10分,錯誤不給分
if (c == a / b) {
sum += 100;
System.out.println("恭喜答案正確");
}
else {
System.out.println("抱歉,答案錯誤");
}
}
//輸出用戶的成績
System.out.println("你的得分為"+sum);
}
}
package shen; 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("C:\\Users\\ASUS\\Desktop\\新建文件夾\\身份證號.txt"); 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 = -value; if (value < min) { min = value; k = i; } } return k; } } Main
public class Student implements Comparable<Student> { private String name; private String number ; private String sex ; private int age; private String province; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getnumber() { return number; } public void setnumber(String number) { this.number = number; } public String getsex() { return sex ; } public void setsex(String sex ) { this.sex =sex ; } public int getage() { return age; } public void setage(int age) { // int a = Integer.parseInt(age); this.age= age; } public String getprovince() { return province; } public void setprovince(String province) { this.province=province ; } public int compareTo(Student o) { return this.name.compareTo(o.getName()); } public String toString() { return name+"\t"+sex+"\t"+age+"\t"+number+"\t"+province+"\n"; } }
各自運行合作夥伴實驗十編程練習2,結合使用體驗對所運行程序提出完善建議;
package demo; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Random; import java.util.Scanner; public class demo { public static void main(String[] args) { /** *文件輸出模塊 *1.調用構造函數counter *2.創建文件字符流,將out中的內容設為空(null) *3.將out結果輸出到test.txt中 *4.try/catch模塊捕獲異常 */ Scanner in = new Scanner(System.in); yunsuan counter = new yunsuan(); PrintWriter out = null; try { out = new PrintWriter("test.txt"); } catch (FileNotFoundException e) { System.out.println("文件夾輸出失敗"); e.printStackTrace(); } /** *四則運算生成模塊 *1.定義一個int型sum,計算成績,並說明生成的運算類型 *2.for語句,將{}內的內容循環10次,從而生成10道題目 *3.隨機生成int型a與b,範圍在0到100以內;生成int型m,範圍為1,2,3,4 *4.利用switch語句,根據生成m的值,隨機生成加減乘除四則運算 *5.將循環結果輸出到test.txt中 */ int sum = 0; System.out.println("隨機生成的四則運算類型"); System.out.println("類型1:除法"); System.out.println("類型2:乘法"); System.out.println("類型3:加法"); System.out.println("類型4:減法"); for (int i = 1; i <= 10; i++) { int a = (int) Math.round(Math.random() * 100); int b = (int) Math.round(Math.random() * 100); int m; Random rand = new Random(); m = (int) rand.nextInt(4) + 1; System.out.println("隨機生成的四則運算類型:" + m); switch (m) { case 1: a = b + (int) Math.round(Math.random() * 100); while(b == 0){ b = (int) Math.round(Math.random() * 100); } while(a % b != 0){ a = (int) Math.round(Math.random() * 100); } //若生成的除法式子必須能整除,且滿足分母為0的條件,則a一定要大於b,且a模b的結果要為0。 System.out.println(i + ": " + a + "/" + b + "="); int c0 = in.nextInt(); out.println(a + "/" + b + "=" + c0); if (c0 == counter.division(a, b)) { sum += 10; System.out.println("right!"); } else { System.out.println("error!"); } break; case 2: System.out.println(i + ": " + a + "*" + b + "="); int c = in.nextInt(); out.println(a + "*" + b + "=" + c); if (c == counter.multiplication(a, b)) { sum += 10; System.out.println("right!"); } else { System.out.println("error!"); } break; case 3: System.out.println(i + ": " + a + "+" + b + "="); int c1 = in.nextInt(); out.println(a + "+" + b + "=" + c1); if (c1 == counter.add(a, b)) { sum += 10; System.out.println("right!"); } else { System.out.println("error!"); } break; case 4: while (a < b) { b = (int) Math.round(Math.random() * 100); } //因為不能產生運算結果為負數的減法式子,所以a一定要大於b。若a<b,則重新生成b。 System.out.println(i + ": " + a + "-" + b + "="); int c2 = in.nextInt(); out.println(a + "-" + b + "=" + c2); if (c2 == counter.reduce(a, b)) { sum += 10; System.out.println("right!"); } else { System.out.println("error!"); } break; } } System.out.println("成績" + sum); out.println("成績:" + sum); out.close(); } }
package demo; public class yunsuan<T> { private T a; private T b; public yunsuan() { a = null; b = null; } public yunsuan(T a, T b) { this.a = a; this.b = b; } public int add(int a,int b) { return a + b; } public int reduce(int a, int b) { return a - b; } public int multiplication(int a, int b) { return a * b; } public int division(int a, int b) { if (b != 0 && a%b==0) return a / b; else return 0; } }
采用結對編程方式,與學習夥伴合作完成實驗九編程練習1
import java.io; 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 studentlist = new ArrayList<>(); 16 Scanner scanner = new Scanner(System.in); 17 File file = new File("C:\\下載\\身份證號.txt"); 18 try { 19 FileInputStream fis = new FileInputStream(file); 20 BufferedReader in = new BufferedReader(new InputStreamReader(fis)); 21 String temp = null; 22 while ((temp = in.readLine()) != null) { 23 24 Scanner linescanner = new Scanner(temp); 25 26 linescanner.useDelimiter(" "); 27 String name = linescanner.next(); 28 String number = linescanner.next(); 29 String sex = linescanner.next(); 30 String age = linescanner.next(); 31 String province =linescanner.nextLine(); 32 Student student = new Student(); 33 student.setName(name); 34 student.setnumber(number); 35 student.setsex(sex); 36 int a = Integer.parseInt(age); 37 student.setage(a); 38 student.setprovince(province); 39 studentlist.add(student); 40 41 } 42 } catch (FileNotFoundException e) { 43 System.out.println("學生信息文件找不到"); 44 e.printStackTrace(); 45 } catch (IOException e) { 46 System.out.println("學生信息文件讀取錯誤"); 47 e.printStackTrace(); 48 } 49 boolean isTrue = true; 50 while (isTrue) { 51 System.out.println("選擇你的操作,輸入正確格式的選項"); 52 System.out.println("1.按姓名字典序輸出人員信息"); 53 System.out.println("2.輸出年齡最大和年齡最小的人"); 54 System.out.println("3.查找老鄉"); 55 System.out.println("4.查找年齡相近的人"); 56 System.out.println("5.退出"); 57 String m = scanner.next(); 58 switch (m) { 59 case "1": 60 Collections.sort(studentlist); 61 System.out.println(studentlist.toString()); 62 break; 63 case "2": 64 int max=0,min=100; 65 int j,k1 = 0,k2=0; 66 for(int i=1;i<studentlist.size();i++) 67 { 68 j=studentlist.get(i).getage(); 69 if(j>max) 70 { 71 max=j; 72 k1=i; 73 } 74 if(j<min) 75 { 76 min=j; 77 k2=i; 78 } 79 80 } 81 System.out.println("年齡最大:"+studentlist.get(k1)); 82 System.out.println("年齡最小:"+studentlist.get(k2)); 83 break; 84 case "3": 85 System.out.println("輸入省份"); 86 String find = scanner.next(); 87 String place=find.substring(0,3); 88 for (int i = 0; i <studentlist.size(); i++) 89 { 90 if(studentlist.get(i).getprovince().substring(1,4).equals(place)) 91 System.out.println("老鄉"+studentlist.get(i)); 92 } 93 break; 94 95 case "4": 96 System.out.println("年齡:"); 97 int yourage = scanner.nextInt(); 98 int near=agenear(yourage); 99 int value=yourage-studentlist.get(near).getage(); 100 System.out.println(""+studentlist.get(near)); 101 break; 102 case "5": 103 isTrue = false; 104 System.out.println("退出程序!"); 105 break; 106 default: 107 System.out.println("輸入有誤"); 108 109 } 110 } 111 } 112 public static int agenear(int age) { 113 int j=0,min=53,value=0,k=0; 114 for (int i = 0; i < studentlist.size(); i++) 115 { 116 value=studentlist.get(i).getage()-age; 117 if(value<0) value=-value; 118 if (value<min) 119 { 120 min=value; 121 k=i; 122 } 123 } 124 return k; 125 } 126 127 }
public class Student implements Comparable<Student> { 2 3 private String name; 4 private String number ; 5 private String sex ; 6 private int age; 7 private String province; 8 9 public String getName() { 10 return name; 11 } 12 public void setName(String name) { 13 this.name = name; 14 } 15 public String getnumber() { 16 return number; 17 } 18 public void setnumber(String number) { 19 this.number = number; 20 } 21 public String getsex() { 22 return sex ; 23 } 24 public void setsex(String sex ) { 25 this.sex =sex ; 26 } 27 public int getage() { 28 29 return age; 30 } 31 public void setage(int age) { 32 // int a = Integer.parseInt(age); 33 this.age= age; 34 } 35 36 public String getprovince() { 37 return province; 38 } 39 public void setprovince(String province) { 40 this.province=province ; 41 } 42 43 public int compareTo(Student o) { 44 return this.name.compareTo(o.getName()); 45 } 46 47 public String toString() { 48 return name+"\t"+sex+"\t"+age+"\t"+number+"\t"+province+"\n"; 49 } 50 }
采用結對編程方式,與學習夥伴合作完成實驗十編程練習2
package XOI;
import java.util.Random;
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
public class Main{
public static void main(String[] args)
{
yunsuan counter=new yunsuan();//與其它類建立聯系
PrintWriter out=null;
try {
out=new PrintWriter("D:/text.txt");//將文件裏的內容讀入到D盤名叫text
}catch(FileNotFoundException e) {
System.out.println("文件找不到");
e.printStackTrace();
}
int sum=0;
for(int i=0;i<10;i++)
{
int a=new Random().nextInt(100);
int b=new Random().nextInt(100);
Scanner in=new Scanner(System.in);
//in.close();
switch((int)(Math.random()*4))
{
case 0:
System.out.println( ""+a+"+"+b+"=");
int c1 = in.nextInt();
out.println(a+"+"+b+"="+c1);
if (c1 == counter.plus(a, b)) {
sum += 10;
System.out.println("答案正確");
}
else {
System.out.println("答案錯誤");
}
break ;
case 1:
if(a<b)
{
int temp=a;
a=b;
b=temp;
}//為避免減數比被減數大的情況
System.out.println(""+a+"-"+b+"=");
/*while((a-b)<0)
{
b = (int) Math.round(Math.random() * 100);
}*/
int c2 = in.nextInt();
out.println(a+"-"+b+"="+c2);
if (c2 == counter.minus(a, b)) {
sum += 10;
System.out.println("答案正確");
}
else {
System.out.println("答案錯誤");
}
break ;
case 2:
System.out.println(""+a+"*"+b+"=");
int c = in.nextInt();
out.println(a+"*"+b+"="+c);
if (c == counter.multiply(a, b)) {
sum += 10;
System.out.println("答案正確");
}
else {
System.out.println("答案錯誤");
}
break;
case 3:
while(b==0)
{ b = (int) Math.round(Math.random() * 100);//滿足分母不為0
}
while(a%b!=0)
{
a = (int) Math.round(Math.random() * 100);
b = (int) Math.round(Math.random() * 100);
}
System.out.println(""+a+"/"+b+"=");
int c0= in.nextInt();
out.println(a+"/"+b+"="+c0);
if (c0 == counter.divide(a, b)) {
sum += 10;
System.out.println("答案正確");
}
else {
System.out.println("答案錯誤");
}
break;
}
}
System.out.println("totlescore:"+sum);
out.println(sum);
out.close();
}
}
package XOI;
public class yunsuan <T>{
private T a;
private T b;
public void yunsaun()
{
a=null;
b=null;
}
public void yunsuan(T a,T b)
{
this.a=a;
this.b=b;
}
public int plus(int a,int b)
{
return a+b;
}
public int minus(int a,int b)
{
return a-b;
}
public int multiply(int a,int b)
{
return a*b;
}
public int divide(int a,int b)
{
if(b!=0 && a%b==0)
return a/b;
else
return 0;
}
}
實驗總結:這次實驗通過互相學習感受自己和別人所寫代碼的不同相互學習進行比較。
11201771010119窮吉