---
# System prepended metadata

title: 'Java第九週[JPA306~310 while迴圈+副程式運用、continue運用、do_while運用]'
tags: [複習用, JAVA課堂學習, 高科大]

---

# Java第九週[JPA306~310 while迴圈+副程式運用、continue運用、do_while運用]

#### 複習用

## 開始解題(JPA306~310):
### 第一題(JPA306)-迴圈階乘計算[(不斷執行，直到條件不成立跳出)]:
#### 計算公式:
[${m^n=m*m*m*....共n次}$]
已知n，可設for迴圈，讓他跑n次
然後m乘上他自己即可
但因為在輸入時，m值會改變，所以要再設一個變數承接
```java=1
for(int i=1;i<=n;i++) //跑n次
{
    int ans=1;
    ans*=m;
}
```
#### 題目解答問題處:
[${100^7}=100000000000000$]
但解答為[${100^7}=276447232$]
是因為int溢位導致，就是超出本身int能計算的範圍
所以結果才是276447232，但我們題庫為此結果，所以不做處理。(參考資料2為處理教學)

{%youtube q-G7YOae1Xw %}
[參考資料](https://happyplayblogs.blogspot.com/2015/05/tqc-java6-306.html)
[參考資料2](https://java.4-x.tw/java-03-2)

![](https://i.imgur.com/UfGJwr9.png)
解答:
```java
import java.util.*;
public class JPA306
{
	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) //當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 result=1;	
        	for(int i=1;i<=n;i++)
		{
			result*=m;    //一定要一個ans或result承接
		}                    //若用m*=m;會多成一個m值
		return result;       //因為本來的m=1被蓋掉成m=2
    	}
}
```
---
### 第二題(JPA307)-迴圈最大公因數[使用輾轉消除法]:
#### 輾轉消除法(歐幾里得算法)
求[最大公因數]的算法，可藉此公式快速算出
**兩個整數的最大公因數=其中較小的數(n)&兩數相除餘數的最大公因數(m%n)**
剩下 **還沒有變成零的數(m)** 就是兩數的最大公因數
```java=1
ans=m%n;  //兩數相除餘數的最大公因數
m=n;      //其中較小的數(假設m為較大)
n=ans;    //再把餘數給n
          //形成m一定最大，n最小，直到n=0就跳出(用while設條件)
```

![](https://i.imgur.com/rtH04xn.png)
解答:
```java
import java.util.Scanner; //自打
public class JPA307 
{
	public static void main (String argv[])
	{
        	int num1, num2;
		Scanner keyboard=new Scanner(System.in); //自打
		
        	System.out.println("Input:"); 
		num1=keyboard.nextInt();

		while (num1!=999) //自打
		{
		
		num2=keyboard.nextInt();            	
		System.out.println(gcd(num1,num2)); 
		
		System.out.println("Input:"); 
		num1=keyboard.nextInt();		
        	}
    	}  
  
	static int gcd(int m, int n)
	{
        	int result=0;		//自打
        	while(n!=0)
		{
            		result=m%n;
			m=n;
			n=result;
        	}	
		return m;        
    	}
}
```
---
### 第三題(JPA308)-電腦週邊費用總計[使用do_while迴圈&總和]:
* 注意!!!記得要多判斷
```java=1
if(s!=-1)
{
    tatol+=s; //避免多加-1，導致結果不對
}
```
![](https://i.imgur.com/F9KIplp.png)
![](https://i.imgur.com/NYIjpnQ.png)

解答:
```java
import java.util.Scanner;
public class JPA308
{
	static Scanner keyboard = new Scanner(System.in);
	static int i = -1;	//用不到
	public static void main(String[] args) 
	{
        	int total = 0, s = 0;
        	
		do
		{
			System.out.print("請輸入消費金額，或輸入-1結束:");
			s=keyboard.nextInt();
			
			if(s!=-1)
			{
				total+=s;
			}
		}while(s!=-1);	//s=-1就跳出(結束)
		System.out.println("電腦週邊總消費:"+total);
    	}
}
```
---
### 第四題(JPA309)-迴圈倍數判斷[continue運用]:
#### continue和break的差別
continue作用與break類似，使用於**迴圈**
**break**會結束區塊執行(**直接跳出**)
**continue**只會**略過**之後陳述句，並回到迴圈區塊開頭**進行下一次迴圈**，而**不是離開迴圈**

**參考文章:**
[第一篇](https://tubobo.net/jobindex/2020/08/29/%E3%80%90java%E3%80%91return-break-continue-%E5%B7%AE%E5%88%A5/)
[第二篇](https://openhome.cc/Gossip/Java/Break-Continue.html)

* **注意!!!迴圈mod時，不是n%，而是 i%，這樣才能跑迴圈**
![](https://i.imgur.com/efvtmzS.png)
解答:
```java
import java.util.Scanner;
class JPA309 
{
	public static void main(String argv[])
	{
		int n,sum=0;

		Scanner keyboard=new Scanner(System.in);		
		n=keyboard.nextInt();

        	for(int i=1;i<=n;i++)
		{
			if(i%7==0)
			{
				continue;  //跳下一個迴圈(忽略本身執行的迴圈)
			}
			else if((i%3==0)||(i%5==0))
			{
				sum+=i;
			}
		}

        	System.out.println("Answer: " + sum);
	}
}
```
---
### 第五題(JPA310)-迴圈正偶數相加[do_while迴圈運用]:
#### do_while迴圈
while為 **成立就繼續跑(不成立跳出)**
題目條件為 n>0、n%2==0
當n=3，3>0，但不是偶數
所以要用||(or)，不用&&(and)
若我們要繼續跑第一個while迴圈，讓他重複執行
直到結果為 **正偶數** 才能跑第二個迴圈(去計算相加)

![](https://i.imgur.com/qQAXZyg.png)
解答:
```java
import java.util.Scanner;
public class JPA310
{
	static Scanner keyboard = new Scanner(System.in);
	public static void main(String[] args) 
	{	
		int n=0;
		do
		{
			System.out.print("請輸入n的值(n>0，且為偶數):");
			n=keyboard.nextInt();

		}while((n<=0)||(n%2!=0));
	
		int sum=0,i=2;
		do
		{
			sum+=i;
			i+=2;
		}while(i<=n);
		System.out.printf("2+4+...+%d=%d\n",n,sum);	
    	}
}
```

---
最後編輯時間:2021/4/28 12:56pm.
###### tags: `JAVA課堂學習` `複習用` `高科大`