Java第一次作業

來源:互聯網
上載者:User

標籤:pos   實現   商品   import   resource   基本資料   final   pack   作業   

(一)學習總結
1.Scanner類實現基本資料輸入的方法
Scanner 使用分隔字元模式將其輸入分解為標記,預設情況下該分隔字元模式與空白匹配。然後可以使用不同的 next 方法將得到的標記轉換為不同類型的值
匯入import java.util.Scanner;
使用

     Scanner sc = new Scanner(System.in);     int i = sc.nextInt();

使使用者從System.in中匯入一個數,根據不同資料類型nextInt()可寫成nextfloat()、nextdouble()、nextboolean()
2.Random類
匯入import java.util.Random;

    Random rand = new Random();     int a=rand.nextInt(); 

Random 類實現的演算法使用一個 protected 工具 + 生產力方法,每次調用它最多可提供 32 個偽隨機產生的位
Math類

    int a=(int)Marh.random()

3.運行程式

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

結果為不等於0.3,因為浮點數類型在運算時,有極小誤差
為了處理精度損失的問題,可以使用java.math.BigDecimal類
修改後的程式

import java.math.BigDecimal;import java.math.MathContext;public class jisuan {    public static void main(String[] args) {        @SuppressWarnings("unused")        // TODO 自動產生的方法存根                class Test {            public void main(String args[]) {                 BigDecimal a = new BigDecimal(0.1);                BigDecimal b = new BigDecimal(0.1);                BigDecimal c = new BigDecimal(0.1);                if(a.add(b).add(c).round(new MathContext(1)).equals(new BigDecimal("0.3"))){                    System.out.println("等於0.3");                }else {                    System.out.println("不等於0.3");                }            }             }    }}

(二)實驗總結
1.看商品猜價格

import java.util.Random;import java.util.Scanner;public class Price {    public static void main(String[] args) {        // TODO 自動產生的方法存根        int a, x,i;        Random rand = new Random();        a = rand.nextInt(100) + 1;        @SuppressWarnings("resource")        Scanner input = new Scanner(System.in);        System.out.println("商品:手錶");        System.out.println("輸入你猜的價格:");        x = input.nextInt();        for (i = 1;; i++) {            if (x>a) {                System.out.println("高了");            } else if (x<a) {                System.out.println("低了");            } else {                System.out.println("恭喜你,猜對了!一共猜了"+i+"次");                break;            }                        if(i>9){                System.out.println("商品的價格為"+a);                break;            }            x = input.nextInt();        }    }}

設計思路:
使用Random類產生隨機數作為商品的價格,通過從鍵盤上輸入資料來表示所猜的價格並與商品原價格做比較若相等跳出迴圈
2.萬年曆

package 萬年曆;import java.util.Scanner;public class Date {    private static final boolean ture = false;    public static void main(String[] args) {        // TODO 自動產生的方法存根        System.out.println("請輸入年份和月份");        @SuppressWarnings("resource")        Scanner input=new Scanner(System.in);        int year=input.nextInt();        int month=input.nextInt();        print(year,month);    }    public static boolean isLeap(int year){        if(year%4==0&&year%100!=0||year%400==0){            return true;        }else{            return false;        }    }    public static int days(int year,int month){        @SuppressWarnings("unused")        boolean y=isLeap(year);        if(month==1||month==3||month==5||month==7||month==8||month==10||month==12){            return 31;        }else if(month==2){            if(y=ture){                return 29;            }else{                return 28;            }        }else{            return 30;        }    }    public static int totalDays(int year,int month){         int sum=0,sum1=0,sum2=0;        for(int i=1900;i<year;i++){            @SuppressWarnings("unused")            boolean x=isLeap(i);            if(x=ture){                sum1=sum1+366;            }else{                sum1=sum1+365;            }        }        for(int j=1;j<month;j++){            if(j==1||j==3||j==5||j==7||j==8||j==10||j==12){                sum2=sum2+31;            }else if(j==4||j==6||j==9||j==11){                sum2=sum2+30;            }else{                @SuppressWarnings("unused")                boolean x=isLeap(year);                if(x=ture){                    sum2=sum2+29;                }else{                    sum2=sum2+28;                }            }        }        sum=sum1+sum2;        return sum;    }    public static void print(int year,int month){        int sum=totalDays(year,month);        int x=sum%7+1;        System.out.printf("%d年%d月\n",year,month);        System.out.printf("星期日    星期一    星期二    星期三    星期四    星期五    星期六\n");            int t=days(year,month);            for(int y=x;y>0;y--){                System.out.printf("\t");            }            for(int i=1;i<=t;i++){                System.out.printf("%d\t",i);                int m=i+x;                if(m%7==0){                    System.out.println();                }            }        }   }

設計思路:
輸入年和月,先判斷是不是閏年,1,3,5,7,8,10,12月是31天,4,6,9,11月是三十天閏年2月29天,平年28天,因為已知1900年1月1日是周一,
所以計算輸入年月距1900.1.1的天數再與7求餘算出這月1號是周幾,判斷出此月的天數,將其輸出
3.評分系統

package 打分;import java.util.Scanner;public class Mark {    public static void main(String[] args) {        // TODO 自動產生的方法存根        int mark[] = null;        mark = new int[10];        int max = 0, min = 0;        double ave[] = null;        ave = new double[5];        for (int i = 0; i < 5; i++) {            System.out.printf("請為第%d個選手打分", i + 1);            for (int j = 0; j < 10; j++) {                @SuppressWarnings("resource")                Scanner input = new Scanner(System.in);                mark[j] = input.nextInt();                            }            max = max(mark);            min = min(mark);            mark[max] = mark[min] = 0;            ave[i] = average(mark, 8);        }        System.out.println("五位選手成績由高到低排序");        print(ave);    }    public static int max(int mark[]) {        int max = mark[0], t = 0;        for (int j = 0; j < 10; j++) {            if (mark[j] > max) {                max = mark[j];                t = j;            }        }        return t;    }    public static int min(int mark[]) {        int min = mark[0], t = 0;        for (int i = 0; i < 10; i++) {            if (mark[i] < min) {                min = mark[i];                t = i;            }        }        return t;    }    public static double average(int mark[], int n) {        double ave = 0;        int sum = 0;        for (int i = 0; i < mark.length; i++) {            sum = sum + mark[i];        }        ave = (double)sum / (double)n;        return ave;    }    public static void print(double ave[]){        for(int i=0;i<ave.length;i++)        for(int j=0;j<ave.length;j++){            if(ave[i]>ave[j]){                double t=ave[i];                ave[i]=ave[j];                ave[j]=t;            }        }        for(int j=0;j<ave.length;j++){            System.out.println(ave[j]);        }    }}

設計思路:
聲明一個一維數組,通過迴圈五次來輸入五個選手的成績,每輸入一個選手的成績時,同時找出最大最小值,並求出平均值存入另一個數組中,之後按照從大到小排序

(三)代碼託管

總結:第一次寫Java實驗,還不是很熟悉Java的內容,和c語言還是有很多不同的地方的,好多時候是按照c語言的習慣來寫的,感覺迴圈數組和方法的問題不大,但是對各種Java類還不是很熟悉,總體感覺Java比c語言能夠實現的功能更多,而且更便捷,更精準。

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.