1、檔案資料排序
輸入:建立一個文字檔input.txt,內容為每一行一個數字
輸出:開啟input.txt,讀入檔案中的資料,將資料排序(從小到大),將資料輸出到output.txt,格式仍然是每一行一個數字
2、除錯
以下是一段計算複利之本利和的副程式,參數之balance為帳戶餘額,rate為利率(介於0~1之間的數字,若利率為1%,則rate=0.01),但是這個副程式卻因為漏掉一個關鍵性的機制而無法正確地執行。請寫一個Java應用程式,在程式中呼叫computeInterest() 這個副程式以計算複利的本利和。
注意:除了撰寫主程式之外,您還必須改正computeInterest() 的問題,才能夠正確地執行程式。
public void computeInterest(double balance, double rate) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("帳戶餘額:" + balance);
System.out.println("目前利率:" + rate);
System.out.print("請輸入存款期數:");
String line = br.readLine();
int n = Integer.parseInt(line);
double sum = balance;
for (int i = 0; i < n; i++) {
sum = sum * (1.0 + rate);
}
System.out.println(n + "期之後的本利和為:" + sum);
}
2007-01-10 11:50:56 · 2 個解答 · 發問者 ? 1 in 電腦與網際網路 ➔ 程式設計
請參考我的做法,
1、檔案資料排序
import java.io.*;
import java.util.*;
public class A {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new FileReader(new File("input.txt")));
String s = null;
ArrayList al = new ArrayList();
while ((s = br.readLine()) != null) {
al.add(s);
}
br.close();
int[] ary = new int[al.size()];
for (int i = 0; i < ary.length; i++) {
ary[i] = Integer.parseInt((String)al.get(i));
}
Arrays.sort(ary);
PrintWriter pw = new PrintWriter("output.txt");
for (int i = 0; i < ary.length; i++) {
System.out.println(ary[i]);
pw.println(ary[i]);
}
pw.close();
}
}
2、除錯
import java.io.*;
public class B {
public void computeInterest(double balance, double rate) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("帳戶餘額:" + balance);
System.out.println("目前利率:" + rate);
System.out.print("請輸入存款期數:");
String line = null;
try {
line = br.readLine();
} catch (IOException ioe) {
System.err.println("Unidentified number");
System.exit(1);
}
int n = Integer.parseInt(line);
double sum = balance;
for (int i = 0; i < n; i++) {
sum = sum * (1.0 + rate);
}
System.out.println(n + "期之後的本利和為:" + sum);
}
public static void main(String[] args) {
B b = new B();
b.computeInterest(10000.0D, 0.01D);
}
}
2007-01-11 07:05:42 · answer #1 · answered by ? 7 · 0⤊ 0⤋
第二題
import java.io.*;
public class computeInterest {
double balance;
double rate;
public computeInterest() {}
public static void main(String[] args) throws IOException {
computeInterest compute = new computeInterest();
compute.computeInterest();
}
public void computeInterest() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("請輸入帳戶餘額:");
String line1 = br.readLine();
double balance = Double.parseDouble(line1);
System.out.println("請輸入利率:" );
String line2 = br.readLine();
double rate = Double.parseDouble(line2);
System.out.print("請輸入存款期數:");
String line = br.readLine();
int n = Integer.parseInt(line);
double sum = balance;
for (int i = 0; i < n; i++) {
sum = sum * (1.0 + rate);
}
System.out.println(n + "期之後的本利和為:" + sum);
}
}
---------------------------------------------------------
不然就是,直接給數據
--------------------------------
import java.io.*;
public class computeInterest {
public computeInterest() {}
public static void main(String[] args) throws IOException {
computeInterest compute = new computeInterest();
compute.computeInterest(4000,0.01);//直接給數據
}
public void computeInterest(double balance, double rate) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("帳戶餘額:" + balance);
System.out.println("目前利率:" + rate);
System.out.print("請輸入存款期數:");
String line = br.readLine();
int n = Integer.parseInt(line);
double sum = balance;
for (int i = 0; i < n; i++) {
sum = sum * (1.0 + rate);
}
System.out.println(n + "期之後的本利和為:" + sum);
}
}
2007-01-11 07:26:48 · answer #2 · answered by ? 2 · 0⤊ 0⤋