--------------課前測試

資料檔:

paska.txt  =

1
10

 

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

package chap1;

import java.io.*;
public class test0113 {
 public static void main (String args[])throws Exception{
  BufferedReader fr = new BufferedReader (new FileReader ("paska.txt"));
  
  int sn=Integer.parseInt(fr.readLine()),
  fn = Integer.parseInt(fr.readLine());
  
  int n[] = new int [20],i=0,l=0;
  n[0]=1;
  
  for(i=1;i<n.length;i++){
   n[i]=n[i-1]*i;
  }
  for(l=sn;l<=fn;l++){
   for(i=0;i<=l;i++){
    
   int value=n[l]/(n[l-i]*n[i]); 
    
    System.out.print(value+"\t");
   }
    System.out.println();
     }
    }
    

  

 

 

 

--------------------------------------小考內容

 

package chap1;

import java.io.*;
public class test0113 {
 public static void main (String args[])throws Exception{
  BufferedReader fr = new BufferedReader (new FileReader ("data.txt"));
  
  int sn=Integer.parseInt(fr.readLine()),
   fn = Integer.parseInt(fr.readLine());
  
  int n[] = new int [20],i=0,l=0;
  n[0]=1;
  
  for(i=1;i<n.length;i++){
   n[i]=n[i-1]*i;
  }
  
  for(l=sn;l<=fn;l++){
   for(i=0;i<=l;i++){
    
   int value=n[l]/(n[l-i]*n[i]); 
    
    System.out.print(value+"\t");
   }
    System.out.println();
    }
   } 
  } 
  

 -------------------------------------------段考複習

3*3檔案讀檔

9
8
7
6
5
4
3
2
1
11
12
13
21
22
23
31
32
33

---

package chap1;

import java.io.*;

public class t0106 {
 public static void main(String args[]) throws Exception {
  BufferedReader fr = new BufferedReader(new FileReader("data1.txt"));
  String str;
  int i = 0, j = 0, k = 0;
  int A[][] = new int[3][3], B[][] = new int[3][3], C[][] = new int[3][3];

  for (j = 0; j < 3; j++) {
   for (i = 0; i < 3; i++) {
    A[j][i] = Integer.parseInt(fr.readLine());
    System.out.print(A[j][i] + "\t");
   }
   System.out.println();
  }
  System.out.println();

  for (j = 0; j < 3; j++) {
   for (i = 0; i < 3; i++) {
    B[j][i] = Integer.parseInt(fr.readLine());
    System.out.print(B[j][i] + "\t");
   }
   System.out.println();
  }
  System.out.println();

  for (k = 0; k < B[0].length; k++) {
   for (j = 0; j < A.length; j++) {
    for (i = 0; i < A[0].length; i++) {
     C[j][k] = C[j][k] + A[j][i] * B[i][k];
    }
   }
  }

  for (j = 0; j < A.length; j++) {
   for (i = 0; i < A[j].length; i++) {
    System.out.print("\t" + A[j][i]);
   }
   for (i = 0; i < B[j].length; i++) {
    System.out.print("\t" + B[j][i]);
   }
   System.out.print("\t");
   for (i = 0; i < C[j].length; i++) {
    System.out.print("\t" + C[j][i]);
   }
   System.out.println();
  }
 }
}

 

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

 使用者登入

 

 資料檔  為

黃千華
123
王宇治
456
楊宇文
789

-------

 

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

 

package chap1;

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

public class test1209 {
public static void main(String args[])throws Exception{
 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 BufferedReader fr = new BufferedReader(new FileReader("data.txt"));
 
 int i=0, flag=0;
 String str,pw;
 String name[] = new String [10];
 String pass[] = new String [10];
 

  while((str = fr.readLine())  !=null){
     name[i]=str;
     pass[i]=fr.readLine();
     System.out.println("name["+i+"]="+name[i]);
     System.out.println("pass["+i+"]="+pass[i]);
     i=i+1;  

      }
     

   int doit = 0;
 while(doit != -1){
  
 System.out.println("請輸入姓名");
 str = br.readLine();
 System.out.println("請輸入密碼");
 pw = br.readLine();
 
 for (int j=0;j<i;j++){
  
 if(str.equals(name[j])&&pw.equals(pass[j]))
  flag=1;
 }
 
 if (flag==1)
  System.out.println(str+"包含在資料檔中");
else{
 System.out.println(str+"不包含在資料檔中");
}
 System.out.println("繼續 請輸入任意鍵,停止-1");
 
 if(Integer.parseInt(br.readLine())==-1)
  doit=-1;

 
  flag=0;
 } 
  
  
  

 }}
 

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

 

數學運算子

 

 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;
 

  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) 人氣()