2021-05-06

从控制台输入一个日期,求此日期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");

   }

}

银行要求金额精确小数点后两位。从屏幕任意输入两个小数,进行加减乘粗运算后结果要求保留2位小数(BigDecimal)

int main()

{

int x,y;

printf("请输入第一个数:\n");

scanf("%d",&x);

printf("请输入第二个数:\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转换成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【功能自定】

创建一个对象猫,姓名为“妮妮”,毛色为“灰色”,年龄为2岁,在屏幕上输出该对象的毛色和年龄,让该对象调用“显示信息”和“喊叫”两个方法。

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();

   }

}

创建一个叫做机动车的类:

属性:车牌号(String),车速(int),载重量(double)

功能:加速(车速自增1)、减速(车速自减1)、修改车牌号(修改为任意值),查询车的载重量。

编写两个构造方法:一个没有参数,在方法中将车牌号设置“XX1234”,速 度设置为100,载重量设置为100;另一个构造方法带参数,能为对象的所有属性赋值

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());

   }

}

封装的作用是什么?

(1)便于使用者正确使用系统,防止错误修改属性

(2)降低了构建大型系统的风险

(3)提高程序的可重用性

(4)降低程序之间的耦合度

以下哪个是有关封装优点的正确描述?单项选择题  C

A. 可以不用定义成员变量

B. 可以直接通过类名修改属性

C. 可以不需要改变接口来改变实现,以达到外部使用代码无需变动

D. 可以不需要改变实现来改变接口,已达到外部使用代码无需变动

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容