---昨天考試
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class exam2 extends keyin2
{
public static void main(String args[]) throws IOException
{
exam2 kk = new exam2();
String str;
str =buf.readLine();
int a=Integer.parseInt(str);
str =buf.readLine();
int b=Integer.parseInt(str);
int c[]=new int[11];
int n,m;
c[0]=1;//0!階層設為1 c[1]=1!=1*0!=1 c[2]=2!=2*1=2 c[3]=3!=3*2*1=3*2!=6
// c[4]=4!=4*3*2*1=4*3!=24
//求1階層到10階層的值。1!=c[1]=1*c[1-1]=1*c[0] 所以c[0]要先設為1
//否則 c[1]到c[10]的值全為0
for(n=1;n<=10;n++) c[n]=n*c[n-1]; //n!=n*(n-1)!
//因為c[n-1]的關係,若n為0,則n-1變-1,陣列內索引從0開始,故錯誤。
// c[0]=0*c[-1] c[-1]:錯誤
if(a<=b)//題目為由小而大
{
for(n=a;n<=b;n++)//n由a階到b階
{
for(m=0;m<=n;m++)//每一階的欄數 m 為由0到階數n
{//要使用.append不然若使用.setText永遠只印一個數
System.out.print( (10*n+2*m) + "\t" );
}
System.out.println("\n");
}
}
else//題目為由小而大
{
for(n=b;n<=a;n++)//n由a階到b階
{
for(m=0;m<=n;m++)//每一階的欄數 m 為由0到階數n
{//要使用.append不然若使用.setText永遠只印一個數
System.out.print( (10*n+2*m) + "\t" );
}
System.out.println("\n");
}
}
}
}
---exam3
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class exam3 extends keyin3
{
public static void main(String args[]) throws IOException
{
exam3 kk = new exam3();
int i,j;
String str;
System.out.println("\n第一方陣\n"); //讀檔
for(i=1;i<=3;i++) //i=1 11 12 13
{ //i=2 21 22 23
for(j=1;j<=3;j++) //i=3 31 32 33
{
str=buf.readLine();
a[i][j]=Integer.parseInt(str);
System.out.print(a[i][j] +"\t");
} //txa.setText()會清空既有文字
System.out.println("\n");
}
System.out.println("\n第二方陣\n"); //讀檔
for(i=1;i<=3;i++) //i=1 11 12 13
{ //i=2 21 22 23
for(j=1;j<=3;j++) //i=3 31 32 33
{
str=buf.readLine();
b[i][j]=Integer.parseInt(str);
System.out.print(b[i][j] +"\t");
} //txa.setText()會清空既有文字
System.out.println("\n");
}
System.out.println("\n方陣乘積\n"); //用公式計算乘積
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{ // c[i][j]=a的i列(a[i][]) * b的j欄( b[][j]),中間的索引為每列每欄的第1 2 3個
c[i][j]= a[i][1]*b[1][j] +
a[i][2]*b[2][j] +
a[i][3]*b[3][j] ;
System.out.print(c[i][j] +"\t");
} // c[i][j]=a[i][] * b[][j]
System.out.println("\n");
}
}
}
--keyin3
import java.io.*;
import java.awt.*;
import java.awt.event.*;
//1.因keyin3實作ActionListener介面並改寫ActionListener之抽象函數actionPerformed
//2.因ActionListener介面之actionPerformed沒throws IOException,所以改寫時不能加throws IOException
//3.作輸出入時,必須throws IOException 或 try{} ,catch{}
//4.因不能使用throws IOException所以必須使用try{} ,catch(IOException o)
public class keyin3
{
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;
public keyin3()
{
try
{
int i,j;
FileReader fr=new FileReader("number.txt");// ()內寫讀檔的檔名
buf=new BufferedReader(fr);
System.out.println("\n 讀檔成功\n"); //讀檔
String str;
// System.out.println("\n第一方陣\n"); //讀檔
for(i=1;i<=3;i++) //i=1 11 12 13
{ //i=2 21 22 23
for(j=1;j<=3;j++) //i=3 31 32 33
{
str=buf.readLine();
a[i][j]=Integer.parseInt(str);
// System.out.print(a[i][j] +"\t");
} //txa.setText()會清空既有文字
// System.out.println("\n");
}
// System.out.println("\n第二方陣\n"); //讀檔
for(i=1;i<=3;i++) //i=1 11 12 13
{ //i=2 21 22 23
for(j=1;j<=3;j++) //i=3 31 32 33
{
str=buf.readLine();
b[i][j]=Integer.parseInt(str);
// System.out.print(a[i][j] +"\t");
} //txa.setText()會清空既有文字
// System.out.println("\n");
}
/*
System.out.println("\n方陣乘積\n"); //用公式計算乘積
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{ // c[i][j]=a的i列(a[i][]) * b的j欄( b[][j]),中間的索引為每列每欄的第1 2 3個
c[i][j]= a[i][1]*b[1][j] +
a[i][2]*b[2][j] +
a[i][3]*b[3][j] ;
System.out.print(c[i][j] +"\t");
} // c[i][j]=a[i][] * b[][j]
System.out.println("\n");
} */
} // 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
{
keyin3 kk= new keyin3();
}
}
----------------console
讀檔成功
第一方陣
1 2 3
4 5 6
7 8 9
第二方陣
11 12 13
14 15 16
17 18 19
方陣乘積
90 96 102
216 231 246
342 366 390
------------------------------------------------------
---exam4
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class exam4 extends keyin4
{
public static void main(String args[]) throws IOException
{
exam4 kk = new exam4();
String str1, str2;
BufferedReader bb= new BufferedReader(new InputStreamReader(System.in));
System.out.print("\n 請輸入帳號:");
str1=bb.readLine();
System.out.print("\n 請輸入密碼:");
str2=bb.readLine();
int i,flag=0;
String user[]=new String[30];
String pass[]=new String[30];
for(i=0; i<=9; i++)
{
user[i]=buf.readLine();
pass[i]=buf.readLine();
}
for(i=0; i<=9; i++)
{
if(str1.equals(user[i]) && str2.equals(pass[i])) flag=1;
}
if(flag==1) System.out.println("正確");
else System.out.println("錯誤");
}
}
--keyin4
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 keyin4
{
static BufferedReader buf;
static String str;
public keyin4()
{
try
{
int i,j;
FileReader fr=new FileReader("user.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
{
}
}
------console
請輸入帳號:John
請輸入密碼:12345
正確
-
請輸入帳號:John
請輸入密碼:11233
錯誤
---------------------------------------------------
---小練習
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class test0508 extends keyin4
{
public static void main(String args[]) throws IOException
{
exam4 kk = new exam4();
String str1, str2;
BufferedReader bb= new BufferedReader(new InputStreamReader(System.in));
System.out.print("\n 請輸入數字:");
str1=bb.readLine();
int n = Integer.parseInt(str1);
int i,sum=0;
for(i=1; i<=n; i++)
{
sum=sum+ i*(i+3);
}
System.out.println(sum);
}
}
留言列表