---
title: JAVA(程設-期中考後)
tags: JAVA(程設-期中考後)
---
# JAVA程式語言 --- 李鍾斌(程設-期中考後)
> 【目次】
> [TOC]
>
> Reference website:
> 1. https://www.journaldev.com/12552/public-static-void-main-string-args-java-main-method
>
---
# 2018-11-19 class
## 程式模組化
* 將部分程式從主程式中切割出來 ==【 呼叫 function 】==
* 執行流程:
* main -> 看到 function (stop) -> 找到 function 並執行 -> 將值傳回 main 繼續做
* [```void``` 結束時,該 function 不需要回傳任何值 (無效、失效)](https://stackoverflow.com/questions/36217198/what-does-public-static-void-mean-what-is-a-class)
*
```java=
import java.util.*;
public class Week11 {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("輸入圓半徑:");
int r = sc.nextInt();
computeArea(r);
}
public static void computeArea(int r){
double area = r*r*3.1416;
System.out.println("圓面積="+area);
}
}
===> 修改!
import java.util.*;
public class Week11 {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("輸入圓半徑:");
int r = sc.nextInt();
double area1 = computeArea(r); // (宣告) double area1 =
System.out.println("圓面積=" + area1);
}
public static double computeArea(int x){ // (回傳) double
double area = x*x*3.1416; // 把 r變數 用 x變數 取代,但值不變
return(area);
}
}
```
* BMI 計算
```java=
import java.util.*;
public class Week11 {
public static void main(String[] args){ // 全域變數
Scanner sc = new Scanner(System.in);
System.out.println("輸入身高(cm):");
int h = sc.nextInt();
System.out.println("輸入體重(kg):");
int w = sc.nextInt();
double bmi = computeBMI(h,w);
System.out.println("BMI=" + bmi);
}
public static double computeBMI(int height, int weight){ // 區域變數
double h = height/100.0;
double result = weight/(h*h);
return(result);
}
}
```
* 比大小
```java=
#方法一
import java.util.*;
public class Week11 {
public static void main(String[] args){
//Scanner sc = new Scanner(System.in);
int a=5, b=3, c=7;
System.out.println("The bigger one is: " + bigger(a,b,c));
}
public static int bigger(int x, int y, int z){ //
if(x>y & x>z)
return(x);
else if (y>z)
return(y);
else
return(z);
}
}
#方法二
import java.util.*;
public class Week11 {
public static void main(String[] args){
//Scanner sc = new Scanner(System.in);
int a=5, b=3, c=7;
System.out.println("The bigger one is: " + bigger(bigger(a,b),c)); //
}
public static int bigger(int x, int y){
if(x>y)
return(x);
else
return(y);
}
}
#方法二(加入array)
import java.util.*;
public class Week11 {
public static void main(String[] args){
//Scanner sc = new Scanner(System.in);
int [] a = {13,11,7,5,3,2,17,19,99,0};
int champ = a[0]; //紀錄目前最大值,一開始先將a[0]設定為最大
for (int i=1; i<10; i++)
champ = bigger(champ,a[i]);
System.out.println("The bigger one is: " + champ);
}
public static int bigger(int x, int y){
if(x>y)
return(x);
else
return(y);
}
}
```
## HW: 副程式練習
```java=
import java.util.*;
public class Week11 {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("a is: ");
int a = sc.nextInt();
System.out.println("b is: ");
int b = sc.nextInt();
int power = pow(a,b); /* 執行流程: main -> 看到 function (stop) -> 找到 function 並執行 -> 將值傳回 main 繼續做 */
System.out.printf("a 的 b 次方:" + power);
}
public static int pow(int x, int y){ //將程式模組化
int result = 1;
for (int i=1; i<=y; i++){ //迴圈 從1跑到y
result *= x;
}
return(result);
}
}
```
# 2018-11-26 class
## 遞迴 Recursion
* 反覆地透過自己呼叫自己,來解決問題。須有終止條件。
* Ex: ```n! = n * (n-1)!```
```java=
public class Week12 {
public static void main(String[] args) {
int num = 5;
int result = factorial(num);
System.out.printf("%d! = %d\n", num, result);
}
public static int factorial(int n){
if(n == 1){
return(1);
}
else{
return(n*factorial(n-1));
}
}
}
```
* 費氏數列:
```java=
import java.util.*;
public class Week12 {
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
System.out.println("輸入項數:");
int n = sc.nextInt(); // 讀取項數
int [] fib = new int [n]; //依據大小宣告陣列
fib[0] = 1;
fib[1] = 1; // 頭兩項為1
for (int i=2; i<n; i++){
fib[i] = fib[i-1] + fib[i-2]; // 第三項起為前兩項之和
}
for (int i=0; i<n; i++){ //利用迴圈逐項輸出
System.out.printf("F(%d)=%d\n",i,fib[i]);
}
}
}
===> 改遞迴
import java.util.*;
public class Week12 {
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
System.out.println("輸入項數:");
int n = sc.nextInt(); // 讀取項數
for (int i=0; i<n; i++){ //利用迴圈逐項輸出
System.out.printf("F(%d) = %d\n",i,fib(i));
}
}
public static int fib(int n) { //先寫~
if( n==0 || n==1 ){
return(1); //直接return答案
}
else{
return(fib(n-1) + fib(n-2)); // 第三項起為前兩項之和
}
}
}
```
## HW: 遞迴練習-指數運算
```java=
import java.util.*;
public class Week12 {
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
System.out.println("輸入底數 a:");
int a = sc.nextInt(); // 讀取底數 a
System.out.println("輸入指數 b:");
int b = sc.nextInt(); // 讀取指數 b
System.out.printf("%d^%d = %d\n", a, b, my_power(a,b));
}
public static int my_power(int a, int b) {
if( b == 0 )
return(1);
else
return(a*my_power(a,b-1));
}
}
```
# 2018-12-03 class
## 程式呼叫
* call by value:全域變數/區域變數
* call by reference
```java=
public class Week13 {
public static void main(String[] args) {
int [] a ={1,2,3,4,5};
int answer;
answer = biggest(a);
//System.out.println("Bigger number is: "+answer);
}
public static int biggest(int [] x){ //x陣列的內容 = a陣列的內容
for (int i=0; i<x.length; i++){
System.out.printf("x [%d] = %d \n", i, x[i]);
}
return(0);
}
}
```
```java=
public class Week13 {
public static void main(String[] args) {
int [] a ={1,2,3,4,5};
int answer;
answer = biggest(a); //將整串a陣列交給biggest程式處理
// System.out.println(a); //陣列a的參考座標
System.out.println("Bigger number is: "+answer);
}
public static int biggest(int [] x){ //副程式以變數x接收主程式中a陣列的參考位置
int ans = x[0];
for (int i=1; i<x.length; i++){
if (ans < x[i])
ans = x[i];
}
return(ans);
}
}
```
## HW: 終極密碼
```java=
import java.util.*;
import java.lang.Math;
public class Week13 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("=====歡迎玩終極密碼!!=====");
System.out.println("請輸入下界:");
int lb = sc.nextInt(); //lowerbound
System.out.println("請輸入上界:");
int ub = sc.nextInt(); //upperbound
int ans = getRandom(lb, ub), guess;
System.out.println("ans=" + ans);
System.out.println("=====請輸入你所猜的數字=====");
boolean bingo = false; //預設 沒猜中
while(!bingo){
System.out.printf("請輸入介於 %d 到 %d的整數:", lb, ub);
guess = sc.nextInt();
if ( guess == ans ){
System.out.printf("恭喜猜中了");
break;
}
}
}
public static int getRandom(int lb, int ub) {
//副程式以變數x接收主程式中a陣列的參考位置
return((int)((Math.random()*(ub-lb)+1)+lb)); //將x設為終極密碼-電腦變數
}
}
//產生亂數
//副程式:getRandom(int lowerbound, int upperbound)
```
## 終極密碼(改進版)
```java=
import java.util.*;
import java.lang.Math;
public class Week13 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("=====歡迎玩終極密碼!!=====");
System.out.println("請輸入下界:");
int lb = sc.nextInt(); //lowerbound
System.out.println("請輸入上界:");
int ub = sc.nextInt(); //upperbound
int ans = getRandom(lb, ub), guess;
System.out.println("ans=" + ans);
System.out.println("=====請輸入你所猜的數字=====");
boolean bingo = false; //預設 沒猜中
while(!bingo){
System.out.printf("請輸入介於 %d 到 %d的整數:", lb, ub);
guess = sc.nextInt();
if ( guess == ans ){
System.out.printf("恭喜猜中了");
break;
}
if ( guess > ans ){
System.out.println("");
System.out.printf("數字要再小一點");
}
if ( guess < ans ){
System.out.println("");
System.out.println("數字要再大一點");
}
}
}
public static int getRandom(int lb, int ub) {
//副程式以變數x接收主程式中a陣列的參考位置
return((int)((Math.random()*(ub-lb)+1)+lb)); //將x設為終極密碼-電腦變數
}
}
//產生亂數
//副程式:getRandom(int lowerbound, int upperbound)
```
# 2018-12-17 class
* Random 一次跑四位數
```java=
import java.util.*;
import java.lang.Math;
public class Week14 {
public static void main(String[] args) {
int ans;
for(int i=0;i<50;i++){
ans = RanGen(1000,9999);
System.out.println("Ans="+ans);
}
}
public static int RanGen(int lb, int ub){
return ((int)(Math.random()*(ub-lb))+lb);
}
}
```
* Random 個別跑四位數
```java=
# 方法一
import java.util.*;
import java.lang.Math;
public class Week14 {
public static void main(String[] args) {
int [] ans = new int[4];
do{
ans[0] = RanGen(1,9); //產生千位數
ans[1] = RanGen(0,9); //產生百位數
ans[2] = RanGen(0,9); //產生十位數
ans[3] = RanGen(0,9); //產生個位數
}while(!Check(ans)); //逤沒通過檢查,則繼續執行迴圈
for(int i=0;i<4;i++){ //列印答案亂數供測試
System.out.println(ans[i]+" ");
}
}
public static int RanGen(int lb, int ub){ //根據輸入的ub與lb,產生一個介於但包含ub和lb的亂數
return ((int)(Math.random()*(ub-lb+1))+lb);
}
public static boolean Check(int [] a){
if (a[0]==a[1] || a[0]==a[2]|| a[0]==a[3]|| a[1]==a[2]|| a[1]==a[3]|| a[2]==a[3]){
return(false);
}else{
return(true);
}
}
}
# 方法二
import java.util.*;
import java.lang.Math;
public class Week14 {
public static void main(String[] args) {
int [] ans = new int[4];
int answer;
do{
answer = RanGen(1000,9999);
Divide(answer,ans);
}while(!Check(ans)); //逤沒通過檢查,則繼續執行迴圈
for(int i=0; i<4; i++){ //列印答案亂數供測試
System.out.println(ans[i]+" ");
}
}
public static int RanGen(int lb, int ub){ //根據輸入的ub與lb,產生一個介於但包含ub和lb的亂數
return ((int)(Math.random()*(ub-lb+1))+lb);
}
public static boolean Check(int [] a){ //用來檢查是否四個位數各自不重複
if (a[0]==a[1] || a[0]==a[2] || a[0]==a[3] || a[1]==a[2] || a[1]==a[3] || a[2]==a[3]){
return(false); //若任兩位數的數字重複,則傳回"偽"
}else{
return(true);
}
}
public static void Divide(int n, int[] a){ //四個位數,個字拆開
for (int i=3; i>=0; i--){
a[i] = n%10;
n /= 10;
}
}
}
```
* HW:猜數字(幾A幾B)
```java=
import java.util.*;
import java.lang.Math;
public class Week14 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int [] ans = new int[4];
int answer;
int [] guess = new int[4];
do{
answer = RanGen(1000,9999);
Divide(answer,ans); //ans=系統產生的亂數,已放入陣列
}while(!Check(ans)); //若沒通過檢查,則繼續執行迴圈
for(int i=0; i<4; i++){ //列印答案亂數,供測試
System.out.print(ans[i]+" ");
}
int a,b; //用來計算得到的幾A幾B
System.out.println("===猜數字===");
do{
a=0;b=0; //每次重猜數字時,AB都要歸0重新計算
do { //當使用者的輸入中有重複的數字或是時,請重猜!!!
System.out.println("Input你所猜的四位數:");
answer = sc.nextInt();
Divide(answer,guess);
}while(!Check(guess));
Divide(answer,guess);
for(int i=0; i<4; i++)
for(int j=0; j<4; j++)
if (ans[i] == guess[j])
if(i==j)
a++;
else
b++;
System.out.println(a+"A"+b+"B");
}while(a!=4); //若沒通過檢查,則繼續執行迴圈
System.out.println("=== 成功了~ 猜數字game over~ ===");
}
public static int RanGen(int lb, int ub){ //根據輸入的ub與lb,產生一個介於但包含ub和lb的亂數
return ((int)(Math.random()*(ub-lb+1))+lb);
}
public static boolean Check(int [] a){ //用來檢查是否四個位數各自不重複
if (a[0]==a[1] || a[0]==a[2] || a[0]==a[3] || a[1]==a[2] || a[1]==a[3] || a[2]==a[3]){
return(false); //若任兩位數的數字重複,則傳回"偽"
}else{
return(true);
}
}
public static void Divide(int n, int[] a){ //四個位數,各自拆開
for (int i=3; i>=0; i--){
a[i] = n%10;
n /= 10;
}
}
}
```
# 2018-12-24 class
## 氣泡排序法
```java=
# 自己做的版本
import java.util.*;
public class Week16 {
public static void main(String[] args) {
//int [] x = {2,4,6,1,3,8,5,7};
Scanner sc = new Scanner(System.in);
System.out.println("輸入資料筆數:");
int n = sc.nextInt();
int [] x = new int[n]; //宣告n個空間的陣列,並把資料一個個輸入進陣列裡
for (int i = 0; i < n ; i++ ){
System.out.printf("請輸入第%d筆資料",i+1);
x[i] = sc.nextInt();
}
int temp = 0;
for ( int i = 0; i < x.length-1 ; i++ ){ //(最外圈:決定重複次數)
for ( int j = 0; j < x.length-1; j++){ //(最內圈:決定前後兩兩筆大小,大的就往後丟)
while ( x[j] > x[j+1] ){
temp = x[j];
x[j] = x[j+1];
x[j+1] = temp;
}
}
}
for ( int i = 0; i < x.length; i++ ){ //輸出排序結果
System.out.print(x[i]);
}
}
}
```
```java=
# 改成 function 版本
import java.util.*;
public class Week16 {
public static void main(String[] args) {
//int [] x = {2,4,6,1,3,8,5,7};
Scanner sc = new Scanner(System.in);
System.out.println("輸入資料筆數:");
int n = sc.nextInt();
int [] x = new int[n]; //宣告n個空間的陣列,並把資料一個個輸入進陣列裡
for (int i = 0; i < n ; i++ ){
System.out.printf("請輸入第%d筆資料",i+1);
x[i] = sc.nextInt();
}
BubbleSort(x);
for ( int i = 0; i < x.length; i++ ){ //輸出排序結果
System.out.print(x[i]);
}
}
public static void BubbleSort(int [] a) {
int temp = 0;
for ( int i = 0; i < a.length-1 ; i++ ){ //(最外圈:決定重複次數)
for ( int j = 0; j < a.length-1; j++){ //(最內圈:決定前後兩兩筆大小,大的就往後丟)
while ( a[j] > a[j+1] ){
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
}
}
```
## HW: 撲克牌抽牌
```java=
import java.lang.Math;
public class Card {
public static void main(String[] args) {
int [] poker = new int [52];
for (int i = 0; i < poker.length; i++){
poker[i]=0;
}
int ok = 0;
do{
int result = RanGen( 0, 51 );
if ( poker[result] == 0 ){
ok++;
poker[result] = 1;
Color(result);
System.out.println(result+"--------------");
}
}while( ok < 5 );
}
public static int RanGen(int lb, int ub) {
//副程式以變數x接收主程式中a陣列的參考位置
return((int)((Math.random()*(ub-lb)+1)+lb)); //將x設為終極密碼-電腦變數
}
public static int Color(int result) { //使用 % 13 來判斷 A, 1, 2 ..., J ,Q ,K
int k = result % 13;
if (result <= 12){
if (k == 0)
System.out.println("黑桃A");
else if (k == 10)
System.out.println("黑桃J");
else if (k == 11)
System.out.println("黑桃Q");
else if (k == 12)
System.out.println("黑桃K");
else
System.out.println("黑桃"+k);
}
else if (result <= 25)
{
if (k == 0)
System.out.println("紅心A");
else if (k == 10)
System.out.println("紅心J");
else if (k == 11)
System.out.println("紅心Q");
else if (k == 12)
System.out.println("紅心K");
else
System.out.println("紅心"+k);
}
else if (result <= 38)
{
if (k == 0)
System.out.println("方塊A");
else if (k == 10)
System.out.println("方塊J");
else if (k == 11)
System.out.println("方塊Q");
else if (k == 12)
System.out.println("方塊K");
else
System.out.println("方塊"+k);
}
else if (result <= 51)
{
if (k == 0)
System.out.println("梅花A");
else if (k == 10)
System.out.println("梅花J");
else if (k == 11)
System.out.println("梅花Q");
else if (k == 12)
System.out.println("梅花K");
else
System.out.println("梅花"+k);
}
return (k+result);
}
}
//52抽5,不放回
```
---
## 期中考檢討
```java=
import java.util.*;
public class Q3c {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int i,j,k = 9;
do{
for(i = k; i >= 3; i--){ //分別執行當i=9,8,7,6,5,4,3時
for(j = 1; j <= i; j = j+2){
System.out.print(i); //列印i值
}
}
} while (i <= 9 && i >= 3);
}
}
```
```java=
import java.util.*;
public class Q2b {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
/*
(b) 承上題,有沒有可能以 switch 語法完成同樣的輸出功能?若可以,請寫出程式
片段,反之也請說明理由。
Ans: 其實可以用switch寫,但是會非常複雜(使用雙層switch),因此強烈不建議用switch寫
*/
}
}
```
```java=
import java.util.*;
public class Q1c {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("請輸入整數:");
int input = sc.nextInt();
boolean prime = true;
for (int i = 2; i < input; i++) {
if (input % i == 0) {
prime = false;
break;
}
}
System.out.println(prime ? "質數" : "非質數"); //若prime=true,"質數"。反之,"非質數"
}
}
```