《Java技術》第一次作業

來源:互聯網
上載者:User

標籤:代碼   誤差   cas   相減   方式   inpu   退出   tin   分系統   

(一)學習總結

1.在java中通過Scanner類完成控制台的輸入,查閱JDK協助文檔,Scanner類實現基本資料輸入的方法是什嗎?不能只用文字描述,一定要寫代碼,通過具體執行個體加以說明。

import java.util.*;public class ABC {    public static void main(String[] args) {            int a;            Scanner input = new Scanner(System.in);            a = input.nextInt();            System.out.println(a);}

這就是輸入一個int型的數,如果想要輸入一個double型的數就把next後面的int改成double。如果要輸入一個字串,next後面就不用寫東西。

2.Random類和Math類的random()方法都能產生隨機數,這兩種方式有什麼區別,各有什麼特點呢?查閱JDK協助文檔,並舉例加以說明。
Math類:int x=(int)(Math.random()*x+y); 就是產生一個x-y之間的數。
Random類:
Random rand = new Random();
int x=rand.nextInt(x)+y;
就是產生一個x-y之間的數。

3.運行下列程式,結果是什嗎?查閱資料,分析為什麼。
輸出結果是不等於0.3,因為在計算double類型資料時會產生誤差。
比較兩個double類型的資料相等時,看二者相減是否無限趨近於0.

public class ABC {    public static void main(String args[]) {         double a = 0.1;        double b = 0.1;        double c = 0.1;        if((a + b + c)-0.3<1e-6){            System.out.println("等於0.3");        }else {            System.out.println("不等於0.3");        }    }     }

(二)實驗總結

實驗內容:
1.看商品猜價格

import java.util.*;public class ABC {    public static void main(String[] args) {        int x = 0, a = 0, i = 0, j = 0, k = 0, n = 0;        String t;        Scanner input = new Scanner(System.in);        ww: while (true) {            x = (int) (Math.random() * 100);            k = 100;            for (i = 5; i > 0; i--) {                System.out.println("請輸入商品價格,還有" + i + "次機會");                a = input.nextInt();                if (a == x) {                    System.out.println("猜對了");                    j += k;                    break;                }                if (a > x) {                    System.out.println("猜大了");                    k -= 20;                }                if (a < x) {                    System.out.println("猜小了");                    k -= 20;                }                if (i == 1) {                    System.out.println("正確答案是" + x);                }            }            n++;            System.out.println("是否進行下一輪?輸入yes進行下一輪,輸入no離開遊戲");            while (true) {                t = input.next();                if (t.equals("yes")) {                    System.out.println("進行下一輪");                    break;                } else if (t.equals("no")) {                    System.out.println("離開遊戲");                    break ww;                } else {                    System.out.println("輸入錯誤,請重新輸入");                }            }        }        System.out.println("一共猜了" + n + "次,總得分為" + j);    }}

2.萬年曆

import java.util.*;public class ABC {    public static void main(String[] args) {        int year, year1, month, sum, sum1, sum2, i, n, zong = 0, h = 0;        Scanner input = new Scanner(System.in);        while (true) {            System.out.println("請輸入年份");            year = input.nextInt();            System.out.println("請輸入月份");            month = input.nextInt();            if (year < 1900 || month < 1 || month > 12) {                System.out.println("輸入錯誤,請重新輸入");            } else {                break;            }        }        sum1 = 0;        sum2 = 0;        year1 = year;        for (year1 = year1 - 1; year1 >= 1900; year1--) {            if (year1 % 4 == 0 && year1 % 100 != 0 || year1 % 400 == 0) {                sum1 = sum1 + 366;            } else {                sum1 = sum1 + 365;            }        }        for (i = 1; i < month; i++) {            if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12) {                sum2 = sum2 + 31;            } else if (i == 4 || i == 6 || i == 9 || i == 11) {                sum2 = sum2 + 30;            } else if (i == 2) {                if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {                    sum2 = sum2 + 29;                } else {                    sum2 = sum2 + 28;                }            }        }        sum = sum1 + sum2;        System.out.println("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六");        switch (sum % 7) {        case 0:            System.out.print("\t");            h = 2;            break;        case 1:            System.out.print("\t\t");            h = 3;            break;        case 2:            System.out.print("\t\t\t");            h = 4;            break;        case 3:            System.out.print("\t\t\t\t");            h = 5;            break;        case 4:            System.out.print("\t\t\t\t\t");            h = 6;            break;        case 5:            System.out.print("\t\t\t\t\t\t");            h = 7;            break;        case 6:            h = 1;            break;        }        if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {            zong = 31;        } else if (month == 4 || month == 6 || month == 9 || month == 11) {            zong = 30;        } else if (month == 2) {            if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {                zong = 29;            }            else {                zong = 28;            }        }        for (n = 1; n <= zong; n++, h++) {            System.out.print(n + "\t");            if (h % 7 == 0) {                System.out.println(" ");            }        }    }}

3.評分系統

import java.util.*;public class ABC {    public static void main(String[] args) {        int a[][] = new int[5][10];        int b[][] = new int[5][8];        int i = 0, j = 0, k = 0, t = 0;        double n, x, y;        double c[] = new double[5];        int d[] = new int[5];        Scanner input = new Scanner(System.in);        for (i = 0; i <= 4; i++) {            for (j = 0; j <= 9; j++) {                System.out.println("請第" + (j + 1) + "位評委給第" + (i + 1) + "號選手打分");                a[i][j] = input.nextInt();            }        }        for (i = 0; i <= 4; i++) {            for (j = 0; j <= 9; j++) {                for (k = 0; k <= 8; k++) {                    if (a[i][k] > a[i][k + 1]) {                        t = a[i][k];                        a[i][k] = a[i][k + 1];                        a[i][k + 1] = t;                    }                }            }        }        for (i = 0; i <= 4; i++) {            for (j = 0; j <= 7; j++) {                b[i][j] = a[i][j + 1];            }        }        for (i = 0; i <= 4; i++) {            n = 0;            x = 0;            for (j = 0; j <= 7; j++) {                n++;                x += b[i][j];            }            c[i] = x / n;            d[i] = i + 1;        }        for (i = 0; i <= 4; i++) {            for (k = 0; k <= 3; k++) {                if (c[k] < c[k + 1]) {                    y = c[k];                    c[k] = c[k + 1];                    c[k + 1] = y;                    t = d[k];                    d[k] = d[k + 1];                    d[k + 1] = t;                }            }        }        for (i = 0; i <= 4; i++) {            System.out.println("第" + (i + 1) + "名是第" + d[i] + "號選手,得分為" + c[i]);        }    }}

《Java技術》第一次作業

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.