# Java練習題
1.題目:古典問題:有一對兔子,從出生後第3個月起每個月都生一對兔子,小兔子長到第三個月後每個月又生一對兔子,假如兔子都不死,問每個月的兔子對數為多少?
2020/12/25完成
```java=
import java.util.Scanner;
public class ex1 {
public static void main(String[] args) {
/*
*
解法:
1.列出每月兔子數量
1,1,2,3,5,8,13........
2.找出規律
a[n]=a[n-1]+a[n-2]
*/
System.out.println("請輸入想知道的兔子數量的月份");
Scanner input=new Scanner(System.in);
int month=input.nextInt();//取得user想知道的月份
System.out.printf("第%d個月的總數為:%d",month,cal(month));
}
private static int cal(int month)
{
if(month==1||month==2)
{
//因為數列的1.2項沒有前兩項,他們都是1
return 1;
}
else
{
return cal(month-1)+cal(month-2);
}
}
}
```
2.題目:判斷101-200之間有多少個質數,並輸出所有質數。
```java=
public class ex2 {
public static void main(String[] args) {
//ANS:主程式:找出101-200, 副程式:判斷是否為質數
int count=0;//數有幾個質數
for(int i=101;i<=200;i++)
{
if(isPrime(i))
{
//如果isPrime的結果為true,印出質數
count++;
System.out.printf("%d\t",i);
}
}
System.out.printf("\n質數共有%d個",count);
}
private static boolean isPrime(int number)
{
for(int j=2;j<number;j++)
{
if(number%j==0)
{
//如果可以被整除,number不是質數
return false;
}
}
//如果整個迴圈跑完都沒有被整除,number是質數]
return true;
}
}
```
3.題目:打印出所有的"水仙花數",所謂"水仙花數"是指一個三位數,其各位數字立方和等於該數本身。例如:153是一個"水仙花數",因為153=1的三次方+5的三次方+3的三次方。
```java=
public class ex3 {
public static void main(String[] args) {
/*
ANS:
主程式:
取出百位、十位、個位數,印出所有水仙花數
副程式:
判斷是否為「水仙花數」
*/
int hun,ten,di;//百、十、個
for(int i=101;i<1000;i++)
{
hun=i/100;
ten=(i%100)/10;//十位
di=i%10;//個位
if(isFlower(i,hun,ten,di))
{
System.out.printf("%d\t",i);
}
}
}
private static boolean isFlower(int i,int hun,int ten,int di)
{
int sum;
//因為Math.pow預設跑出來的資料型態是double
sum=(int) (Math.pow(hun,3)+Math.pow(ten,3)+Math.pow(di,3));
if(i==sum)
return true;
else
{
return false;
}
}
}
```
4.題目:將一個正整數分解質因數。例如:輸入90,打印出90=2*3*3*5。
```java=
import java.util.Scanner;
public class ex4 {
public static void main(String[] args) {
/*
ANS:
1.寫一個迴圈,讓該正整數從2開始除,到該正整數
2.裡面寫判斷式
1.如果除到正整數都沒被整除→印出結果
2.如果可以被整除→把值印出來
*/
String ans="";
int count=0;//看它前面有沒有被整除過1次 有->前面放* 才不會讓最後一個字後面有*
System.out.println("請輸入一個正整數");
Scanner input=new Scanner(System.in);
int number=input.nextInt();
System.out.print(number);
for(int i=2;i<=number;i++)
{
while(number%i==0)
{
if(count==1)
{
number/=i;//number=number/i
System.out.print("*"+i);
}
else
{
//if(count==0)
number/=i;//number=number/i
System.out.print("="+i);
count++;
}
}
if(number==i&&number!=1)
{
//沒有被整除,務必要設number!=1,因為如果number是1->已被整除過
System.out.print(i);
}
}
}
}
```
5.題目:利用條件運算子的巢狀來完成此題:
學習成績>=90分的同學用A表示,
60-89分之間的用B表示,
60分以下的用C表示。
```java=
package test;
import java.util.Scanner;
public class ex5 {
public static void main(String[] args) {
/*
ANS:條件運算子,基本語法(5>3)?"YES":"NO"->YES
*/
System.out.println("請輸入學習成績");
Scanner input=new Scanner(System.in);
int score =input.nextInt();//儲存成績
System.out.println(score>=90?"A":(score>=60?"B":"C"));
}
}
```
6.題目:輸入兩個正整數m和n,求其最大公約數和最小公倍數。
```java=
package test;
import java.util.Scanner;
public class ex6 {
public static void main(String[] args) {
/*
ANS:
輾轉相除法
1.設r=a/b的餘數,若r=0,b為ans
2.互換.a<-b,b<-r
兩數的乘積=兩數的最大公因數*最小公倍數
*/
int m,n,temp=1,r=1;//temp為暫時儲存的數(交換需要),r為餘數
int bigIn=0;//最大公因數
int small=0;//最小公倍數
Scanner input=new Scanner(System.in);
System.out.println("請輸入兩個正整數");
m=input.nextInt();//儲存m
n=input.nextInt();//儲存n
small=m*n;//兩數的乘積
if(n>m)
{
//交換,我們要保持第一個數為較大的數
temp=m;
m=n;
n=temp;
}
//輾轉相除法
try
{
r=m%n;//先算,才不會進去的時候r=1
while(r!=0)
{
//輾轉相除法
m=n;
n=r;
r=m%n;
}
bigIn=n;//最後temp為0時,n為最大公因數
small/=bigIn;//small=small*bigIn 最小公倍數=兩數的乘積/最大公因數
System.out.printf("最大公因數:%d\n最小公倍數:%d",bigIn,small);
}
catch(ArithmeticException e)
{
if(bigIn==0||small==0)
{
System.out.printf("最大公因數:%d\n最小公倍數:%d",1,small);
}
}
}
}
```
7.題目:輸入一行字元,分別統計出其英文字母、空格、數字和其它字元的個數。
```java=
import java.util.Scanner;
public class ex7 {
public static void main(String[] args) {
/*
1.宣告變數(英文字母、空格、數字、其他..)
2.設定條件
3.輸出結果
*一行字元要用字串接收,nextLine()
*字串轉字元,查ASCII碼(如下)
*英文(65-90,97-122)、空格(32)、數字(48-57)、其他(else)
*/
int eng=0;//英文字母
int space=0;//空格
int num=0;//數字
int others=0;//其他字元
Scanner input=new Scanner(System.in);
System.out.println("請輸入一行字元");
String s=input.nextLine();//接收字元
//把字串丟到字元(char)陣列
char[] str=s.toCharArray();
//拿裡面的字元看是否在範圍內,並且做統計
for(int i=0;i<str.length;i++)
{
if(str[i]==32)
{
space++;
}
else if((str[i]>=65&&str[i]<=90)||(str[i]>=97&&str[i]<=122))
{
eng++;
}
else if(str[i]>=48&&str[i]<=57)
{
num++;
}
else
{
others++;
}
}
//print
System.out.printf("英文:%d\n空格:%d\n數字:%d\n其他:%d\n",eng,space,num,others);
}
}
```
8.題目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一個數字。假設a=2,項次為3,則輸出結果的形式如:2+22+222=246;
```java=
package test;
import java.util.Scanner;
public class ex8 {
public static void main(String[] args) {
/*
ANS:
1.設a,n
2.輸入a,n
3.跑迴圈(這個公式)
4.輸出結果
*/
int s=0;
int a,n;//declaration
int changeA;//changeA為儲存改變後的a(a*10+a)
Scanner input=new Scanner(System.in);
System.out.println("請輸入a");
a=input.nextInt();//get a
System.out.println("請輸入n");
n=input.nextInt();//get n
changeA=a;//把a的值丟進去,第一次可以計算
for(int i=0;i<n;i++)
{
s=s+changeA;
changeA=changeA*10+a;//a=a*10+a;
}
System.out.printf("s=%d",s);
}
}
```
9.題目:一個數如果恰好等於它的因子之和,這個數就稱為"完數"。
例如6=1+2+3.程式設計找出1000以內的所有完數。
```java=
package test;
public class ex9 {
public static void main(String[] args) {
/*
ANS:
1.跑迴圈(1-1000)
a.找因數
b.把因數和相加
2.output(print結果)
*/
int sum=0;
for(int i=1;i<=1000;i++)
{
//跑1000個數
for(int j=1;j<i;j++)
{
//因為要找i的因數,所以j要跑到i
if(i%j==0)
{
sum=sum+j;
}
}
if(i==sum)
{
System.out.println(i);//print 完全數
}
sum=0;//sum要重置,重新計算
}
}
}
```
10.印出9x9乘法表
```java=
package test;
public class nine {
public static void main(String[] args) {
//print 9x9乘法表
for(int i=1;i<10;i++)
{
for(int j=1;j<10;j++)
{
System.out.printf("%dx%d=%d\t",j,i,i*j);
}
System.out.println();
}
}
}
```