-
從控制臺輸入一個日期析珊,求此日期20天后的日期值(時間類型為 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("當前時間是 : " + localDateTime + "\n" + "當前時間加20天后為 : " + plusDaysResult + "\n"); } }
-
銀行要求金額精確小數(shù)點后兩位扑馁。從屏幕任意輸入兩個小數(shù)雄家,進行加減乘粗運算后結(jié)果要求保留2位小數(shù)(BigDecimal)
int main() { int x,y; printf("請輸入第一個數(shù):\n"); scanf("%d",&x); printf("請輸入第二個數(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); }
-
給定一個日期,求此日期所在月份的第一天是星期幾
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("請輸入年:"); int year = cs.nextInt(); System.out.print("請輸入月:"); 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"); } } } } }
-
建立一個名叫Cat的類:
屬性:姓名较屿、毛色、年齡
方法:顯示信息show【功能自定】实幕、喊叫shout【功能自定】
-
創(chuàng)建一個對象貓吝镣,姓名為“妮妮”,毛色為“灰色”昆庇,年齡為2歲末贾,在屏幕上輸出該對象的毛色和年齡,讓該對象調(diào)用“顯示信息”和“喊叫”兩個方法整吆。
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)建一個叫做機動車的類:
- 屬性:車牌號(String)拱撵,車速(int),載重量(double)
- 功能:加速(車速自增1)表蝙、減速(車速自減1)拴测、修改車牌號(修改為任意值),查詢車的載重量府蛇。
編寫兩個構(gòu)造方法:一個沒有參數(shù)集索,在方法中將車牌號設置“XX1234”,速 度設置為100汇跨,載重量設置為100务荆;另一個構(gòu)造方法帶參數(shù),能為對象的所有屬性賦值
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="XX231"; 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("車牌號="+che1.getChepaihao()); System.out.println("當前車速="+che1.getChesu()); System.out.println("載重="+che1.getZaizhong()); main che2=new main("豫j0394",120,200); che2.jiansu(20); System.out.println("車牌號="+che2.getChepaihao()); System.out.println("當前車速="+che2.getChesu()); System.out.println("載重="+che2.getZaizhong()); } }