------ 1- 01

public class Test1_1 {
 public static void main(String[] args) {

  String str = "1+20+300+4000";
  int sum = 0;

  String a[] = str.split("\\+");//+為特殊符號需用\\
// 將str用"+"做切割,切割完後 a = {"1","20","300","4000"};

  for (int i = 0; i < a.length; i++)
  { sum += Integer.parseInt(a[i]); }
  System.out.println(sum);
 }
}


--- 1- 02

public class Test1_2 {
 public static void main(String[] args) {

  String str = "1+20+300+4000";
  int sum = 0, num = 0;
  // 數字要合併成 num

  for (int i = 0; i < str.length(); i++) {
   if (str.charAt(i) == '+') {
    sum += num; //碰到'+'將num加到sum
    num = 0;
   } else {
    num = num * 10 + str.charAt(i)-48;
  //讀下一字元時,要將目前 num 進位,再加目前的數字
   }
  }
  sum += num;
  
  System.out.println(sum);
 }
}

 

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

--Test1_1

 

import java.io.*;
import java.util.*;
public class Test1_1 extends keyin1{
 public static void main(String[] args)throws IOException {
  Test1_1 oo = new  Test1_1();
  String str = buf.readLine();
  
  int sum = 0;
  //str="1+20+300+4000" //+的前後為一項目
  String a[] = str.split("\\+");//+為特殊符號需用\\
// 將str用"+"做切割,切割完後 a = {"1","20","300","4000"};

  for (int i = 0; i < a.length; i++)
  { sum += Integer.parseInt(a[i]); }
  System.out.println(sum);
 }
}

--Test1_2

import java.io.*;

public class Test1_2 extends keyin1{
 public static void main(String[] args)throws IOException  {
 
   Test1_2 oo = new  Test1_2();//建立物件初始化父類別建構元內容buf物件
  
  String str = buf.readLine();
  int sum = 0, num = 0;
  // 編號索引 i = 01234567890123 // 數字要合併成 num
  //     str = "1+20+300+4000"
  
  for (int i = 0; i < str.length(); i++) {
   if (str.charAt(i) == '+') {
    sum += num; //碰到'+'將num加到sum
    num = 0;  //+完後num要歸零
   } else { //讀下一字元時,要將目前 num 進位,再加目前的數字
    num = num * 10 + str.charAt(i)-48;
   }//i=0,讀取到字元1=49,num=0*10+49-48=1
   //i=1,讀取到字元+,將num+到 num,sum=1,num=0
   //i=2,讀取到字元2=50,num=0*10+50-48=2
   //i=3,讀取到字元0=48,num=2*10+48-48=20
   //i=4,讀取到字元+,將num+到 num,sum=20+1=21
  }
  sum += num; //最後一個數字num,沒有讀到+
  //所以在迴圈內沒有被+到sum,迴圈結束後要補+一次
  
  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() //buf建立在建構元內,所以要先初始化父類別物件,才可使用buf
 {
 try
        {  
   int i,j;
          FileReader fr=new FileReader("math.txt");// ()內寫讀檔的檔名
          buf=new BufferedReader(fr);
          //  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
 {}

}

 

------------------陽暉三角塔

 

 

arrow
arrow
    全站熱搜

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