Day06
public class Work01{
/**
1.求出1-100之间偶数和
*/
public static void main(String[] args){
int sum = 0;
for(int i = 1;i<=100;i++){
if(i % 2 == 0){
sum += i;
}
}
System.out.println("1~100之间偶数之和为:" + sum);
}
}
public class Work02{
/**
2.计算从1到100个值以内所有能被3或者17整除的数的和并输出。
*/
public static void main(String[] args){
int sum = 0;
for(int i = 1;i<=100;i++){
if(i%3==0 || i%17==0){
sum += i;
}
}
System.out.println("1~100之间能被3或者17整除的数之和为:" + sum);
}
}
public class Work03{
/**
3.3000米长的绳子,每天减一半。问多少天这个绳子会小于5米?不考虑小数
*/
public static void main(String[] args){
int length = 3000;
int day = 0;
while(length >= 5){
length /= 2;
day++;
}
System.out.println(day + "天后绳子的长度小于5米");
}
}
public class Work04{
/**
4.求2~100之内的素数。【素数 : 只能被1或本身整除的数】
*/
public static void main(String[] args){
for(int num = 2;num<=100;num++){
boolean bool = true;
for(int i = 2;i<num;i++){
if(num % i == 0){
bool = false;
break;
}
}
if(bool){
System.out.println(num);
}
}
}
}
import java.util.Scanner;
public class Work05{
/**
5.模拟ATM取款机的三次密码校验,当天达到3次输入密码错误,
则提示“账号被冻结”,其他情况则提示还有多少次输入机会
*/
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int count = 3;
while(count > 0){
System.out.println("请输入密码:");
String password = scan.next();
if(password.equals("123456")){
System.out.println("随便取款...");
break;
}else{
count--;
if(count == 0){
System.out.println("账号被冻结,请携带身份证前往银行办理");
break;
}
System.out.println("您还有" + count + "次机会");
System.out.println("是否继续录入,录入请按y,退出请按其他任意键");
String str = scan.next();
if(!str.equals("y")){
System.out.println("正在退出**银行系统,欢迎下次再来...");
break;
}
}
}
}
}
import java.util.Scanner;
public class Work06{
/**
6.要求循环录入2个班的学员成绩,假设每个班都有3个学员,
依次录入,统计超过90分的学员人数,以及这批超过90分的学员平均分。
*/
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int count = 0;//超过90分学生的人数
double sum = 0;//超过90分学生成绩的总和
for(int i = 1;i<=2;i++){
for(int j = 1;j<=3;j++){
System.out.println("请输入第" + i + "个班的第" + j + "名学生的成绩:");
double score = scan.nextDouble();
if(score > 90){
count++;
sum += score;
}
}
}
System.out.println("超过90分学生的人数为:" + count);
double avg = sum/count;
System.out.println("超过90分学生成绩的平均分为:" + avg);
//保留两位小数
avg = (int)(avg*100)/100.0;
System.out.println("超过90分学生成绩的平均分为:" + avg);
}
}
public class Test01{
/**
知识点:静态方法
理解:特定功能的代码块
好处:解决代码的冗余
语法结构:
访问修饰符 static 返回值类型 方法名([参数列表]){
...代码块...
}
分类:
无参数无返回值的方法
带参数的方法
带返回值的方法
知识点:静态方法 -- 无参数无返回值的方法
语法结构:
public static void 方法名(){
...代码块
}
注意:
1.public是访问修饰符的一种,表示公有的
2.void表示无返回值(关键字)
3.方法在类中声明
4.方法与方法之间是平级关系
5.方法没被调用,就是个摆设
需求1:模拟玩家LOL的操作过程
分析:走位、发技能
需求2:设计一个方法,打印三角形
*/
public static void main(String[] args){
//调用方法
//Test01.run();
//Test01.run();
//Test01.play();
//Test01.run();
//Test01.play();
//Test01.run();
//调用方法
run();
run();
play();
run();
play();
run();
printStar();
printStar();
}
public static void printStar(){
for(int i = 0;i<5;i++){
for(int j = 0;j<=i;j++){
System.out.print("*");
}
System.out.println();
}
}
public static void run(){
System.out.println("风骚的走位 -- 500行");
}
public static void play(){
System.out.println("发技能 -- 1200行");
}
}
public class Test02{
/**
知识点:静态方法 -- 带参数的方法
语法结构:
public static void 方法名(参数列表){
...代码块...
}
注意:
1.形式参数/形参:声明方法时规定的参数
2.形参必须声明类型
3.形参属于该方法的变量,作用域就在该方法内,形参也是局部变量
4.局部变量:方法里声明的变量
5.实际参数/实参:调用方法时传入的数据
6.实参和形参的类型必须兼容
7.实参和形参可以有多个,用逗号分割
需求1:设计一个方法,打印三角形,三角形的行数由调用方传入
需求2:设计一个方法,传入两个int值,输出最大值
*/
public static void main(String[] args){
printStar(3);
printStar(5);
printStar('A');//'A' - 65
getMax(10,20);
}
public static void getMax(int a,int b){
int max = (a>b)?a:b;
System.out.println("最大值为:" + max);
}
public static void printStar(int num){
for(int i = 0;i<num;i++){
for(int j = 0;j<=i;j++){
System.out.print("*");
}
System.out.println();
}
}
}
import java.util.Scanner;
public class Test03{
/**
知识点:静态方法 -- 带返回值的方法
语法结构:
public static 返回值类型 方法名([参数列表]){
...代码块...
return 数据;//1.结束当前方法 2.将数据返回给调用方
}
注意:
1.带返回值的方法有没有参数要看具体需求
2.声明方法时需要设置返回值类型
3.return后接的是需要返回的具体数据
4.方法功能单一性(一个方法的功能不要过于强大)
5.方法声明时规定的返回值类型 和 return后的数据 必须兼容
6.返回值只能有一个,如果想返回多个值就必须使用数组、集合、....
需求:
设计一个方法,传入两个int值,返回最大值
在控制台输入是三个int值,输出最大值
*/
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.println("请输入第一个数字:");
int a = scan.nextInt();
System.out.println("请输入第二个数字:");
int b = scan.nextInt();
System.out.println("请输入第三个数字:");
int c = scan.nextInt();
int max = getMax(a,b);
max = getMax(max,c);
System.out.println("最大值为:" + max);
}
public static int getMax(int a,int b){
int max = (a>b)?a:b;
return max;
}
}
public class Test04{
/**
知识点:方法的重载
概念:方法之间的关系
条件:
1.在同一个类中
2.方法名必须一致
3.参数列表的个数或者类型不一致
4.与返回值无关
好处:系统会根据实参类型自动匹配到对应的方法中
应用场景:在同一个类中,多个方法功能大概一致,但是细节实现不一致,就可以考虑使用重载
需求1:设计一个方法,传入两个int值,返回最大值 -- getMax(int a,int b)
需求2:设计一个方法,传入三个int值,返回最大值 -- getMax(int a,int b,int c)
需求3:设计一个方法,传入两个double值,返回最大值 -- getMax(double a,double b)
需求4:设计一个方法,传入三个double值,返回最大值 -- getMax(double a,double b,double c)
设计方法的步骤:
1.考虑方法名(见名知意)
2.考虑形参(几个?类型?)
3.考虑返回值(需不需要?返回值类型)
*/
public static void main(String[] args){
//打印getMax方法,前提是该方法必须有返回值
System.out.println(getMax(10.1,20,30));
}
public static int getMax(int a,int b){
return (a>b)?a:b;
}
public static int getMax(int a,int b,int c){
int max = (a>b)?a:b;
max = (max>c)?max:c;
return max;
}
public static double getMax(double a,double b){
return (a>b)?a:b;
}
public static double getMax(double a,double b,double c){
double max = (a>b)?a:b;
max = (max>c)?max:c;
return max;
}
//错误示范:重载要关注形参的类型,不关注形参的名字
//public static void method(String str,int i){
//}
//public static void method(String ss,int ii){
//}
}
public class Test05{
/**
知识点:方法的递归
含义:方法调用方法自身
注意:递归是一种程序设计的思想
*/
public static void main(String[] args){
//错误示范
//StackOverflowError - 栈内存溢出
//原因:调用方法就会在栈内存中开辟空间,用于存放该方法的局部变量,
// 死循环的调用方法,就会使得栈内存满载并溢出了
method();
}
public static void method(){
method();
}
}
public class Test06{
/**
知识点:方法的递归
含义:方法调用方法自身
注意:递归是一种程序设计的思想
经验:
1.找规律:什么情况下方法应该调用方法自身
2.找出口:什么情况下应该解决该方法
需求:设计一个方法,传入一个int值n,求n的阶乘
分析:
5! = 1*2*3*4*5 -> 5! = 4! * 5
4! = 1*2*3*4 -> 4! = 3! * 4
3! = 1*2*3 -> 3! = 2! * 3
2! = 1*2 -> 2! = 1! * 2
1! = 1 -> 1! = 1
找规律:n! = (n-1)! * n
找出口:1! = 1
*/
public static void main(String[] args){
int num = getFactorial(5);
System.out.println(num);
}
public static int getFactorial(int n){
if(n != 1){
return getFactorial(n-1) * n;
}else{
return 1;
}
}
}
public class Test07{
/**
知识点:方法的递归
含义:方法调用方法自身
注意:递归是一种程序设计的思想
经验:
1.找规律:什么情况下方法应该调用方法自身
2.找出口:什么情况下应该解决该方法
需求 - 不死神兔问题:
有1对兔子,从出生后的第3个月起每个月都生一对兔子,
小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问第n个月有几对兔子
设计一个方法,传入月份,获取当月兔子的对数
分析:
找规律:当月兔子的对数等于上个月+上上个月兔子的对数
找出口:1月或2月都是1对兔子
扩展:
不死神兔的数据:1,1,2,3,5,8,13,21...
这种数列叫做斐波那契数列/黄金分割数列
该数列相邻两个数的比例越往后越趋近于0.618
*/
public static void main(String[] args){
int num = getRabbit(10);
System.out.println(num);
}
public static int getRabbit(int month){
if(month == 1 || month == 2){
return 1;
}else{
return getRabbit(month-1) + getRabbit(month-2);
}
}
}
import java.util.Scanner;
public class Test08{
/**
知识点:万年历
需求:输入年和月,打印当月的日历
线索:1900年1月1日是星期一
要想打印当月的日历,需要哪些数据作为支持?
年份、月份、当月天数、当月第一天是星期几
*/
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.println("请输入年:");
int year = scan.nextInt();
System.out.println("请输入月:");
int month = scan.nextInt();
//当月天数
int day = getDay(year,month);
//总天数
int allDay = getAllDay(year,month);
//计算星期
int week = getWeek(allDay);
//打印日历
printCalendar(year,month,day,week);
}
//打印日历
public static void printCalendar(int year,int month,int day,int week){
System.out.println(" --- " + year + "年" + month + "月 ---");
System.out.println("一\t二\t三\t四\t五\t六\t日");
int count = 0;
for(int i = 1;i<week;i++){
System.out.print("\t");
count++;
}
for(int i = 1;i<=day;i++){
System.out.print(i + "\t");
count++;
if(count % 7 == 0){
System.out.println();
}
}
}
//计算星期
public static int getWeek(int allDay){
int week = allDay%7;
if(week == 0){
week = 7;
}
return week;
}
//计算1900年1月1日到输入年输入月的总天数
public static int getAllDay(int year,int month){
int allDay = 0;
//年的总天数
for(int i = 1900;i<year;i++){
if(isLeapYear(i)){
allDay += 366;
}else{
allDay += 365;
}
}
//月的总天数
for(int i = 1;i<month;i++){
allDay += getDay(year,i);
}
//累加当月的第一天
allDay += 1;
return allDay;
}
//获取当月天数
public static int getDay(int year,int month){
int day = 0;
switch(month){
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(isLeapYear(year)){
day = 29;
}else{
day = 28;
}
break;
}
return day;
}
//判断是否是闰年
public static boolean isLeapYear(int year){
if(year%4==0 && year%100!=0 || year%400==0){
return true;
}
return false;
}
}
public class Test09{
/**
知识点:Java的引用数据类型
数组
类
接口
枚举
知识点:一维数组
理解:一组数据的容器
声明:
数据类型[] 数组名;
数据类型 数组名[];
概念:
1.数组是引用数据类型
2.数组中的数据也叫做元素
3.元素都是编号也叫做下标/索引
4.下标从0开始
5.数组元素在内存中开辟的空间是连续的
6.数组一旦初始化成功,长度不可变(意味着数组不能添加和删除)
数组的初始化:
静态初始化:元素由程序员指定,长度由系统分配
动态初始化:
*/
public static void main(String[] args){
//静态初始化1:
//String[] names = new String[]{"麻生希","椎名空","水菜丽","朝桐光","樱井步"};
//静态初始化2:先声明,再初始化
//String[] names;
//names = new String[]{"麻生希","椎名空","水菜丽","朝桐光","樱井步"};
//静态初始化3:简化静态初始化1:
String[] names = {"麻生希","椎名空","水菜丽","朝桐光","樱井步"};
//设置指定下标上的元素
names[2] = "侯小康";
//获取指定下标上的元素
String str = names[2];
System.out.println("获取指定下标上的元素:" + str);//侯小康
//获取数组元素的个数
int len = names.length;
System.out.println("获取数组元素的个数:" + len);//5
System.out.println("------------------------------------");
//遍历数组 -- for
for(int i = 0;i<names.length;i++){
System.out.println(names[i]);
}
System.out.println("------------------------------------");
//遍历数组 -- foreach/增强for循环
for(String element : names){
System.out.println(element);
}
/**
for vs foreach
遍历数组时,使用到下标 -- for循环
遍历数组时,不使用到下标 -- foreach
*/
}
}
public class Test10{
/**
知识点:一维数组
理解:一组数据的容器
声明:
数据类型[] 数组名;
数据类型 数组名[];
概念:
1.数组是引用数据类型
2.数组中的数据也叫做元素
3.元素都是编号也叫做下标/索引
4.下标从0开始
5.数组元素在内存中开辟的空间是连续的
6.数组一旦初始化成功,长度不可变(意味着数组不能添加和删除)
数组的初始化:
静态初始化:元素由程序员指定,长度由系统分配
动态初始化:长度由程序员指定,元素由系统分配
整数类型默认值:0
浮点类型默认值:0.0
字符类型默认值:' '
布尔类型默认值:false
引用类型默认值:null(空)
*/
public static void main(String[] args){
//动态初始化1:[null,null,null,null,null]
//String[] names = new String[5];//5表示该数组需要开辟空间的个数
//动态初始化2:先声明,再初始化
String[] names;
names = new String[5];
//设置指定下标上的元素
names[0] = "侯小康1";
names[1] = "侯小康2";
names[2] = "侯小康3";
names[3] = "侯小康4";
names[4] = "侯小康5";
//ArrayIndexOutOfBoundsException -- 数组下标越界异常
//names[5] = "侯小康";
//获取指定下标上的元素
String str = names[2];
System.out.println("获取指定下标上的元素:" + str);//侯小康
//获取数组元素的个数
int len = names.length;
System.out.println("获取数组元素的个数:" + len);//5
System.out.println("------------------------------------");
//遍历数组 -- for
for(int i = 0;i<names.length;i++){
System.out.println(names[i]);
}
System.out.println("------------------------------------");
//遍历数组 -- foreach/增强for循环
for(String element : names){
System.out.println(element);
}
/**
for vs foreach
遍历数组时,使用到下标 -- for循环
遍历数组时,不使用到下标 -- foreach
*/
}
}
import java.util.Scanner;
public class Test11{
/**
知识点:一维数组的案例
创建一个int类型的数组,长度由用户指定,数据由用户输入,输入完毕后获取最大值并输出
*/
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.println("请输入数组长度:");
int len = scan.nextInt();
int[] arr = new int[len];
for(int i = 0;i<arr.length;i++){
System.out.println("请输入第" + (i+1) + "个元素:");
int num = scan.nextInt();
arr[i] = num;
}
//假设下标为0的位置上的元素为最大值
int max = arr[0];
for(int i = 1;i<arr.length;i++){
if(max < arr[i]){
max = arr[i];
}
}
System.out.println("最大值为:" + max);
/**
小结:
初始化时只知道元素,就使用静态初始化
初始化时只知道长度,就使用动态初始化
int类型的数组为引用数据类型,数组中的元素是基本数据类型
*/
}
}
1.方法
无参数无返回值的方法
带参数的方法(形参和实参)
带返回值的方法(方法声明时的类型、return后接的数据)
2.方法的重载(概念、条件、好处、应用场景)
3.方法的递归(概念、找出口、找规律)
4.方法版本的万年历
5.一维数组
作业:
1.知识点的梳理文档(方法.md、数组.md)
2.代码编写3遍
3.画内存图(手画2遍、拍照发到QQ群里)
4.课后作业
5.复习:上周所有内容
6.预习:面相对象