-
從控制臺(tái)輸入一個(gè)日期,求此日期20天后的日期值(時(shí)間類型為 LocalDateTIme 毁靶,做完后再使用 java.util.Date再做一次)
public class LocalDate { public static void main(String[] args) { LocalDateTime localDateTime = LocalDateTime.now(); LocalDateTime plusDaysResult = localDateTime.plusDays(20L); System.out.println("當(dāng)前時(shí)間是 : " + localDateTime + "\n" + "當(dāng)前時(shí)間加20天后為 : " + plusDaysResult + "\n"); } }
-
銀行要求金額精確小數(shù)點(diǎn)后兩位。從屏幕任意輸入兩個(gè)小數(shù)樱报,進(jìn)行加減乘粗運(yùn)算后結(jié)果要求保留2位小數(shù)(BigDecimal)
int main() { int x,y; printf("請(qǐng)輸入第一個(gè)數(shù):\n"); scanf("%d",&x); printf("請(qǐng)輸入第二個(gè)數(shù):\n"); scanf("%d",&y); prinf("相加:%d\n",x+y); prinf("相減:%d\n",x-y); prinf("相乘:%d\n",x*y); prinf("相除:%.2f\n",x/(y*1.0));//把y*1.0轉(zhuǎn)換成float類型的 printf("取余:%d",x%y); }
-
給定一個(gè)日期更哄,求此日期所在月份的第一天是星期幾
package lxx; import java.util.Scanner; /** * @author kangjiafu * @date 2021/5/5 20:40 */ public class main { public static void main(String[] args) { int totalDay = 0; int dayOfWeek; int day = 0; int dayOfYear = 0; Scanner cs = new Scanner(System.in); System.out.print("請(qǐng)輸入年:"); int year = cs.nextInt(); System.out.print("請(qǐng)輸入月:"); int month = cs.nextInt(); boolean bool = false; if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { bool = true; } for (int i = 1900; i < year; i++) { if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) { totalDay += 366; } else { totalDay += 365; } } for (int i = 1; i <= month; i++) { switch (i) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: day = 31; break; case 4: case 6: case 9: case 11: day = 30; break; case 2: if (bool) { day = 29; break; } else { day = 28; break; } } if (i < month) { dayOfYear += day; } totalDay += dayOfYear; dayOfWeek = (1 + totalDay) % 7; System.out.println("星期天\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六"); for (int i1 = 0; i < dayOfWeek; i++) { System.out.print("\t"); } for (int i1 = 1; i <= day; i++) { if ((totalDay + i) % 7 == 6) { System.out.print(i + "\n"); } else { System.out.print(i + "\t"); } } } } }
- 建立一個(gè)名叫Cat的類:
屬性:姓名、毛色锥累、年齡
方法:顯示信息show【功能自定】、喊叫shout【功能自定】
-
創(chuàng)建一個(gè)對(duì)象貓集歇,姓名為“妮妮”桶略,毛色為“灰色”,年齡為2歲诲宇,在屏幕上輸出該對(duì)象的毛色和年齡际歼,讓該對(duì)象調(diào)用“顯示信息”和“喊叫”兩個(gè)方法。
public class Cat { String name; String color; int age; public void xingMing(){ System.out.println("妮妮"); } public void hanJiao(){ System.out.println("喵"); } }
public class CatTest { public static void main(String[] args){ Cat c=new Cat(); c.name="妮妮"; c.color="灰色"; c.age=2; System.out.println("該貓的顏色為:"+c.color); System.out.println("該貓的年齡為:"+c.age+"歲"); c.xingMing(); c.hanJiao(); } }
-
創(chuàng)建一個(gè)叫做機(jī)動(dòng)車的類:
- 屬性:車牌號(hào)(String)姑蓝,車速(int)鹅心,載重量(double)
- 功能:加速(車速自增1)、減速(車速自減1)纺荧、修改車牌號(hào)(修改為任意值)旭愧,查詢車的載重量。
編寫兩個(gè)構(gòu)造方法:一個(gè)沒有參數(shù)宙暇,在方法中將車牌號(hào)設(shè)置“XX1234”输枯,速 度設(shè)置為100,載重量設(shè)置為100占贫;另一個(gè)構(gòu)造方法帶參數(shù)桃熄,能為對(duì)象的所有屬性賦值
public class main { private String chepaihao; private int chesu; private double zaizhong; public String getChepaihao() { return chepaihao; } public void setChepaihao(String chepaihao) { this.chepaihao = chepaihao; } public int getChesu() { return chesu; } public void setChesu(int chesu) { this.chesu = chesu; } public double getZaizhong() { return zaizhong; } public void setZaizhong(double zaizhong) { this.zaizhong = zaizhong; } public main() { chepaihao="XX1234"; chesu=100; zaizhong=100; } public main(String chepaihao, int chesu, double zaizhong) { super(); this.chepaihao = chepaihao; this.chesu = chesu; this.zaizhong = zaizhong; } public void jiasu(int sudu) { if(sudu<0) { System.out.println("無法加速"); } else { chesu+=sudu; } } public void jiansu(int sudu) { if(sudu<0) { System.out.println("無法減速"); } else { if((chesu-sudu)<0) { System.out.println("減速失敗"); } else { chesu-=sudu; } } } }
public class che { public static void main(String[] args) { main che1=new main(); che1.setChepaihao("豫k5651"); che1.jiasu(30); System.out.println("車牌號(hào)="+che1.getChepaihao()); System.out.println("當(dāng)前車速="+che1.getChesu()); System.out.println("載重="+che1.getZaizhong()); main che2=new main("豫j0394",120,200); che2.jiansu(20); System.out.println("車牌號(hào)="+che2.getChepaihao()); System.out.println("當(dāng)前車速="+che2.getChesu()); System.out.println("載重="+che2.getZaizhong()); } }
-
封裝的作用是什么?
(1)便于使用者正確使用系統(tǒng)型奥,防止錯(cuò)誤修改屬性
(2)降低了構(gòu)建大型系統(tǒng)的風(fēng)險(xiǎn)
(3)提高程序的可重用性
(4)降低程序之間的耦合度
-
以下哪個(gè)是有關(guān)封裝優(yōu)點(diǎn)的正確描述瞳收?單項(xiàng)選擇題 C
A. 可以不用定義成員變量
B. 可以直接通過類名修改屬性
C. 可以不需要改變接口來改變實(shí)現(xiàn),以達(dá)到外部使用代碼無需變動(dòng)
D. 可以不需要改變實(shí)現(xiàn)來改變接口厢汹,已達(dá)到外部使用代碼無需變動(dòng)