对象
理解
可以这样来理解,当客户提出需求,比如想要一个电脑功能,你需要根据电脑的功能,去造一些东西,这些东西就是你可以使用的材料,比如变量,常量,静态量你可以根据这些东西去搭好框架也就是方法或者构造器,你可以使用这些东西来解决提出的功能,这就是对象
运用:通过new来创建
循环
理解:就是有规律的重复,
思维: 由繁到简,一步一步简化,找到规律再去优化代码
重点:在企业中一定不能使用嵌套循环,应使用方法,方便调用
四要素
- 初始表达式
- 条件表达式
- 循环体
- 迭代因子
简单的循环 if whlie do{}whlie();
public class MyTest1{
/*主入口*/
public static void main(String[] args){
for(int i=0;i<10;i++){
System.out.println(i+1);
}
int b=0;
while(b<10){
System.out.println(b+1);
b++;
}
int c =0;
do{
System.out.println(c+1);
c++;
}while(c<10);
}
}
由繁到简的推理去做简化
/*System.out.println("helloworld....");
System.out.println("helloworld....");
System.out.println("helloworld....");
*/
/*
//加入计数器
int count = 0;
System.out.println("helloworld....");
count ++;
System.out.println("helloworld....");
count ++;
System.out.println("helloworld....");
count ++;
if(count ==2){
return ;
}
*/
/* //加入了计数器+判断
int count = 0;
if(count<3){
System.out.println("helloworld....");
count ++;
}
if(count<3){
System.out.println("helloworld....");
count ++;
}
if(count<3){
System.out.println("helloworld....");
count ++;
}*/
int count = 0; //初始表达式
while(count<3 /*条件判断*/){
System.out.println("helloworld...."); //循环体
count ++; //迭代因子 改变循环条件
}
System.out.println("----------------");
for(int n=0;n<3;n++){
System.out.println("helloworld....");
}
System.out.println("----------------");
int m =0;
do{
System.out.println("helloworld....");
m++;
}while(m<3);
九九乘法表
当你需要实现某个功能时,你需要使用嵌套循环,就比如九九乘法表,一般初学者都是使用嵌套循环。但在企业,不方便理解,以及以后维护,这里时候你可以把一些规律简化到方法里面后在mian方法里面用一层循环调用另一个循环,这样代码的可维护性就好了很多
企业中推荐使用
package com.liuyi.Dome01;
public class Test02 {
/*主入口*/
public static void main(String[] args) {
for(int i=1;i<10;i++) {
mul(i);
}
}
/*九九乘法表 表里面的*/
public static void mul(int num) {
for(int i=1;i<=num;i++) {
System.out.print(i+"*"+num+"="+i*num+"\t");
}
System.out.print("\n");
}
}
break
字面上理解就是结束
用法:在一个循环体中,你想让它到达一定的条件就结束整个循环这时候就可以使用到break,它在中的字面理解为防止向下穿透,也就是跳出循环
for(int i=0;i<10;i++){
if(i==5){
break;
}
}// 结果为到5就不执行直接跳出循环了
continue
字面上的理解为继续循环
然在java中它是指中止本次循环,继续下一次循环,也就是说这一个循环他不做了但是她有没有跳出这个循环,而是接着做这个循环的下一次循环
for(int i=0;i<10;i++){
if(i==5){
continue;
}
}//这个结果可以为 0 1 2 3 4 6 7 8 9
扩展:
break 与return 的区别
break表示跳出当前循环但是不结束这个函数,也就是说这个循环我结束了但是循环体下的其他的东西我还是执行的,
但是return 就不一样了,他表示终止整个函数向函数返回一个值,当表达之为空则终止被调函数
也就是说,当你在一函数中你用return ,它的后面的语句是不会执行的,他是结束了整个函数,如果有参数他就是返回这个参数