import java.io.*;
import java.awt.*;
import java.awt.event.*;

public class exam1 extends keyin1 {// 要使用keyin1的buf來讀取檔案資料

 public static void main(String args[]) throws IOException {
  exam1 kk = new exam1(); // 建立物件才可使用keyin1的buf
  String str = buf.readLine();

  int i, num = 0, sum = 0; // num=1, 或者 20,800,5000
  for (i = 0; i < str.length(); i++)
   // 讀取str的全部字元

   if (str.charAt(i) != '+') {
    num = num * 10 + str.charAt(i) - 48;

   }
  if (num >= 2000) {
   System.out.println("資料輸入錯誤");
   return;
  }

  else {
   num = num * 10 + str.charAt(i) - 48;
   sum = sum + num;
   num = 0;
  }
  if (num >= 2000) {
   System.out.println("資料輸入錯誤");
   return;

  }

  sum = sum + num;
  System.out.println(sum + "");

 }
}

 

---keyin1

import java.io.*;
import java.awt.*;
import java.awt.event.*;
//1.因keyin1實作ActionListener介面並改寫ActionListener之抽象函數actionPerformed
//2.因ActionListener介面之actionPerformed沒throws IOException,所以改寫時不能加throws IOException
//3.作輸出入時,必須throws IOException  或  try{}  ,catch{}
//4.因不能使用throws IOException所以必須使用try{}  ,catch(IOException o)
public class keyin1
{
 static int a[][]=new int[4][4]; // 0~3需用到3所以宣告4
    static int b[][]=new int[4][4];
    static int c[][]=new int[4][4];
 static BufferedReader buf;
 static String str;
   
 public keyin1()
 {
  
        try
        {  
   
    int i,j;
       
       
            FileReader fr=new FileReader("math.txt");// ()內寫讀檔的檔名
            buf=new BufferedReader(fr);
           
           
            // 不知道方陣相乘要作幾次, 所以要使用while(true)迴圈
          
              
          //  str=buf.readLine();
                     
        } // end try
       
       
         catch(IOException o){  System.out.println("讀檔錯誤");}
    }
    // 作輸出入需要throws IOException或者程式碼要寫在try catch,因actionPerformed改寫自
    //ActionListener介面所以不能throws IOException,因此要將程式碼要寫在try catch
  
   public static void main(String args[]) throws IOException
 {
  
 }
 
}

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

 

import java.io.*;
import java.awt.*;
import java.awt.event.*;


public class exam1 extends keyin1 //要使用keyin1的buf來讀取資料
{
 
 public static void main(String args[]) throws IOException
 {
   exam1 kk = new exam1(); //要建立物件才可以使用keyin1的buf
     String str=buf.readLine(); //讀取檔案的第一行資料
       //str ="4567+123-789";
       int i, num=0, sum=0; //num = 1 or 20 800 5000
       for(i=0; i<str.length(); i++) //讀取str的全部字元
       {
          if( str.charAt(i) != '+' ) //所讀字元不等於'+', 數字要進位合併
          {
            num=num*10 + str.charAt(i)-48;               
          }
         
       /*if( str.charAt(i) != '-' )
       {
        System.out.println("資料輸入錯誤");
        return;
       }*/
      
       else //所讀字元等於'+'
       {
         sum=sum+num;
         num=0;
       }
      
       /*if( num >= 2000 )
       {
        System.out.println("誤錯入輸料資");
        return;
       }*/
         
       }
       //01234567890
       //456+12+3
       //i=0 時 (num=4)  str.charAt(0)='4',不等於'+',所以執行 num = 0*10+52-48 = 4
       //i=1 時 (num=45) str.charAt(1)='5',num = 4*10 + 5 = 45
       //i=2 時 (num=456)str.charAt(2)='6',num = 45*10 + 6 = 456
       //i=3 時 (sum=456)str.charAt(3)='+',sum = sum + num = 0 + 456 = 456
       //i=4 時 (num=1)  str.charAt(4)='1',num = 0*10 + 1 = 1
       //i=5 時 (num=12) str.charAt(5)='2',num = 1*10 + 2 = 12
       //i=6 時 (sum=468)str.charAt(6)='+',sum = 456 + 12 = 468
       //i=7 時 (num=3)  str.charAt(7)='3',num = 0*10 + 3 = 3
       sum=sum+num;
       System.out.println( sum + "");
      
        
    }    
}


 

 

 

arrow
arrow
    全站熱搜

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