各位大大好:
事情是這樣的~
我現在有3個CODE在資料夾:
分別如下:
程式碼如下:
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.*;
/**
*
* @author User
*/
class AccountError extends Exception{
public AccountError(String message){super(message);}
}
class Account implements Serializable{
private long balance;
public Account(long money){balance=money;}
public void deposite(long money)throws AccountError{
if(money<0)throw new AccountError("存款金額不可為負值");
else
balance+=money;}
public void withdraw (long money)throws AccountError {
if(money>balance)throw new AccountError("存款不足");
else
balance-=money;
}
public long ckeckBalance(){return balance;}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author User
*/
public class WriteAccountObject {
/**
* @param args the command line arguments
*/
public static void main(String[] argv) throws IOException{
// TODO code application logic here
System.out.print("簡單帳戶模擬計算,");
System.out.println("開戶需要存多少錢?");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Account myAccount=new Account(Integer.parseInt(br.readLine()));
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("AccountFile"));
oos.writeObject(myAccount);
oos.flush();
oos.close();
System.out.println("已將帳戶資訊存至檔案 AccountFile!");
}
}
原來我是打算做到這裡先開戶存1000到AccountFile
但是失敗
所以要做的下一個 程式碼:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author User
*/
public class ReadAccountObject {
/**
* @param args the command line arguments
*/
public static void main(String[] argv) throws IOException,ClassNotFoundException{
// TODO code application logic here
System.out.println("由檔案讀取帳戶資訊");
ObjectInputStream ois=new ObjectInputStream(new FileInputStream("AccountFile"));
Account myAccount=(Account)ois.readObject();
ois.close();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
try{
while(true){
System.out.print("\n您現在要(1)從款(2)提款,");
int choice=Integer.parseInt(br.readLine());
System.out.print("請輸入金額,");
int money=Integer.parseInt(br.readLine());
if(choice==1){
myAccount.deposite(money);
System.out.print("存了"+money+"元後,帳戶還剩");
System.out.println(myAccount.ckeckBalance()+"元");
}
else if(choice==2){
myAccount.deposite(money);
System.out.print("存了"+money+"元後,帳戶還剩");
System.out.println(myAccount.ckeckBalance()+"元");
}
}
}
catch(AccountError e){
System.out.println(e);
}
}
}
也無法計算出甚麼?
真的怎麼想都想不出要改哪裡~因為程式碼也都沒有反紅??QQ
建議你
拿這裡的Example 2去改成你要的結果比較快
另外,點這裡是我這次鐵人賽唯一的一篇文章,喜歡的話左上角點 Like
最後
如果你還打算繼續問下去的話
請去前一個問題選個最佳解答
做為結案的動作
如果沒打算繼續問的話
就算了
你要不要先學會看錯誤訊息?
不是很明顯告訴你 readxxxxx.java 23行有問題。
問題的錯誤就是找不到指定檔案。
你上篇找到檔案的問題解決了沒?
有阿~ 上一篇解決了~所以我再建一個AccountFile的意思嗎?
那你這篇為何會犯上一篇的錯。
還是只是解決你程式不會出問題。
但你根本不懂怎麼解決。
我用絕對路徑還是不可以....
但是上面那個怎麼改還是無法.........
用絕對路徑也無法
我後來用下面這個例子是成功的~
程式碼1:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author User
*/
public class Student implements Serializable {
public Student (String s,short e,short m,short j){
name=s;
EScore=e;
MScore=m;
JScore=j;
}
public Student(){}
public String getN(){return name;}
public Short getE(){return EScore;}
public Short getM(){return MScore;}
public Short getJ(){return JScore;}
public double getAvg(){
return(EScore+MScore+JScore);
}
private String name;
private short EScore;
private short MScore;
private short JScore;
}````
---
程式碼2:
````import java.io.*;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author User
*/
public class WriteObject {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
// TODO code application logic here
System.out.println("請輸入要建立的學生成績檔檔名");
System.out.print("➡");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String filename=br.readLine();
ObjectOutputStream os=new ObjectOutputStream(new FileOutputStream(filename));
String str=new String();
int counter=0;
do{
counter++;
System.out.print("請輸入學生姓名:");
String name=br.readLine();
System.out.print("請輸入英文分數:");
str=br.readLine();
short e=Short.parseShort(str);
System.out.print("請輸入數學分數:");
str=br.readLine();
short m=Short.parseShort(str);
System.out.print("請輸入Java分數:");
str=br.readLine();
short j=Short.parseShort(str);
Student ss=new Student(name,e,m,j);
os.writeObject(ss);
System.out.print("還要輸入另外一筆資料嗎(y/n)");
str=br.readLine();
}while(str.equalsIgnoreCase("Y"));
os.flush();
os.close();
System.out.println("\n已寫入"+counter+"筆學生資料至檔案"+filename);
}
}
ObjectStream File FileReader/FileWriter
程式碼3:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author User
*/
public class ReadObject {
public static void main(String[] args) throws IOException,ClassNotFoundException {
// TODO code application logic here
System.out.println("要讀取的學生成績檔檔名");
System.out.print("➡");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String filename=br.readLine();
int counter=0;
double Esum=0;
double Msum=0;
double Jsum=0;
Student ss=new Student();
System.out.println("姓名\t英文\t數學\tJava\t平均");
System.out.println("-------------------------------------");
try(ObjectInputStream ois=
new ObjectInputStream(new FileInputStream(filename))){
while(true){
ss=(Student)ois.readObject();
counter++;
Esum +=ss.getE();
Msum +=ss.getM();
Jsum +=ss.getJ();
System.out.println(ss.getN()+'\t'+ss.getE()+'\t'
+ss.getM()+'\t'+ss.getJ()+'\t'
+ss.getAvg());
}
}
catch(EOFException e){
System.out.println("\n已從檔案"+filename+"讀取"+counter+"筆學生資料");
System.out.println("\n全員英文平均"+(Esum/counter));
System.out.println("\n全員數學平均"+(Msum/counter));
System.out.println("\n全員JAVA平均"+(Jsum/counter));
}
}
}