我與java的同居日常(3)

大數(shù)據(jù)

BigInteger:用valueOf方法將普通數(shù)值轉(zhuǎn)化為大數(shù)值,但它也無法直接使用算術(shù)運(yùn)算符(如+、*),需要使用add和multiply來運(yùn)算。

? ? ? ? ? ? ?BigInteger a=BigInteger.valueOf(100)

? ? ? ? ? ? ?BigInteger c=a.add(b)相當(dāng)于c=a+b

? ? ? ? ? ? ?BigInteger d=c.multioly(b)相當(dāng)于d=c*b

Scanner in=new Scanner(System.in);

System.out.print("How many number do you need to draw");

int k=in.nextInt();

System.out.print("What is the highest number you can draw?");

int n=in.nextInt();

BigInteger lotteryOdds=BigInteger.valueOf(1);

for(int i=1;i<=k;i++)

lotteryOdds=lotteryOdds.multiply(BigInteger.valueOf(n-i+1)).divide(BigInteger.valueOf(i));

System.out.println("Your odds are 1 in"+lotteryOdds+".Good luck!");


數(shù)組

int[] a=new int[100]

把數(shù)組控制到五十萬以下。

引用類型的只記住地址。

基本類型的拷貝為深拷貝,引用類型的拷貝為淺拷貝,淺拷貝指數(shù)組用一個(gè)內(nèi)存,深拷貝則是不同的內(nèi)存。


面向?qū)ο蟪绦蛟O(shè)計(jì)(OOP)

類:對(duì)事物描述的定義

靜態(tài)方法是類方法,被所有對(duì)象共享。


隨機(jī)生成數(shù)字:

Scanner in=new Scanner(System.in);

? System.out.print("How many numbers do you need to draw?");

? int k=in.nextInt();

? System.out.print("What is the highest number you can draw?");

int n=in.nextInt();

int[] numbers=new int[n];

for(int i=0;i<numbers.length;i++)

numbers[i]=i+1;

int[] result=new int[k];

for(int i=0;i<result.length;i++){

?int r=(int)(Math.random()*n);

result[i]=numbers[r];

numbers[r]=numbers[n-1];

n--;

? }

?Arrays.sort(result);

System.out.println("Bet the following combination.It will make you rich!");

for(int r:result)

System.out.println(r);


顯示時(shí)間

System.out.println(LocalDate.of(1999, 12, 12));

LocalDate newYearsEve=LocalDate.of(1999, 12, 12);

int year=newYearsEve.getYear();

int month=newYearsEve.getMonthValue();

int day=newYearsEve.getDayOfMonth();

System.out.println(LocalDate.now());

LocalDate aThousandDaysLater=newYearsEve.plusDays(100000);

year=aThousandDaysLater.getYear();

month=aThousandDaysLater.getMonthValue();

day=aThousandDaysLater.getDayOfMonth();

System.out.println(aThousandDaysLater);


LocalDate date=LocalDate.now();

int month=date.getMonthValue();

int today=date.getDayOfMonth();

date=date.minusDays(today-1);

DayOfWeek weekday=date.getDayOfWeek();

int value=weekday.getValue();

System.out.println("Mon Tue Wed Thu Fri Sat Sun");

for(int i=1;i<value;i++)

System.out.print("? ? ");

while(date.getMonthValue()==month)

{

System.out.printf("%3d",date.getDayOfMonth() );

if(date.getDayOfMonth()==today)

System.out.print("*");

else

System.out.print(" ");

date=date.plusDays(1);

if(date.getDayOfWeek().getValue()==1)

System.out.println();

}

if(date.getDayOfWeek().getValue()!=1)

System.out.println();

Mon Tue Wed Thu Fri Sat Sun

? 1? ? 2? ? 3? ? 4? ? 5? ? 6? ? 7

? 8? ? 9? 10? 11? 12? 13? 14

15? 16? 17? 18* 19? 20? 21

22? 23? 24? 25? 26? 27? 28

29? 30? 31


域:field(屬性)

構(gòu)造方法:constructor

class Employee

{

private String name;

private double salary;

private LocalDate hireDay;


構(gòu)造器與類同名

每個(gè)類默認(rèn)有一個(gè)構(gòu)造器

構(gòu)造器沒有返回值

構(gòu)造器和new一起調(diào)用

構(gòu)造器可以有0個(gè)、1個(gè)或多個(gè)參數(shù)

不要再構(gòu)造器里定義與實(shí)例域同名的局部變量

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,854評(píng)論 0 10
  • Java經(jīng)典問題算法大全 /*【程序1】 題目:古典問題:有一對(duì)兔子,從出生后第3個(gè)月起每個(gè)月都生一對(duì)兔子,小兔子...
    趙宇_阿特奇閱讀 2,077評(píng)論 0 2
  • 50道經(jīng)典Java編程練習(xí)題,將數(shù)學(xué)思維運(yùn)用到編程中來。抱歉哈找不到文章的原貼了,有冒犯的麻煩知會(huì)聲哈~ 1.指數(shù)...
    OSET我要編程閱讀 7,289評(píng)論 0 9
  • K# Java核心技術(shù)卷一 逐一聲明每一個(gè)變量可以提高程序的可讀性。 在Java中,變量的聲明盡可能地靠近變量第一...
    蘭陵憂患生閱讀 492評(píng)論 0 1
  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom閱讀 3,205評(píng)論 0 3

友情鏈接更多精彩內(nèi)容