# 《Java技術》第一次作業

來源:互聯網
上載者:User

標籤:port   注意   random   隨機   text   pac   oat   lan   print   

(一)學習總結

1.Scanner類資料的使用方法
需要匯入java.util.Scanner
每種資料都有對應的輸入方法:
需要留意的是String類型,next()方法碰到空格或者斷行符號即結束輸入,如果需要輸入一整行,包含空格,應該用nextLine();
char 類型,沒有輸入的方法;

package java練習;import java.util.Scanner;public class S1 {    public static void main(String[] args) {        // TODO Auto-generated method stub        Scanner in = new Scanner(System.in);        int x=in.nextInt();  // 10        System.out.println(x);   //10        double y=in.nextDouble();  //2.2        System.out.println(y);  //2.2        float z=in.nextFloat();  //1.2        System.out.println(z);  //1.2        String a=in.nextLine(); //hello world        System.out.println(a);  //hello world        String b=in.next(); //hello world        System.out.println(b); //hello    }}

2.Random類和Math類的random()方法
java.lang.Math,Math.random() 返回帶正號的 double 值,該值大於等於 0.0 且小於 1.0;
如果要產生一個在20~100之間的整數應

package java練習;public class S1 {    public static void main(String[] args) {        // TODO Auto-generated method stub        int x=(int)Math.random()*20+80;        System.out.println(x); //80    }}

如果用Random類,產生一個在20~100之間的整數應

package java練習;import java.util.Random;public class S1 {    public static void main(String[] args) {        // TODO Auto-generated method stub        Random rand = new Random();         int x=rand.nextInt(20)+80;         System.out.println(x); //93    }}

如果產生double類型隨機數,則使用nextDouble(x),參數x是產生上限,範圍為0~x;
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”,原因是浮點數類型在運算時,有極小誤差。
修改方法一:使用BigDecimal類
可以使用equals方法和compareTo方法
區別:對於BigDecimal的大小比較,用equals方法的話會不僅會比較值的大小,還會比較兩個對象的精確度,而compareTo方法則不會比較精確度,只比較數值的大小。equals的傳回值是boolean,而compareTo的傳回值為int,相等返回0;

package java練習;import java.math.BigDecimal;import java.math.MathContext;public class S1 {       public static 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");            }        }      }

結果:“等於0.3”;
注意:使用round方法確定精度範圍,需要用到MathContext類。
在定義new BigDecimal("0.3")時,用String類型構造,若用int類型,看下例:

package java練習;import java.math.BigDecimal;import java.math.MathContext;public class S1 {       public static void main(String args[]) {            BigDecimal a = new BigDecimal(0.3);           BigDecimal b = new BigDecimal("0.3");           System.out.println(a);           System.out.println(b);        }      }

結果:

修改方法二:

package java練習;import java.math.BigDecimal;import java.math.MathContext;public class S1 {        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.猜數遊戲
程式設計思路:
1.產生隨機價格
2.輸入猜的價格
3.比較兩個價格,若猜錯且機會沒有用完,回到2
4.輸入是否繼續,若繼續,回到1;
2.萬年曆
程式設計思路:
1.輸入年份year,月份month
2.調用yeardays求1990到year之間的整年的天數
3.調用monthdays求1到moth之間的整月的天數
4.總天數%7取餘數判斷1號為星期幾
3.互評成績
程式設計思路:
1.設計求數組最值函數
2.在求平均值函數中,調用求最值函數,將每個元素求和,最後減去最值求平均
3.調用sort函數排序,輸出成績
注意:輸出定位字元時,應該用雙引號

(三)代碼託管

# 《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.