循环以及一些对象的浅谈

对象

理解

可以这样来理解,当客户提出需求,比如想要一个电脑功能,你需要根据电脑的功能,去造一些东西,这些东西就是你可以使用的材料,比如变量,常量,静态量你可以根据这些东西去搭好框架也就是方法或者构造器,你可以使用这些东西来解决提出的功能,这就是对象

运用:通过new来创建

循环

理解:就是有规律的重复,

思维: 由繁到简,一步一步简化,找到规律再去优化代码

重点:在企业中一定不能使用嵌套循环,应使用方法,方便调用

四要素
  1. 初始表达式
  2. 条件表达式
  3. 循环体
  4. 迭代因子

简单的循环 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");
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值