JAVA基础练习题(一)

通过对Java的基础知识的学习,需要反复多次的进行题目练习来理解所学知识。
1. 从键盘上接收 年/月/日 一共三个数字,求该年还剩多少天?

在这里插入图片描述

import java.util.Scanner;

//从键盘上接收 年/月/日   一共三个数字,求该年还剩多少天?
public class Z01 {
	//静态成员属于类,不需要生成对象就存在了。而非静态须要生成对象才产生,因此静态成员不能直接访问非静态。
	//静态的方法是在类加载的时候加载,也就是说类的加载优先于对象的加载
	//不是静态的方法在产生对象的时候才加载,即T01 t=New T01
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		System.out.println("请输入三个数:");
		int year = scan.nextInt();
		int month = scan.nextInt();
		int day = scan.nextInt();
		
		Z01 t = new Z01();
		int days = t.getLeaveDays(year, month, day);
		System.out.println("该年还剩"+days+"天");
	}
	
	//1.判断该年是否是闰年
	public boolean isLeap(int year) {
		return(year%4==0&&year%100!=0||year%100!=0);
	}
	//2.判断该年有多少天
	public int getDays(int year) {
		return (isLeap(year)==true?366:365);
	}
	//3.计算该年已经过了多少天
	//1,3,5,7,8,10,12有31天
	public int getPassDays(int year,int month,int day) {
		int sum = day;
		switch (month-1) {
		case 12:
			sum=sum+31;
		case 11:
			sum=sum+30;
		case 10:
			sum=sum+31;
		case 9:
			sum=sum+30;
		case 8:
			sum=sum+31;
		case 7:
			sum=sum+31;
		case 6:
			sum=sum+30;
		case 5:
			sum=sum+31;
		case 4:
			sum=sum+30;
		case 3:
			sum=sum+31;
		case 2:
			sum=sum+(isLeap(year)==true?29:28);
		case 1:
			sum=sum+31;
		default:
			break;
		}
		return sum;
	}
	//4.计算该年还剩多少天
	public int getLeftDays(int year,int month,int day) {
		return getDays(year)-getPassDays(year,month,day);
	}
}

运行结果如下图所示:
在这里插入图片描述

2. 从键盘上接收 起始年/月/日 截至年/月/日 一共六个数字,求两个日期之间差多少天
在这里插入图片描述
该题的方法可以利用第一题的,所以再建一个工具类,方便调用。工具类的代码如下所示:

public class DateUnits {
    //1.判断该年是否是闰年
	public boolean isLeap( int year )
	{
		return year%4==0&year%100!=0 || year%400 == 0;
	}
	
	//2,判断该年有多少天
	public int getDays( int year )
	{
		return isLeap(year) == true ? 366 : 365;
	}
	
	//3.计算该年已经过了多少天
	public int getPassDays( int year , int month , int day )
	{
		int sum = day;
		switch (month-1) {
		case 12:
			sum = sum + 31;
		case 11:
			sum = sum  + 30;
		case 10:
			sum = sum  + 31;
		case 9:
			sum = sum  + 30;
		case 8:
			sum = sum  + 31;
		case 7:
			sum = sum  + 31;
		case 6:
			sum = sum  + 30;
		case 5:
			sum = sum  + 31;
		case 4:
			sum = sum  + 30;
		case 3:
			sum = sum  + 30;
		case 2:
			sum = sum  + (isLeap(year)==true?29:28);
		case 1:
			sum = sum  + 31;
		default:
			break;
		}
		
		return sum;
	}	
	
	// 4.还剩下多少天
	public int getLeaveDays( int year , int month , int day  )
	{
		return getDays(year) - getPassDays(year, month, day);
	}	
	
	// 5.求两个日期差
	public int datediff(int startYear, int startMonth, int startDay,
			int endYear, int endMonth, int endDay)
	{
		int sum = 0;
		for (int i = startYear; i <= endYear; i++) {
			sum = sum + new DateUtils().getDays(i);
		}
		
		sum = sum -  getPassDays(startYear, startMonth, startDay)  
				- getLeaveDays(endYear, endMonth, endDay);
		return sum;
	}
}

第二题的代码如下所示:(调用工作类)
第一种写法

import java.util.Scanner;

//从键盘上接收 起始年/月/日  截至年/月/日 一共六个数字,求两个日期之间差多少天
public class Z11 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scan = new Scanner(System.in);
		System.out.println("请输入六个数字:");
		int startYear = scan.nextInt();
		int startMonth = scan.nextInt();
		int startDay = scan.nextInt();
		int endYear = scan.nextInt();
		int endMonth = scan.nextInt();
		int endDay = scan.nextInt();

		int days = new Z11().datediff(startYear, startMonth, startDay, endYear, endMonth, endDay);
		System.out.println("相差" + days + "天");
	}
    //写俩日期之差的方法,当然可以把它写在工具类里面调用它
	public int datediff(int startYear, int startMonth, int startDay, int endYear, int endMonth, int endDay) {
   //通过循环遍历的方法计算起始年到结束年的天数,因为可能会有间隔年
		int sum = 0;
		for (int i = startYear; i <= endYear; i++) {
			sum = sum + new DateUnits().getDays(i);
		}
		//sum = sum-起始年份已经过去的天数-结束年份剩余的天数
		sum = sum - new DateUnits().getPassDays(startYear, startMonth, startDay)
				- new DateUnits().getLeaveDays(endYear, endMonth, endDay);
		// System.out.println(sum);
		return sum;
	}

}

第二种写法

import java.util.Scanner;

//从键盘上接收 起始年/月/日  截至年/月/日 一共六个数字,求两个日期之间差多少天
public class Z12 {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		System.out.println("请输入六个数字:");
		int startYear = scan.nextInt();
		int startMonth = scan.nextInt();
		int startDay = scan.nextInt();
		int endYear = scan.nextInt();
		int endMonth = scan.nextInt();
		int endDay = scan.nextInt();
        //调用工具类
		int days = new DateUnits().datediff(startYear, startMonth, startDay, endYear, endMonth, endDay);
		System.out.println("相差" + days + "天");
	}
 
}

运行结果如下图所示:
在这里插入图片描述
3. 小明于2002年的2月6日继承了爸爸的有衣钵,开始两天打鱼,三天晒网,请问小明于2008年05月07日在打鱼还是在晒网?

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// 1.调用工具类里面的函数
		int days = new DateUnits().datediff(2002, 2, 6, 2008, 5, 7);
		// 2.通过求余操作来计算那一天是在打鱼还是晒网
		int y = days % 5;
		// 3.分别是倒数第一天,倒数第二天,倒数第三天晒网
		if (y == 0 || y == 4 || y == 3) {
			System.out.println("小明在晒网");
		} else {
			System.out.println("小明在打鱼");
		}

	}

}

4. 编写一个Java程序在屏幕上输出1!+2!+3!+……+10!的和。

//编写一个Java程序在屏幕上输出1!+2!+3!+……+10!的和。
public class Z41 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		 int n = 10;
		 int sum = 0;
		 for(int i = 1;i<=n;i++) {
			 sum = sum + getN(i);
			 
		 }
		 System.out.println(sum);
	}
	public static int getN(int n) {
		int sum =1;//5*4*3*2*1
		for(int i =1;i<=n;i++) {
			sum = sum*i;
		}
		return sum;
	}

}

5.编写一个点类,一个圆类,圆的成员有圆心和半径。
点类的属性成员有:x坐标,y坐标 ;其中有一个方法用于输出该点的坐标。
圆类的属性成员有:圆心坐标,半径;其中有两个方法分别用于计算圆的面积和周长。

public class Z05 {
	public static void main(String[] args) {
		Point p = new Point(2, 3);
		p.display();
		//没有构造函数初始化直接赋值的方法
		Circle c = new Circle();
		c.p = p;
		c.r = 10; 
		
		System.out.println( c.area() );
		System.err.println( c.perimeter() );
		
		c = new Circle(3, 4, 5);
		c = new Circle(p , 6);

	}
}
//点类的属性成员有:x坐标,y坐标 ;其中有一个方法用于输出该点的坐标
class Point
{
	public int x;
	public int y;
	
	public Point()
	{
		
	}
	public Point(int x , int y)
	{
		this.x = x;
		this.y = y;
	}
	
	public void display()
	{
		System.out.println( "x:=" + x + ",y:=" + y );
	}
}
//圆类的属性成员有:圆心坐标,半径;其中有两个方法分别用于计算圆的面积和周长
class Circle
{
	//属性成员
	public Point p = new Point();
	public int r = 0;
	private double PI = 3.14;
	//计算方法
	public Circle()
	{
		
	}
	public Circle( int x , int y , int r )
	{
		p.x = x;
		p.y = y;
		this.r = r;
	}
	//这种也可以
	public Circle( Point p , int r )//引用点类的圆心
	{
		this.p = p;
		this.r = r;
	}
	//计算圆的面积
	public double area()
	{
		return PI*r*r;
	}
	//计算圆的周长
	public double perimeter()
	{
		return 2*PI*r;
	}
}

这道题考察构造函数的使用,以下用圆类举个例子方便大家理解:
在这里插入图片描述

6. 编写一个类,该类有三个成员方法,分别实现:两个整数相加;两个小数相加;一个整数和一个小数相加。

public class Z06 {
	public static void main(String[] args) {
		Sum sum = new Sum();//构造函数
		double result = sum.add(2.3, 4.5);
		System.out.println(result);
	}
}
//定义求和类
class Sum
{	
//方法的重载
	public int add( int x , int y )
	{
		return x + y;
	}
	
	public double add( double x , double y )
	{
		return x + y;
	}
	
	public double add( int x , double y )
	{
		return x + y;
	}
}

7. 定义一个学生类,学生属性为:学号,姓名,出生年,出生月,出生日,性别。
(1)学生类的方法为:打印学生信息
(2)存在两个构造函数:无参的和初始化所有学生信息的有参构造函数

在测试类中,定义一个数组,存放如下学生信息:
1001 刘德华 2002 1 18 true
1002 刘嘉玲 2003 3 29 false
1003 刘雪华 2000 6 8 false
1004 刘德凯 1999 7 14 true
1005 刘凯威 2005 2 28 true

测试类中有方法,用于计算男女生数量或者总人数
测试类中有方法,用于计算某一年出生的学生数量

public class Z07 {
	//假设main()是测试类
	public static void main(String[] args) {
		//2.定义一个数组		
		Student[] students1 = new Student[5];
		students1[0] = new Student(1001, "刘德华", 2002, 1, 18, true); 
		students1[1] = new Student(1002, "刘嘉玲", 2003, 3, 29, false);
		students1[2] = new Student(1003, "刘德凯", 2000, 6, 8, true);
		students1[3] = new Student(1004, "刘雪华", 1999, 7, 14, false);
		students1[4] = new Student(1005, "刘恺威", 2005, 2, 28, true);
        
		//5.输出
		System.out.println(  "女生的数量:" + getCountForSex( students1,2));
		System.out.println(  "男生的数量:" + getCountForSex( students1,1));
		System.out.println( "总数:" + getCountForSex( students1,3) );
		
		int year = 2002;		
		System.out.println(year + "年出生的学生人数为:" + getCountForYear(students1 , year));
	}
	//3.计算某一年出生的学生数量
	public static int getCountForYear(Student[] students1 , int year)
	{
		int count = 0;	
		//for-each循环,遍历数组的每一个元素
		for (Student student : students1) {
			if( year == student.year )
			{
				count++;
			}
		}
		
		return count;
	}
	//4.计算男女生数量或者总人数
	public static int getCountForSex(Student[] students1 ,  int flag ) // 1. 男  2.女  3.全部
	{
		boolean sex = false;
		if(flag == 1)
		{
			sex = true;
		}else if(flag == 2 )
		{
			sex = false;
		}else
		{
			return students1.length;//求总人数
		}
		
		int count = 0;
		for (Student student : students1) {
			if(  student.sex == sex)
			{
				count++;
			}
		}
		
		return count;
	}
}
//1.学生类
class Student
{
	// 1.1学生信息:学号,姓名,出生年,出生月,出生日,性别
	public int sid;
	public String sname;
	public int year;
	public int month;
	public int day;
	public boolean sex;
	//1.2方法重载了,一般写一个无参的,写一个有参的,不会出错,大家写熟练了可以鼠标操作快速生成
	public Student()
	{
		
	}
	public Student(int sid, String sname, int year, int month, int day, boolean sex) {
		this.sid = sid;
		this.sname = sname;
		this.year = year;
		this.month = month;
		this.day = day;
		this.sex = sex;
	}
	
    //1.3打印方法
	public void display()
	{
		System.out.println(  sid + ","+ sname + " , " 
					+ year+ "," + month + "," + day+ "," +sex );
	}
}

初始化数组有两种方法:
在这里插入图片描述

8. 定义一个货物类,成员属性为:货物编号,货物名称,货物数量,货物价格,货物产地有如下成员方法:
进货方法:可以实现对当前货物编号的货物的数量进行增加
出货方法:可以实现对当前货物编号的货物的数量进行减少

在测试类里面,定义一个数组,用于存储所有的货物,货物信息如下:
1 大白菜 1000 2 山东寿光
2 芹菜 500 3.5 山东滨州
3 土豆 1200 1.8 山东济南

在测试类里面,提示用户:“请选择 进货/出货 ,进货输入1,出货输入2”
接收用户输入的操作类型后,
接着提示用户:“请输入要进货的货物编号”,
用户从键盘上输入货物的编号后,显示出要进货/出货的货物信息。
接着提示用户 请输入进货/出货数量,
用户从键盘上行输入进货/出货数量后,实现对该货物的货物数量的增加/减少。
并将进货/出货后的该货物信息显示在控制台上。

public class Z08 {

	public static void main(String[] args) {
		System.out.println("请输入您要进货/出货,进货输入1,出货输入2:");
		Scanner sc = new Scanner(System.in);
		int flag = sc.nextInt();
		System.out.println("请输入要进货/出货的货物编号");
		int gid = sc.nextInt();
		
		Goods goods = new Goods();
		
		if( flag == 1 ) //进货
		{
			System.out.println("请输入进货的数量:");
			int count = sc.nextInt();
			goods.inputGood(gid, count);
		}else
		{
			System.out.println("请输入出货的数量:");
			int count = sc.nextInt();
			goods.outputGood(gid, count);
		}
		//循环遍历,查看货物
		for (Good good : goods.goods) {
			good.display();
		}
	}
}


class Good
{
	// 货物编号,货物名称,货物数量,货物价格,货物产地
	public int gid;
	public String gname;
	public int total;
	public double price;
	public String address;
	//构造函数,一个有参,一个无参
	public Good(int gid, String gname, int total, double price, String address) {
		super();
		this.gid = gid;
		this.gname = gname;
		this.total = total;
		this.price = price;
		this.address = address;
	}
	
	public Good() {
		super();
	}
	
	public void display()
	{//学了toString的用toString生成
		System.out.println(
				gid+ "," +gname + "," +total+ "," +price+ "," +address
				);
	}
	
	public void input( int count ) //进货
	{
		this.total = this.total + count;
	}
	
	public void output( int count ) //出货
	{
		this.total = this.total - count;
	}
}

class Goods //仓库,以后可以用集合
{
	public Good[] goods = new Good[3];
	//初始化数组用构造函数来写
	public Goods()
	{
		goods = new Good[3];
		goods[0] = new Good(1, "大白菜", 1000, 2, "山东寿光");
		goods[1] = new Good(2, "芹菜", 500, 3.5, "山东滨州");
		goods[2] = new Good(3, "土豆", 1200, 1.8, "山东济南");		
	}
	
	// 进货
	public void inputGood( int gid , int count )
	{
		for (Good good : goods) {
			if( gid == good.gid )
			{
				good.input(count);
				break;
			}
		}
	}
	
	// 出货
	public void outputGood( int gid , int count )
	{
		for (Good good : goods) {
			if( gid == good.gid )
			{
				good.output(count);
				break;
			}
		}
	}
}

本次习题适合初学者学习,后期习题会用更简单的方法写。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

全宇宙无敌最靓的仔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值