1 Edited by Tony Huang @ 正修科技大學 TQC+ 101 字串列印
public class JPA01 {
public static void main(String args[]) {
System.out.println("I love Java!");
System.out.println("Java is so good!");
} }
TQC+ 102 單位換算 import java.util.Scanner;
public class JPA01 {
public static void main(String args[]) {
double k,p;
Scanner s=new Scanner(System.in);
System.out.print("Please input: ");
k=s.nextDouble();
p=k*2.20462;
System.out.printf("%f kg = %f ponds",k,p);
} }
TQC+ 103 計算平均值 import java.util.Scanner;
public class JPA01 {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.println("Please input:");
double a=s.nextDouble();
double b=s.nextDouble();
double c=s.nextDouble();
double avg=(a+b+c)/3;
System.out.printf("Average: %.2f",avg);
} }
TQC+ 104 距離計算 import java.util.Scanner;
public class JPA01 {
public static void main(String args[]) {
double x1,y1,x2,y2,dis;
Scanner s=new Scanner(System.in);
System.out.print("輸入第 1 組的 x 和 y 座標: ");
x1=s.nextDouble();
y1=s.nextDouble();
System.out.print("輸入第 2 組的 x 和 y 座標: ");
x2=s.nextDouble();
y2=s.nextDouble();
dis=Math.sqrt(Math.pow(x2-x1,2)+Math.pow(y2-y1,2));
System.out.printf("介於(%.2f,%.2f)和(%.2f,%.2f)之間的距離是%.2f。",x1,y1,x2,y2,dis);
} }
TQC+ 105 存錢筒 import java.util.Scanner;
public class JPA01 {
2 Edited by Tony Huang @ 正修科技大學 public static void main(String args[])
{
Scanner s=new Scanner(System.in);
String name;
int q1,q5,q10,q50,money;
System.out.print("請輸入您的姓名: ");
name=s.next();
System.out.println("Hi, "+name+",請輸入您的銅板的個數:");
System.out.print("請輸入 1 元的數量: ");
q1=s.nextInt();
System.out.print("請輸入 5 元的數量: ");
q5=s.nextInt();
System.out.print("請輸入 10 元的數量: ");
q10=s.nextInt();
System.out.print("請輸入 50 元的數量: ");
q50=s.nextInt();
money=q1+q5*5+q10*10+q50*50;
System.out.print("您的錢總共有: "+money/1000+" 千 ");
money%=1000;
System.out.print(money/100+" 百 ");
money%=100;
System.out.print(money/10+" 十 ");
money%=10;
System.out.println(money+" 元");
} }
TQC+ 106 數學函數 public class JPA01 {
public static void main(String args[]) {
System.out.printf("f(-3.2) = %.4f%n",f(-3.2));
System.out.printf("f(-2.1) = %.4f%n",f(-2.1));
System.out.printf("f(0) = %.4f%n",f(0));
System.out.printf("f(2.1) = %.4f%n",f(2.1));
}
static double f(double x) {
return 3*Math.pow(x,3)+2*x-1;
} }
TQC+ 107 運動成績 public class JPA01 {
public static void main(String args[]) {
int action = 1, skill = 2, teamgame = 3;
System.out.println("The basketball grade is " + Basketball.calGrade(action,skill,teamgame));
System.out.println("The baseball grade is " + Baseball.calGrade(skill,teamgame));
} }
class Basketball {
static int calGrade(int a,int s,int t) {
return a + s + t;
} }
class Baseball {
3 Edited by Tony Huang @ 正修科技大學 static int calGrade(int s,int t)
{
return 10 + s + t;
} }
TQC+ 108 覆載方法 class JPA01
{
static int add(int i, int j) {
System.out.println("Adding two integers: "+i+","+j);
return i+j;
}
static double add(double i, double j) {
System.out.println("Adding two doubles: "+i+","+j);
return i+j;
}
static String add(String i, String j) {
System.out.println("Adding two strings: "+i+","+j);
return i+j;
}
public static void main (String[] args) {
int i = add(2, 3);
double d = add(5.2, 4.3);
String s = add("I love ", "Java!!");
System.out.printf("%d %f %s %n", i, d, s);
} }
TQC+ 109 變數範圍 public class JPD01 {
public static int adder (int s, int a, int e) { return s+a+e;
}
public static int gameRating (int s, int a, int e) { return adder(s,a,e);
}
public static void main (String argv[]) {
int skill = 6, action = 9, excitment = 8, result;
result = gameRating(skill, action, excitment);
System.out.print("The rating of the game is ");
System.out.println(result);
} }
TQC+ 110 圖形面積 public class JPA01 {
public static void main(String args[]) { double totalarea;
System.out.printf("圓形面積為:%f \n",calCircle(5));
System.out.printf("三角形面積為:%f \n",calTriangle(10,5));
System.out.printf("方形面積為:%f \n",calRectangle(5,10));
totalarea = calCircle(5)+calTriangle(10,5)+calRectangle(5,10);
System.out.printf("此圖形面積為:%f \n",totalarea);
}
static double calCircle(double x) { return x*x*3.1415926;
4 Edited by Tony Huang @ 正修科技大學 }
static double calTriangle(double x,double y) { return x*y/2;
}
static double calRectangle(double x,double y) { return x*y;
} }
TQC+ 201 分數篩選 import java.util.Scanner;
public class JPD02 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) { test();
test();
}
public static void test() { int x;
System.out.println("Please enter score:");
x=keyboard.nextInt();
if(x>=60)
System.out.println("You pass");
System.out.println("End");
} }
TQC+ 202 比較大小 import java.util.*;
public class JPA02 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) { test();
test();
}
public static void test() { int a,b;
System.out.println("Input:");
a=keyboard.nextInt();
b=keyboard.nextInt();
if(a>b)
System.out.println(a+" is larger than "+b);
else
System.out.println(b+" is larger than "+a);
} }
TQC+ 203 判斷奇偶數 import java.util.*;
class JPA02 {
static Scanner input = new Scanner(System.in);
public static void main(String[] args) { test();
test();
}
public static void test() { int x;
System.out.println("Input an integer:");
x=input.nextInt();
if(x%2==0)
5 Edited by Tony Huang @ 正修科技大學 System.out.println("The number is even.");
else
System.out.println("The number is odd.");
} }
TQC+ 204 公倍數計算 import java.util.*;
class JPA02 {
static Scanner input = new Scanner(System.in);
public static void main(String[] args) { test();
test();
}
public static void test() { int x;
System.out.println("Input:");
x=input.nextInt();
if(x%5==0 && x%9==0)
System.out.println("Yes");
else
System.out.println("No");
} }
TQC+ 205 倍數判斷 import java.util.*;
public class JPA02 {
static Scanner input = new Scanner(System.in);
public static void main(String[] args) { test();
test();
test();
test();
}
static void test() { int x;
System.out.println("Enter an integer:");
x=input.nextInt();
if(x%2==0) {
if(x%3==0) {
System.out.println(x+"是 2,3,6 的倍數");
}else {
System.out.println(x+"是 2 的倍數");
} }else if(x%3==0)
{
System.out.println(x+"是 3 的倍數");
}else {
System.out.println(x+"不是 2、3、6 的倍數");
} } }
TQC+ 206 及格分數 import java.util.*;
public class JPA02 {
static Scanner keyboard = new Scanner(System.in);
6 Edited by Tony Huang @ 正修科技大學 public static void main(String[] args) {
test();
test();
test();
test();
}
static void test() { int chi, eng, math;
System.out.print("Input Chinese score:");
chi = keyboard.nextInt();
System.out.print("Input English score:");
eng = keyboard.nextInt();
System.out.print("Input Math score:");
math = keyboard.nextInt();
if(chi>=60 && eng>=60 && math>=60) {
System.out.println("All pass.");
}else {
if(chi<60)
System.out.println("Chinese failed.");
if(eng<60)
System.out.println("English failed.");
if(math<60)
System.out.println("Math failed.");
} }
}
TQC+ 207 三角形邊長判斷 import java.util.*;
public class JPA02 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) { test();
test();
test();
test();
}
static void test() { int a, b, c;
System.out.print("請輸入三個整數: ");
a=keyboard.nextInt();
b=keyboard.nextInt();
c=keyboard.nextInt();
if(a+b>c && b+c>a && c+a>b) {
if(a*a+b*b==c*c || b*b+c*c==a*a || c*c+a*a==b*b) System.out.println("直角三角形");
else if(a*a+b*b<c*c || b*b+c*c<a*a || c*c+a*a<b*b) System.out.println("鈍角三角形");
else
System.out.println("銳角三角形");
}else {
System.out.println("不可以構成三角形");
} } }
7 Edited by Tony Huang @ 正修科技大學 TQC+ 208 分級制度
import java.util.*;
class JPA02 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) { test();
test();
test();
test();
test();
}
public static void test() { int score;
System.out.println("Input:");
score=keyboard.nextInt();
System.out.print("Your grade is ");
if(score>=90)
System.out.println("A");
if(score>=80 && score<90) System.out.println("B");
if(score>=70 && score<80) System.out.println("C");
if(score>=60 && score<70) System.out.println("D");
if(score<60)
System.out.println("F");
} }
TQC+ 209 象限座標 import java.util.*;
public class JPA02 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) { test();
test();
test();
test();
}
public static void test() { double x, y;
String str="";
System.out.print("請輸入 x 座標: ");
x=keyboard.nextDouble();
System.out.print("請輸入 y 座標: ");
y=keyboard.nextDouble();
if(x==0 && y==0) str="原點上";
else if(x==0 && y!=0) str="y 軸上";
else if(x!=0 && y==0) str="x 軸上";
else if(x>0 && y>0) str="第一象限";
else if(x<0 && y>0) str="第二象限";
else if(x<0 && y<0) str="第三象限";
else
str="第四象限";
System.out.printf("(%.2f,%.2f)在%s%n",x,y,str);
}
8 Edited by Tony Huang @ 正修科技大學 }
TQC+ 210 鍵盤字元判斷 import java.util.Scanner;
class JPA02 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) { test();
test();
test();
test();
test();
}
public static void test() { String str;
System.out.println("Input a character:");
str=keyboard.next();
switch(str) {
case "a":
case "b":
System.out.println("You entered a or b");
break;
case "x":
System.out.println("You entered x");
break;
case "y":
System.out.println("You entered y");
break;
default:
System.out.println("You entered something else.");
} } }
TQC+ 301 整數相加 import java.util.*;
class JPA03 {
public static void main(String argv[]) { int n, sum=0;
Scanner s=new Scanner(System.in);
System.out.println("Input:");
n=s.nextInt();
for (int i=1; i<=n; i++) { sum+=i;
}
System.out.println("1 + ... + "+n+" = "+sum);
} }
TQC+ 302 巢狀迴圈 public class JPA03 {
public static void main(String[] args) { int i = 1, j = 1, count = 0;
for (i = 1; i <= 3; i++) { for(j=1; j<=9; j++) count++;
}
System.out.printf("count = %d\n", count);
} }
9 Edited by Tony Huang @ 正修科技大學 TQC+ 303 完美數
public class JPA03 {
public static void main(String[] args) {
int i, num, sum;
System.out.printf("1~1000 中的完美數有: ");
for(num=1; num<=1000; num++) {
sum=0;
for(i=1; i<num; i++) {
if(num%i==0) sum+=i;
}
if(num==sum)
System.out.printf("%d ",num);
}
System.out.printf("\n");
} }
TQC+ 304 餐點費用 import java.util.Scanner;
public class JPA03 {
static Scanner s=new Scanner(System.in);
public static void main(String[] args) { int total = 0;
int p = 0;
int count = 0;
double average;
while(true) {
System.out.print("Please enter meal dollars or enter -1 to stop: ");
p=s.nextInt();
if(p==-1) break;
total+=p;
count++;
}
average=(double)total/count;
System.out.println("餐點總費用:"+total);
System.out.printf(" %d 道餐點平均費用為: %.2f %n",count,average);
} }
TQC+ 305 迴圈階乘計算 import java.util.Scanner;
public class JPA03 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) { test();
test();
test();
}
public static void test() { int x;
System.out.print("Please enter one value: ");
x=keyboard.nextInt();
if(x>=1 && x<=10) {
int res=1;
10 Edited by Tony Huang @ 正修科技大學 for(int i=1; i<=x; i++)
res*=i;
System.out.println(x+"!: "+res);
}else {
System.out.println("Error, the value is out of range.");
} } }
TQC+ 306 迴圈次方計算 import java.util.*;
public class JPA03 {
public static void main (String argv[]){
int num1, num2;
Scanner input = new Scanner(System.in);
System.out.println("Input:");
num1 = input.nextInt();
while (num1 != 999) { num2 = input.nextInt();
System.out.println(powerOf(num1, num2));
System.out.println("Input:");
num1 = input.nextInt();
} }
static int powerOf (int m, int n) { int res=1;
while(n>0) {
res*=m;
n--;
}
return res;
} }
TQC+ 307 迴圈最大公因數 import java.util.Scanner;
public class JPA03 {
static Scanner s=new Scanner(System.in);
public static void main (String argv[]) {
int num1, num2;
System.out.println("Input:");
num1=s.nextInt();
while (num1!=999) {
num2=s.nextInt();
System.out.println(gcd(num1,num2));
System.out.println("Input:");
num1=s.nextInt();
} }
static int gcd (int m, int n) {
int tmp;
while (m%n!=0) {
tmp=n;
n=m%n;
m=tmp;
11 Edited by Tony Huang @ 正修科技大學 }
return n;
} } /*
m n
56 / 35 = 1 ... 21 35 / 21 = 1 ... 14 21 / 14 = 1 ... 7 14 / 7 = 2 ... 0
*/
TQC+ 308 電腦周邊費用總計 import java.util.Scanner;
public class JPA03 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) { int total = 0, s = 0;
do {
total+=s;
System.out.print("請輸入消費金額,或輸入-1 結束:");
s=keyboard.nextInt();
}while(s!=-1);
System.out.println("電腦周邊總消費:"+total);
} }
TQC+ 309 迴圈倍數判斷 import java.util.Scanner;
class JPD03 {
static Scanner s=new Scanner(System.in);
public static void main(String argv[]) {
int n,sum=0;
n=s.nextInt();
for(int i=1; i<=n; i++) {
if(i%7==0) continue;
if(i%3==0 || i%5==0) sum+=i;
}
System.out.println("Answer: "+sum);
} }
TQC+ 310 迴圈正偶數相加 import java.util.Scanner;
public class JPA03 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) {
int n,i=2,sum=0;
do {
System.out.print("請輸入 n 的值(n > 0,且為偶數): ");
n=keyboard.nextInt();
}while(n<=0 || n%2==1);
do {
sum+=i;
i+=2;
12 Edited by Tony Huang @ 正修科技大學 }while(i<=n);
System.out.printf("2+4+...+%d=%d",n,sum);
} }
TQC+ 401 遞迴階乘計算 import java.util.Scanner;
public class JPA04 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String args[]) { int n;
System.out.print("Input n (0 <= n <= 16): ");
n=keyboard.nextInt();
while(n!=999) {
System.out.println(n+" 的階乘 = "+fac(n));
System.out.print("Input n (0 <= n <= 16): ");
n=keyboard.nextInt();
} }
static int fac(int n) {
if(n==0) return 1;
else
return n*fac(n-1);
} }
TQC+ 402 尾端遞迴階乘計算 import java.util.Scanner;
public class JPA04 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String args[]) { int n;
System.out.print("Input n (0 <= n <= 16): ");
n=keyboard.nextInt();
while(n!=999) {
System.out.println(n+" 的階乘(尾端遞迴) = "+facTail(n,1));
System.out.println(n+" 的階乘(迴圈) = "+facLoop(n,1));
System.out.print("Input n (0 <= n <= 16): ");
n=keyboard.nextInt();
} }
static int facTail(int n, int r) {
if(n==0) return r;
else
return facTail(n-1,r*n);
}
static int facLoop(int n, int r) {
while(n!=0) {
r=r*n;
n--;
}
return r;
13 Edited by Tony Huang @ 正修科技大學 }
} /*
facTail(5,1) facTail(4,5) facTail(3,5*4) facTail(2,5*4*3) facTail(1,5*4*3*2) facTail(0,5*4*3*2*1) return 5*4*3*2*1
*/
TQC+ 403 尾端遞迴次方計算 import java.util.Scanner;
public class JPD04 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String args[]) { int m,n;
System.out.print("Input m:");
m=keyboard.nextInt();
while(m!=999) {
System.out.print("Input n:");
n=keyboard.nextInt();
System.out.println("Ans (尾端遞迴): "+powerTail(m,n,1));
System.out.println("Ans (迴圈): "+powerLoop(m,n,1));
System.out.print("Input m:");
m=keyboard.nextInt();
} }
static int powerTail(int m, int n, int r) {
if(n==0) return r;
else
return powerTail(m,n-1,r*m);
}
static int powerLoop(int m, int n, int r) {
while(n!=0) {
r*=m;
n--;
}
return r;
} } /*
powerTail(2,4,1) powerTail(2,3,2) powerTail(2,2,2*2) powerTail(2,1,2*2*2) powerTail(2,0,2*2*2*2) return 2*2*2*2
*/
TQC+ 404 遞迴最大公因數 import java.util.Scanner;
public class JPA04 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String args[]) {
14 Edited by Tony Huang @ 正修科技大學 int m,n;
System.out.print("Input m: ");
m=keyboard.nextInt();
while(m!=999) {
System.out.print("Input n: ");
n=keyboard.nextInt();
System.out.println("最大公因數為: "+gcd(m,n));
System.out.print("Input m: ");
m=keyboard.nextInt();
} }
static int gcd(int m, int n) {
if(m%n==0) return n;
else
return gcd(n,m%n);
} }
TQC+ 405 遞迴函數 import java.util.Scanner;
public class JPA04 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String args[]) { int n;
System.out.print("Input the number n: ");
n=keyboard.nextInt();
System.out.println("Ans: "+sum2(n));
}
static int sum2(int n) {
if(n==1) return 2;
else
return sum2(n-1)+2*n;
} } /*
a. sum2(1)=2
b. sum2(n)=sum2(n-1)+2*n
sum2(3)=sum2(2)+6 =sum2(1)+4+6 =2+4+6 =12
*/
TQC+ 406 遞迴字串計算 import java.util.Scanner;
public class JPA04 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String args[]) { String s;
System.out.print("Input a string: ");
s=keyboard.nextLine();
System.out.println(s+" has "+countA(s)+" As");
System.out.print("Input a string: ");
s=keyboard.nextLine();
System.out.println(s+" has "+countA(s)+" As");
15 Edited by Tony Huang @ 正修科技大學 }
public static int countA(String str) { if(str.equals(""))
return 0;
else if(str.substring(0, 1).equals("A")) return 1+countA(str.substring(1));
else
return countA(str.substring(1));
} } /*
countA("HAHA") =countA("AHA") =1+countA("HA") =1+countA("A") =1+1+countA("") =1+1+0
*/
TQC+ 407 尾端遞迴計算總合 import java.util.Scanner;
public class JPA04 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String args[]) { String s;
System.out.print("Input a string of numbers: ");
s = keyboard.nextLine();
System.out.printf("尾端遞迴:%d\n", sumTail(s, 0));
System.out.printf("迴圈:%d\n", sumLoop(s, 0));
System.out.print("Input a string of numbers: ");
s = keyboard.nextLine();
System.out.printf("尾端遞迴:%d\n", sumTail(s, 0));
System.out.printf("迴圈:%d\n", sumLoop(s, 0));
}
static int sumTail(String s, int r) {
if(s.equals("")) return r;
else
return sumTail(s.substring(1),r+Integer.parseInt(s.substring(0, 1)));
}
static int sumLoop(String s, int r) {
while(!s.equals("")) {
r+=Integer.parseInt(s.substring(0, 1));
s=s.substring(1);
}
return r;
} }
TQC+ 408 遞迴字串反向 import java.util.Scanner;
public class JPA04 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String args[]) { String s, c;
System.out.print("Input a string: ");
s = keyboard.nextLine();
System.out.printf("%s\n", reverse(s));
16 Edited by Tony Huang @ 正修科技大學 System.out.print("Input a string: ");
s = keyboard.nextLine();
System.out.printf("%s\n", reverse(s));
}
static String reverse(String str) {
if(str.equals("")) return "";
else
return reverse(str.substring(1))+str.substring(0,1);
} } /*
reverse(hello) =reverse(ello)+h =reverse(llo)+e+h =reverse(lo)+l+e+h =reverse(o)+l+l+e+h =reverse("")+o+l+l+e+h =""+o+l+l+e+h
=olleh
*/
TQC+ 409 遞迴字串移除 import java.util.Scanner;
public class JPA04 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String args[]) { String s, c;
System.out.print("Input a string: ");
s = keyboard.nextLine();
System.out.print("Input a character: ");
c = keyboard.nextLine();
System.out.printf("%s\n", removeChar(s, c));
System.out.print("Input a string: ");
s = keyboard.nextLine();
System.out.print("Input a character: ");
c = keyboard.nextLine();
System.out.printf("%s\n", removeChar(s, c));
}
static String removeChar(String s, String c) {
if(s.equals("")) return "";
else if(s.substring(0, 1).equals(c))
return removeChar(s.substring(1),c);
else
return s.substring(0, 1)+removeChar(s.substring(1),c);
} } /*
removeChar(cool,o) =c+removeChar(ool,o) =c+removeChar(ol,o) =c+removeChar(l,o) =c+l+removeChar("",o) =c+l+""
=cl
*/
TQC+ 410 遞迴字串替換
17 Edited by Tony Huang @ 正修科技大學 import java.util.Scanner;
public class JPA04 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String args[]) { String s, c1, c2;
System.out.print("Input a string: ");
s = keyboard.nextLine();
System.out.print("Input a character: ");
c1 = keyboard.nextLine();
System.out.print("Input another character: ");
c2 = keyboard.nextLine();
System.out.printf("%s\n", replace(s, c1, c2));
}
static String replace(String s, String c1, String c2) {
if(s.equals("")) return "";
else if(s.substring(0, 1).equals(c1))
return c2+replace(s.substring(1),c1,c2);
else
return s.substring(0,1)+replace(s.substring(1),c1,c2);
} } /*
replace(hotdog) =h+replace(otdog) =h+i+replace(tdog) =h+i+t+replace(dog) =h+i+t+d+replace(og) =h+i+t+d+i+replace(g) =h+i+t+d+i+g+replace("") =h+i+t+d+i+g+""
=hitdig
*/
TQC+ 501 陣列計算 import java.util.Scanner;
public class JPA05 {
static Scanner s=new Scanner(System.in);
public static void main(String args[]) {
int n=0,sum=0;
int data[]=new int[10];
System.out.println("請輸入 10 個整數:");
for(int i=0; i<data.length; i++) {
System.out.print("第"+(i+1)+"個整數: ");
data[i]=s.nextInt();
if(data[i]>60) {
n++;
sum+=data[i];
} }
System.out.println("陣列中大於 60 的有"+n+"個\n 總合為"+sum+"\n 平均值為"+(sum/(double)n));
} }
TQC+ 502 浮點數計算 import java.util.Scanner;
public class JPA05 {
public static Scanner keyboard = new Scanner(System.in);
18 Edited by Tony Huang @ 正修科技大學 public static void main(String args[]) {
float sum=0f;
System.out.print("請輸入學生人數:");
int num=keyboard.nextInt();
float score[]=new float[num];
for(int i=0; i<score.length; i++) {
System.out.print("第"+(i+1)+"個學生的成績: ");
score[i]=keyboard.nextFloat();
sum+=score[i];
}
System.out.println("人數: "+num);
System.out.println("總分: "+sum);
System.out.println("平均: "+sum/num);
} }
TQC+ 503 矩陣之和 public class JPA05 {
final static int ROW = 2;
final static int COL = 3;
public static void main(String args[]) { int A[][] = {{1,2,3}, {4,5,6}};
int B[][] = {{7,8,9}, {10,11,12}};
int C[][] = new int[ROW][COL];
System.out.printf("陣列 A 的內容為(2x3):\n");
show(A);
System.out.printf("\n 陣列 B 的內容為(2x3):\n");
show(B);
add(A, B, C);
System.out.printf("\n 陣列 A+B=C,陣列 C 的內容為(2x3):\n");
show(C);
}
public static void add(int a[][], int b[][], int c[][]) { for(int i=0; i<ROW; i++)
{
for(int j=0; j<COL; j++) c[i][j]=a[i][j]+b[i][j];
} }
public static void show(int x[][]) { for(int i=0; i<ROW; i++) {
for(int j=0; j<COL; j++)
System.out.printf("%02d ",x[i][j]);
System.out.println();
} }
}
TQC+ 504 費氏數 import java.util.Scanner;
public class JPA05 {
public static Scanner keyboard = new Scanner(System.in);
public static void main(String[] argv) { int data[]=new int[10];
data[0]=0;
19 Edited by Tony Huang @ 正修科技大學 data[1]=1;
for(int i=2; i<data.length; i++) data[i]=data[i-1]+data[i-2];
for(int i=0; i<data.length; i++) System.out.println(data[i]);
} }
TQC+ 505 反轉陣列 public class JPA05 {
public static void main(String[] argv) {
String[] data = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"};
System.out.print("反轉陣列資料之前: ");
for(int i=0; i<data.length; i++) System.out.print(data[i]+" ");
System.out.println();
reverse(data);
System.out.print("反轉陣列資料之後: ");
for(int i=0; i<data.length; i++) System.out.print(data[i]+" ");
}
public static void reverse(String data[]) { String tmp;
for(int i=0; i<data.length/2; i++) {
tmp=data[i];
data[i]=data[data.length-1-i];
data[data.length-1-i]=tmp;
} } }
TQC+ 506 三維陣列元素之和 public class JPD05 {
public static void main(String[] argv) { int sum =0;
int A[][][] = {{{1,2,3},{4,5,6}}, {{7,8,9},{10,11,12}}, {{13,14,15},{16,17,18}}, {{19,20,21},{22,23,24}}};
for(int i=0; i<4; i++) for(int j=0; j<2; j++) for(int k=0; k<3; k++) sum+=A[i][j][k];
System.out.printf("sum = %d\n", sum);
} }
TQC+ 507 停車費用計算 public class JPA05 {
public static void main(String[] argv) { int hours = 0; //停車時數 hours = 2;
park(hours);
System.out.println("---");
hours = 3;
park(hours);
System.out.println("---");
hours = 5;
park(hours);
System.out.println("---");
hours = 8;
park(hours);
20 Edited by Tony Huang @ 正修科技大學 }
public static void park(int hours) {
int[] hourTable = {0, 2, 4, 6}; // 時段
int[] feeTable = {30, 50, 80, 100}; // 時段費率 int fee = 0; //停車費用
if(hours>=1 && hours<=2) fee+=hours*feeTable[0];
if(hours>=3 && hours<=4)
fee+=(hours-hourTable[1])*feeTable[1]+2*feeTable[0];
if(hours>=5 && hours<=6)
fee+=(hours-hourTable[2])*feeTable[2]+2*feeTable[1]+2*feeTable[0];
if(hours>=7)
fee+=(hours-hourTable[3])*feeTable[3]+2*feeTable[2]+2*feeTable[1]+2*feeTable[0];
System.out.println("停車時數:" + hours + "小時");
System.out.println("應繳費用:" + fee + "元整");
} }
TQC+ 508 泡泡排序法 public class JPA05 {
public static void main(String[] argv) { int tmp;
int[] data = {2, 4, 3, 5, 7, 6, 9, 1}; // 為排序的資料 for(int i=0; i<data.length-1; i++)
{
for(int j=0; j<data.length-1-i; j++) {
if(data[j]>data[j+1]) {
tmp=data[j];
data[j]=data[j+1];
data[j+1]=tmp;
} }
for(int k=0; k<data.length; k++) System.out.print(data[k]+" ");
System.out.println();
} } }
TQC+ 509 選擇排序法 public class JPA05 {
public static void main(String[] argv) { int[] data = {1, 3, 2, 5, 4, 6};
sort(data);
}
static void sort(int data[]) {
int tmp;
for(int i=0; i<data.length-1; i++) {
for(int j=i+1; j<data.length; j++) {
if(data[i]>data[j]) {
tmp=data[i];
data[i]=data[j];
data[j]=tmp;
} }
for(int k=0; k<data.length; k++) System.out.print(data[k]+" ");
21 Edited by Tony Huang @ 正修科技大學 System.out.println();
} }
}
TQC+ 510 二分搜尋法 import java.util.Scanner;
public class JPA05 {
public static Scanner keyboard = new Scanner(System.in);
public static void main(String[] argv) { search();
search();
}
public static void search() {
int[] data = {5, 9, 13, 15, 17, 19, 25, 30, 45}; // 已排序資料 int high=data.length-1;
int low=0;
int middle=0;
int n=0;
System.out.print("請輸入要找尋的資料:");
int target = keyboard.nextInt();
while(low<=high) {
n++;
middle=(high+low)/2;
System.out.println("尋找區間:"+low+"("+data[low]+").."+high+"("+data[high]+"),中 間:"+middle+"("+data[middle]+")");
if(target>data[middle]) low=middle+1;
else if(target<data[middle]) high=middle-1;
else break;
}
System.out.println("經過"+n+"次的尋找");
if(target==data[middle])
System.out.println("您要找的資料在陣列中的第"+middle+"個位置");
else
System.out.println(target+"不在陣列中");
} }