2020-11-25

11-25笔记

类与对象

return关键字的使用

  1. 结束方法的作用

  2. 针对于有返回值类型的方法,使用“return 数据” 方法返回所要的数据。

  3. 注意的:return关键字后面不可以声明执行语句。

代码部分:

11-25笔记

类与对象

return关键字的使用

  1. 结束方法的作用

  2. 针对于有返回值类型的方法,使用“return 数据” 方法返回所要的数据。

  3. 注意的:return关键字后面不可以声明执行语句。

代码部分:

package cn.zhangwuji.exer;
//5.声明一个日期类型MyDate:有属性:年year,月month,日day。
//创建2个日期对象,分别赋值为:你的出生日期,你对象的出生日期,并显示信息。
	
public class dateProblem {
		
		public static void main(String[] args) {
			dateProblem dp=new dateProblem();
			dp.mydate();
			
		
		}
		public void mydate() {
			 date  d1=new date();
			 d1.year=1998;
			 d1.month=07;
			 d1.day=23;
			 System.out.println(d1.year+d1.month+d1.day);
			
		}
		public void mygirldate() {
			 date  d2=new date();
			 d2.year=1999;
			 d2.month=07;
			 d2.day=23;
		}
}
class date{
	int year;
	int month;
	int day;
}

package cn.zhangwuji.exer;
//利用面向对象的编程方法,计算⚪的面积、

import java.util.Scanner;

//测试类
public class circleMath {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		circle c1= new circle();
		System.out.println("请输入圆的半径");
		c1.r=scan.nextDouble();
		double s=c1.area();
		System.out.println("半径为"+c1.r+"的圆的面积是"+c1.area());
			
			
	}
	
	
}

//圆
class circle {
	//属性  半径
	double r;

	public double area() {

		double area = Math.PI * Math.pow(r, 2);
		return area;

	}
}
package cn.zhangwuji.exer;

import java.util.Scanner;

//3.1编写程序,声明一个method方法,在方法中打印一个10*8的*型矩形,在main方法中调用该方法。
//3.2修改上一个程序,在method方法中,除打印一个10*8的*型矩形外,再计算该矩形的面积,并将其作为方法返回值。在main方法中调用该方法接收返回的面积值并打印。
//3.3修改上一个程序,在method方法提供m和n两个参数,方法中打印一个m*n的*型矩形,并计算该矩形的面积,将其作为方法返回值。在main方法中调用该方法,接收返回的面积值并打印。
//
public class MathProblem {
	public static void main(String[] args) {
		MathProblem m1=new MathProblem();
		Scanner scan=new Scanner(System.in);
		System.out.println("请输入矩形长度");
		int n=scan.nextInt();
		System.out.println("请输入矩形宽度");
		int m=scan.nextInt();
		int m2=m1.method(n, m);
		System.out.println("矩形面积是"+m2);
	}

//	1.public void method() {
//		for (int i = 0; i < 10; i++) {
//			for (int j = 0; j < 8; j++) {
//				System.out.print("*");
//			}
//			System.out.println();
//		}
//
//	}
//	2.public int method() {
//		for (int i = 0; i < 10; i++) {
//			for (int j = 0; j < 8; j++) {
//				System.out.print("*");
//			}
//			System.out.println();
//		}
//		return 10*8;
//
//	}
	public int method(int n,int m) {
		
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				System.out.print("*"+" ");
			}
			System.out.println();
		}
		return m*n;

	}
}
题目:定义类Student,包含三个属性:学号number(int),年级state(int),成绩score(int)。

创建20个学生对象,学号为1到20,年级和成绩都由随机数确定。

问题一:打印出3年级(state值为3)的学生信息。
问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息
提示:
1)生成随机数: Math.random(),返回值类型double;
2)四舍五入取整:Math.round(double d),返回值类型long。
math.random();
公式:获取[a,b]范围的随机数:(int)(Math.random( )* (b-a+ 1))+a

package cn.zhangwuji.exer;
//定义类Student,包含三个属性:学号number(int),年级state(int),成绩score(int)。

//创建20个学生对象,学号为1到20,年级和成绩都由随机数确定。

//问题一:打印出3年级(state值为3)的学生信息。
//问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息
//提示:
//1)生成随机数: Math.random(),返回值类型double;
//2)四舍五入取整:Math.round(double d),返回值类型long。
//math.random();
//公式:获取[a,b]范围的随机数:(int)(Math.random( )* (b-a+ 1))+a

public class Student1530time {
	public static void main(String[] args) {
		
	
		Student [] stu =new Student[20];
		
		for(int i=0;i<stu.length;i++) {
			
			stu[i]=new Student();
			stu[i].number=i+1;
			//年纪【1-6】
			stu[i].state=(int)(Math.random()*(6-1+1)+1);
			//成绩【0-100】
			stu[i].score=(int)(Math.random()*(100+1));
			System.out.println("学号"+stu[i].number+"成绩是"+stu[i].score+"年纪是"+stu[i].state);
			//问题一:打印出3年级(state值为3)的学生信息。
		}
		System.out.println("----------------");
			for(int i=0;i<stu.length;i++) {
				if (stu[i].state==3) {
					
					System.out.println(stu[i].showinfo());
					
					}
			}
		
		//冒泡排序 **
		for(int i=0;i<stu.length-1;i++) {
			for(int j=0; j<stu.length-1-i;j++) {
				if(stu[j].score>stu[j+1].score) {
					Student temp=stu[j];
					stu[j]=stu[j+1];
					stu[j+1]=temp;
					
				}
			}
		}
		System.out.println("-------按照成绩升序****----------");
		for(int i=0;i<stu.length;i++) {
		
				
				System.out.println(stu[i].showinfo());
				
				
		}
	
	}		

}
	


class   Student {
	int number;
	int state;
	int score;
	public String  showinfo() {
		return "学号"+number+"成绩是"+score+"年纪是"+state;
	}
}	

完善后的代码:

随机数的取值::获取[a,b]范围的随机数:(int)(Math.random( )* (b-a+ 1))+a

package cn.zhangwuji.exer;
//定义类Student,包含三个属性:学号number(int),年级state(int),成绩score(int)。

//创建20个学生对象,学号为1到20,年级和成绩都由随机数确定。

//问题一:打印出3年级(state值为3)的学生信息。
//问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息
//提示:
//1)生成随机数: Math.random(),返回值类型double;
//2)四舍五入取整:Math.round(double d),返回值类型long。
//math.random();
//公式:获取[a,b]范围的随机数:(int)(Math.random( )* (b-a+ 1))+a

public class Student1530time2 {
	public static void main(String[] args) {
		//对象数组
		Student1[] stu = new Student1[20];
		Student1530time2 test = new Student1530time2();
		// 循环赋值
		test.evalAll(stu);
		// 打印数组
		test.showAll(stu);

		System.out.println("-----查找固定3年级--------");
		
		//查找固定年级的数据
		test.searcheState(stu, 3);
		
		// 冒泡排序 **
		test.sortAll(stu);
		
		System.out.println("-------按照成绩升序****----------");
		test.showAll(stu);	

	}
	//循环赋值方法
	public void evalAll(Student1 []stu) {
		
		for (int i = 0; i < stu.length; i++) {

			stu[i] = new Student1();
			
			stu[i].number = i + 1;
			// 年纪【1-6】
			stu[i].state = (int) (Math.random() * (6 - 1 + 1) + 1);
			// 成绩【0-100】
			stu[i].score = (int) (Math.random() * (100 + 1));
			// 问题一:打印出3年级(state值为3)的学生信息。
		}

	}
	// 遍历打印方法
	public void showAll(Student1[] stu) {
		for (int i = 0; i < stu.length; i++) {
			System.out.println(stu[i].showinfo());

		}
	}
	//封装排序方法
	public void sortAll(Student1[] stu) {
		for (int i = 0; i < stu.length - 1; i++) {
			for (int j = 0; j < stu.length - 1 - i; j++) {
				if (stu[j].score > stu[j + 1].score) {
					Student1 temp = stu[j];
					stu[j] = stu[j + 1];
					stu[j + 1] = temp;

				}
			}
		}
	}
	//查找班级
	/**
	 * 
	 * @param stu 数组
	 * @param score 成绩
	 */
	public void searcheState(Student1[] stu,int score) {
		for(int i=0;i<stu.length;i++) {
			if (stu[i].state==score) {
				
				System.out.println(stu[i].showinfo());
				
			}
		}
	}
}

class Student1 {
	int number;
	int state;
	int score;

	public String showinfo() {
		return "学号" + number + "成绩是" + score + "年纪是" + state;
	}
}

P196

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值