通过对Java的基础知识的学习,需要反复多次的进行题目练习来理解所学知识。 1. 从键盘上接收 年/月/日 一共三个数字,求该年还剩多少天?
import java.util.Scanner;
public class Z01 {
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+"天");
}
public boolean isLeap(int year) {
return(year%4==0&&year%100!=0||year%100!=0);
}
public int getDays(int year) {
return (isLeap(year)==true?366:365);
}
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;
}
public int getLeftDays(int year,int month,int day) {
return getDays(year)-getPassDays(year,month,day);
}
}
运行结果如下图所示:
2. 从键盘上接收 起始年/月/日 截至年/月/日 一共六个数字,求两个日期之间差多少天 该题的方法可以利用第一题的,所以再建一个工具类,方便调用。工具类的代码如下所示:
public class DateUnits {
public boolean isLeap( int year )
{
return year%4==0&year%100!=0 || year%400 == 0;
}
public int getDays( int year )
{
return isLeap(year) == true ? 366 : 365;
}
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;
}
public int getLeaveDays( int year , int month , int day )
{
return getDays(year) - getPassDays(year, month, day);
}
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) {
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 - new DateUnits().getPassDays(startYear, startMonth, startDay)
- new DateUnits().getLeaveDays(endYear, endMonth, endDay);
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) {
int days = new DateUnits().datediff(2002, 2, 6, 2008, 5, 7);
int y = days % 5;
if (y == 0 || y == 4 || y == 3) {
System.out.println("小明在晒网");
} else {
System.out.println("小明在打鱼");
}
}
}
4. 编写一个Java程序在屏幕上输出1!+2!+3!+……+10!的和。
public class Z41 {
public static void main(String[] args) {
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;
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);
}
}
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 {
public static void main(String[] args) {
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);
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));
}
public static int getCountForYear(Student[] students1 , int year)
{
int count = 0;
for (Student student : students1) {
if( year == student.year )
{
count++;
}
}
return count;
}
public static int getCountForSex(Student[] students1 , int flag )
{
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;
}
}
class Student
{
public int sid;
public String sname;
public int year;
public int month;
public int day;
public boolean sex;
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;
}
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()
{
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;
}
}
}
}
本次习题适合初学者学习,后期习题会用更简单的方法写。
|