data.txt => 

1+20+500+8000

5+60+700+9000

 

-------------讀取內容並顯示-------------

 

package chap1;

import java.io.BufferedReader;
import java.io.FileReader;

public class t1202 {
 public static void main(String args[]) throws Exception {
   BufferedReader fr = new BufferedReader(new FileReader("data.txt"));
   String number="4020Q090",name="李依靜";
   
   String str = null;
   int num =0 , sum=0 ;
     
   while((str=fr.readLine()) != null){  // != 不等於
    System.out.println(str);
   
   }
   }
 }

 

---

 

 

package chap1;

import java.io.BufferedReader;
import java.io.FileReader;

public class t1202 {
public static void main(String args[]) throws Exception {
   BufferedReader fr = new BufferedReader(new FileReader("data.txt"));
   String number="4020Q090",name="李依靜";
   
   String str = null;
 int num =0 , sum=0 ;
     
 while((str=fr.readLine()) != null){  // != 不等於
    System.out.println(str);
   
 for(int i=0;i<str.length();i++){
 
 if(str.charAt(i)  !='+'){  // 讀到+字號就自動輸出前面的數值
 
  num = num*10 + str.charAt(i)-48;
  }
 else {
    System.out.println(num);
    num = 0;
    }
    System.out.println(num);
    num = 0;
   
   }
   }
 }}

 

 ---

 

 

package chap1;

import java.io.BufferedReader;
import java.io.FileReader;

public class t1202 {
 public static void main(String args[]) throws Exception {
  BufferedReader fr = new BufferedReader(new FileReader("data.txt"));
  String number = "4020Q090", name = "李依靜";

  String str = null;
  int num = 0, sum = 0;

  while ((str = fr.readLine()) != null) { // != 不等於
   System.out.println(str);

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

    if (str.charAt(i) != '+') { // 讀到+字號就自動輸出前面的數值

     num = num * 10 + str.charAt(i) - 48;
    } else {
     System.out.println(num);
     sum=sum+num;
     num = 0;
     
    }
   }
     System.out.println(num);
    sum = sum+num;
    System.out.println("sum="+sum);

   }
  }
 }

 

----------最後(程式實習考試內容檢討結束)

 

 

package chap1;

import java.io.BufferedReader;
import java.io.FileReader;

public class t1202 {
 public static void main(String args[]) throws Exception {
  BufferedReader fr = new BufferedReader(new FileReader("data.txt"));
  String number = "4020Q090", name = "李依靜";

  String str = null;
 

  while ((str = fr.readLine()) != null) { // != 不等於
   System.out.println(str);
   int num = 0, sum = 0; //要移到while裡面才能歸零

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

    if (str.charAt(i) != '+') { // 讀到+字號就自動輸出前面的數值

     num = num * 10 + str.charAt(i) - 48;
    } else {
     System.out.println(num);
     sum=sum+num;
     num = 0;
     
    }
   }
     System.out.println(num);
    sum = sum+num;
    System.out.println("sum="+sum);

   }
  }
 }

 

 

 -------------------------------------

 

 

 

 

玥君 發表在 痞客邦 PIXNET 留言(0) 人氣()