close

Q1. 某大學為因應國際化趨勢將採用歐美國家使用的「等第制」計分法, 目前需要一個程式能將「百分制」轉成「等第制」並在螢幕上顯示,
請在下面的showGrade方法中寫上程式碼, 並達到以下要求 :
如果傳入的分數參數為80~100則在螢幕上顯示A,
如果傳入的分數參數為70~79則在螢幕上顯示B,
如果傳入的分數參數為60~69則在螢幕上顯示C,
如果傳入的分數參數為50~59則在螢幕上顯示D,
如果傳入的分數參數為0~49則在螢幕上顯示E

public class ScoreUtil {
    public static void showGrade(int score) {

   }
}

嘗試一:

package javaapplication1;

import java.util.Scanner;

public class ScoreUtil {

    public static void showGrade(int score) {

       Scanner scanner = new Scanner (System.in);
       
        switch (level) {

            case 1:
                System.out.print("A");
            case 2:
                System.out.print("B);
            case 3:
                System.out.print("C");
            case 4:
                System.out.print("D");
            case 5:
                System.out.print("E");

        }

    }
}

 

Q2. 請宣告一個class名為Test, 內宣告一個靜態method名為showMultiplicationTable, 不收參數, 沒有回傳值, 並能夠在螢幕上顯示如下格式之九九乘法表 :
2*1=2 3*1=3       4*1=4       5*1=5       6*1=6       7*1=7       8*1=8       9*1=9 
2*2=4 3*2=6       4*2=8       5*2=10 6*2=12 7*2=14 8*2=16 9*2=18 
2*3=6 3*3=9       4*3=12 5*3=15 6*3=18 7*3=21 8*3=24 9*3=27 
2*4=8 3*4=12 4*4=16 5*4=20 6*4=24 7*4=28 8*4=32 9*4=36 
2*5=10 3*5=15 4*5=20 5*5=25 6*5=30 7*5=35 8*5=40 9*5=45 
2*6=12 3*6=18 4*6=24 5*6=30 6*6=36 7*6=42 8*6=48 9*6=54 
2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49 8*7=56 9*7=63 
2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64 9*8=72 
2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81 

從左到右總共有8組, 每組間使用"\t"分隔, 包含最後一組的後面也有"\t", 乘號及等於符號左右都不要有空格, 數字前後也不要有空格
從上到下總共有9行, 其中最後一行印完9*9=81後有換一行

我的答案:

package javaapplication2;
public class JavaApplication2 {
    public static void main(String[] args) {
        int i=0;
        int j=0;
       
        for(i=0,i<=10,i++){
           for (j=0, j<=i; j++)
        }
        System.out.print(i+"x"+j+"="+i*j+"\t");
       
    }finally{
System.out.print("\t");

}
   
}

 

老師答案:

public class Test {
    public static void showMultiplicationTable() {
        for (int i = 1; i <= 9; i++) {
            for (int j = 2; j <= 9; j++) {
                System.out.print(j + "*" + i + "=" + (j * i) + "\t");
   // 上一行也可改為Java 5.0新增的printf()寫法, System.out.printf("%d*%d=%d\t", i, j, i*j);
            }
            System.out.println();
        }
   }
}

 

 

Q.3 假定一對兔子出生滿兩個月就可以生一對小兔子,之後每一個月又可以再生一對小兔子...

也就是年初,只有1對小兔子。
一月,小兔子還沒長大,所以還是只有1+0=1對。
二月,小兔子長成大兔子,開始生下一對小兔子,共有1+1=2對。
三月,大兔子又生了一對小兔子,而小兔子還沒長大,所以共有2+1=3對。
四月,第一對小兔子也長大開始生小兔子,這個月生了兩對,總共有3+2=5對。
五月,第二對小兔子也長大了,所以有三對大兔子會生小兔子,總共有5+3=8對。
六月,第四月生的小兔子也長大了,所以這個月生了5對,總共有8+5=13對。

請寫出一個public class名為Rabbit, 內寫一個static method名為getRabbits, 可接受一個int的月數, 並以long型態回傳經過此月數後的兔子總數.
例如:
Rabbit.getRabbits(0)要回傳1
Rabbit.getRabbits(3)要回傳3
Rabbit.getRabbits(5)要回傳8
Rabbit.getRabbits(6)要回傳13

 

我的答案:

package rabbit;

public class Rabbit {

    public static int getRabbit(int month) {
       
       
        return month;
    }

    public static void main(String[] args) {
        getRabbit(1);
    }

}

 

老師答案:

public class Rabbit {
 public static long getRabbits(int months) {
    long month1 = 0;
        long month2 = 1;
        for(int i=0; i < months; i++) {
            long temp = month1 + month2;
            month1 = month2;
            month2 = temp;
        }
    return month2;
 }

/* 遞迴寫法
    public static long getRabbit(int months) {
        if(months<=1) return 1;
        return getRabbit(months-1) + getRabbit(months-2);
    }
*/
}

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 鈴木保齡球 的頭像
    鈴木保齡球

    Java程式學習手札

    鈴木保齡球 發表在 痞客邦 留言(0) 人氣()