王穎奇 20171010129《面向物件程式設計(java)》第十一週學習總結
實驗十一 集合
實驗時間 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:
因為dog類無法強制型別轉換為cat類,所以程式會出現以下錯誤:
更改思路(兩種方法):
(1)輸出cat類時,將dog類放到cat類的輸出迴圈之外,並將Dog(7)中的內容強制轉換成dog類後單獨輸出。
更改後代碼:
package demo; 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 < 7; i++) ((Cat) cats.elementAt(i)).print(); ((Dog) cats.elementAt(7)).print(); } }CatsAndDogs
(2)輸出結果時,使用instanceof語句對類(cat和dog類)進行判斷,根據情況輸出。
更改後代碼:
package demo; 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(); } else ((Dog) cats.elementAt(i)).print(); } }CatsAndDogs
更改後的結果(均為以下結果):
示例程式2:
示例程式3:
程式解析:
該示例檢驗Math.random()方法的隨機性。在理想情況下,該方法應該產生一系列完美的隨機分佈的數字。為了驗證這一點,需要生成數量眾多的隨機數字,然後計算落在不同範圍內的數字量。該程式生成10000個隨數,檢視它們在0~20之間的分布如何。
package demo; import java.util.*; class Counter { int i = 1; //default:即不加任何訪問修飾符,通常稱為“預設訪問模式“。該模式下,只允許在同一個包中進行訪問。 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)); //用Math.random()方法生成0到19的所有整數,並以int型存放,同時用Integer方法封裝到陣列中 if (ht.containsKey(r)) ((Counter) ht.get(r)).i++; else ht.put(r, new Counter()); //輸出r中資料的鍵值對出現的次數 } System.out.println(ht); } }Statistics
執行結果:
關於int i為何種訪問許可權修飾符的思考(上課所提到的):
測試程式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"); } } |
l 在Elipse環境下編輯執行除錯教材360頁程式9-1,結合程式執行結果理解程式;
l 掌握ArrayList、LinkList兩個類的用途及常用API。
ArrayListDemo(執行結果):
增加System.out.println(al.size());語句可以輸出有多少個元素
LinkedListDemo(執行結果):
教材p360 9-1程式:
package linkedList; 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) { List<String> a = new LinkedList<>(); //生成一個String型陣列a,用add方法追加元素 a.add("Amy"); a.add("Carl"); a.add("Erica"); List<String> b = new LinkedList<>(); //生成一個String型陣列b,用add方法追加元素 b.add("Bob"); b.add("Doug"); b.add("Frances"); b.add("Gloria"); // merge the words from b into a //將b陣列中的單詞合併到a中 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); // remove every second word from b //刪除b陣列中的第二個單詞 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中刪除b中的所有單詞 a.removeAll(b); System.out.println(a); } }LinkedListTest
執行結果:
測試程式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()); } } } |
執行結果:
l 在Elipse環境下除錯教材365頁程式9-2,結合執行結果理解程式;瞭解HashSet類的用途及常用API。
package set; import java.util.*; /** * This program uses a set to print all unique words in System.in. * @version 1.12 2015-06-21 * @author Cay Horstmann */ public class SetTest { public static void main(String[] args) { Set<String> words = new HashSet<>(); // HashSet implements Set long totalTime = 0; try (Scanner in = new Scanner(System.in)) { while (in.hasNext()) { String word = in.next(); long callTime = System.currentTimeMillis(); words.add(word); callTime = System.currentTimeMillis() - callTime; totalTime += callTime; } } Iterator<String> iter = words.iterator(); for (int i = 1; i <= 20 && iter.hasNext(); i++) System.out.println(iter.next()); System.out.println(". . ."); System.out.println(words.size() + " distinct words. " + totalTime + " milliseconds."); } }SetTest
l 在Elipse環境下除錯教材367頁-368程式9-3、9-4,結合程式執行結果理解程式;瞭解TreeSet類的用途及常用API。
package treeSet; 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)); sortByDescription.addAll(parts); System.out.println(sortByDescription); } }TreeSetTest
package treeSet; 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); } }Item
執行結果:
測試程式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,結合程式執行結果理解程式;
瞭解HashMap、TreeMap兩個類的用途及常用API。
程式碼:
package map; 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 { 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")); // print all entries System.out.println(staff); // remove an entry staff.remove("567-24-2546"); // replace an entry staff.put("456-62-5527", new Employee("Francesca Miller")); // look up a value System.out.println(staff.get("157-62-7935")); // iterate through all entries staff.forEach((k, v) -> System.out.println("key=" + k + ", value=" + v)); } }MapTest
package map; /** * A minimalist employee class for testing purposes. */ public class Employee { private String name; private double salary; /** * Constructs an employee with $0 salary. * @param n the employee name */ public Employee(String name) { this.name = name; salary = 0; } public String toString() { return "[name=" + name + ", salary=" + salary + "]"; } }Employee
執行結果:
實驗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 ba; 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 Cardtest { // 檔案讀取模組 利用ArrayList構造studentlist存放檔案內容 private static ArrayList<Card> cardlist; public static void main(String[] args) { cardlist = new ArrayList<>(); Scanner scanner = new Scanner(System.in); File file = new File("C:\\身份證號.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 id = linescanner.next(); String sex = linescanner.next(); String age = linescanner.next(); String area = linescanner.nextLine(); Card card = new Card(); card.setName(name); card.setId(id); card.setSex(sex); int a = Integer.parseInt(age); card.setAge(a); card.setArea(area); cardlist.add(card); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block System.out.println("資訊檔案找不到"); e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block System.out.println("資訊檔案讀取錯誤"); e.printStackTrace(); } // 根據實驗要求,利用switch語句選不同操作的模組 boolean isTrue = true; while (isTrue) { System.out.println("歡迎來到身份證資訊查詢系統,請選擇你的操作"); System.out.println("1.字典排序"); System.out.println("2.輸出年齡最大和最小的人"); System.out.println("3.尋找年齡相近的人"); System.out.println("4.尋找老鄉"); System.out.println("5.退出"); int nextInt = scanner.nextInt(); switch (nextInt) { case 1: Collections.sort(cardlist); System.out.println(cardlist.toString()); break; case 2: int max = 0, min = 100; int j, k1 = 0, k2 = 0; for (int i = 1; i < cardlist.size(); i++) { j = cardlist.get(i).getAge(); if (j > max) { max = j; k1 = i; } if (j < min) { min = j; k2 = i; } } System.out.println("年齡最大:" + cardlist.get(k1)); System.out.println("年齡最小:" + cardlist.get(k2)); break; case 3: System.out.println("你的年齡:"); int yourage = scanner.nextInt(); int near = agenear(yourage); System.out.println(cardlist.get(near)); break; case 4: System.out.println("你的家鄉?"); String find = scanner.next(); String place = find.substring(0, 3); for (int i = 0; i < cardlist.size(); i++) { if (cardlist.get(i).getArea().substring(1, 4).equals(place)) System.out.println("老鄉 " + cardlist.get(i)); } break; case 5: isTrue = false; System.out.println("程式已退出!"); break; default: System.out.println("輸入有誤"); } } } //尋找年齡相近的人的位置 public static int agenear(int age) { int j = 0, min = 100, value = 0, k = 0; for (int i = 0; i < cardlist.size(); i++) { value = cardlist.get(i).getAge() - age; if (value < 0) value = -value; if (value < min) { min = value; k = i; } } return k; } }Cardtest
package ba; //對資料具體處理的模組 public class Card implements Comparable<Card> { private String name; private String id; private String sex; private int age; private String area; //返回具體的資料 public String getName() { return name; } public void setName(String name) { this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getArea() { return area; } public void setArea(String area) { this.area = area; } // 按名字的字典順序排列 public int compareTo(Card c) { return this.name.compareTo(c.getName()); } // 用toString方法返回資料 public String toString() { return name + "\t" + id + "\t" + sex + "\t" + age + "\t" + area + "\n"; } } CardCard
完善建議:
簡化程式碼的行數
l 各自執行合作伙伴實驗十程式設計練習2,結合使用體驗對所執行程式提出完善建議;
王斌龍的程式碼:
package 異常; import java.io.PrintWriter; import java.util.Scanner; public class Test { public static void main(String[] args) { @SuppressWarnings("resource") Scanner in = new Scanner(System.in); Demo demo=new Demo(); PrintWriter output = null; try { output = new PrintWriter("test.txt"); } catch (Exception e) { e.printStackTrace(); } int sum = 0; for (int i = 0; i < 10; i++) { int a = (int) Math.round(Math.random() * 100); int b = (int) Math.round(Math.random() * 100); int c = (int) Math.round(Math.random() * 3); switch (c) { case 0: System.out.println(a + "+" + b + "="); int d0 = in.nextInt(); output.println(a + "+" + b + "=" + d0); if (d0 == demo.demo1(a, b)) { sum += 10; System.out.println("恭喜答案正確"); } else { System.out.println("抱歉,答案錯誤"); } break; case 1: while (a < b) { int x = a; a = b; b = x; } System.out.println(a + "-" + b + "="); int d1 = in.nextInt(); output.println(a + "-" + b + "=" + d1); if (d1 == demo.demo2(a, b)) { sum += 10; System.out.println("恭喜答案正確"); } else { System.out.println("抱歉,答案錯誤"); } break; case 2: System.out.println(a + "*" + b + "="); int d2 = in.nextInt(); output.println(a + "*" + b + "=" + d2); if (d2 == demo.demo3(a, b)) { sum += 10; System.out.println("恭喜答案正確"); } else { System.out.println("抱歉,答案錯誤"); } break; case 3: while (b == 0 || a % b != 0) { a = (int) Math.round(Math.random() * 100); b = (int) Math.round(Math.random() * 100); } System.out.println(a + "/" + b + "="); int d3 = in.nextInt(); output.println(a + "/" + b + "=" + d3); if (d3 == demo.demo4(a, b)) { sum += 10; System.out.println("恭喜答案正確"); } else { System.out.println("抱歉,答案錯誤"); } break; } } System.out.println("你的得分為" + sum); output.println("你的得分為" + sum); output.close(); } }Test
package 異常; public class Demo<T> { private T a; private T b; public Demo() { a = null; b = null; } public Demo(T a, T b) { this.a = a; this.b = b; } public int demo1(int a, int b) { return a + b; } public int demo2(int a, int b) { return a - b; } public int demo3(int a, int b) { return a * b; } public int demo4(int a, int b) { return a / b; } }Demo
完善建議:
個人認為,為了程式的嚴謹性,需要在Demo類中的除法計算語句(模組)中加入“a>b,a%b==0,(a/b)%1==0”的限制條件(語句)
l 採用結對程式設計方式,與學習夥伴合作完成實驗九程式設計練習1;
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 { /** * 1.檔案讀取模組 利用ArrayList構造studentlist存放檔案內容2. 建立檔案字元流,分類讀取檔案內容 3.try/catch語句捕獲異常 */ 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(); // 加入的捕獲異常程式碼 } /* * 1.根據實驗要求,選擇具體操作的模組 2.利用switch語句選擇具體的操作 */ 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
package shen; /* * 分類返回具體資料 *利用介面技術比較name的大小 *用toString方法返回資料 */ 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"; } }Student
l 採用結對程式設計方式,與學習夥伴合作完成實驗十程式設計練習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("text.txt"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block 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中 */