Re:if java for

en…为什么要提if呢,if(1>0){恒成立的问题就没必要非得用if来控制节能减排了,省点代码量很重要啊。}

  • A:流程控制语句的分类
    • 顺序结构
    • 选择结构
    • 循环结构
  • B:执行流程:
    • 从上往下,依次执行。

列道if的程序:

class Test1_If {
public static void main(String[] args) {		
	Scanner sc = new Scanner(System.in);
	//需求:键盘录入一个成绩,判断并输出成绩的等级。
	System.out.println("请输入学生成绩范围在1到100之间");
	int x = sc.nextInt();
	if (x >= 90 && x <= 100) {
		System.out.println("优");
	}else if (x >= 80 && x <= 89 ) {
		System.out.println("良");
	}else if (x >= 70 && x <= 79 ) {
		System.out.println("中");
	}else if (x >= 60 && x <= 69 ) {
		System.out.println("及");
	}else if (x >= 0 && x <= 59 ) {
		System.out.println("差");
	}else {
		System.out.println("成绩录入错误");
	}
   //可以扩展一下
   /*
    if (x>100 || x<0) {
		System.out.println("成绩不在0-100之间");
	} else if (x>=90) {
		System.out.println("优");
	} else if (x>=80) {
		System.out.println("良");
	} else if (x>=70) {
		System.out.println("中");
	} else if (x>=60) {
		System.out.println("及");
	} else {
		System.out.println("差");
	}
    */
    /*
    if (x>100 || x<0) {
		System.out.println("成绩不在0-100之间");
	} else if (x<60) {
		System.out.println("差");
	} else if (x<70) {
		System.out.println("及");
	} else if (x<80) {
		System.out.println("中");
	} else if (x<90) {
		System.out.println("良");
	} else  {
		System.out.println("优");
	}
   */
	}
}	

if,else if,else问题解决了之后就没什么在意的了,毕竟还有for循环来增加代码量和难度,关注点一下子就会被其难搞的逻辑问题绕进去,装饰上if语句后——该打Java初级的第一个boos了。
当然不可能直接打boos,先得清一下小怪,每个case值在魔王的左右手switch带领下迎接你的挑战。
星期问题就是其很常见的攻击手段

class Test1_Switch {
public static void main(String[] args) {
	//* A:整数(给定一个值,输出对应星期几)
	int week = 1;
	switch (week) {
	case 1:
		System.out.println("星期一");
	break;
	case 2:
		System.out.println("星期二");
	break;
	case 3:
		System.out.println("星期三");
	break;
	case 4:
		System.out.println("星期四");
	break;
	case 5:
		System.out.println("星期五");
	break;
	case 6:
		System.out.println("星期六");
	break;
	case 7:
		System.out.println("星期日");
	break;
	default:
		System.out.println("对不起没有对应的星期");
	break;
		}
	}
}

没有碰到case穿透问题,应该是有装备爆出来的,被称之为逻辑的老师,他会告诉你原本的魔王是一个心地善良的人…直到你要打败他。好的过场结束,上魔王:

class Test1_For {	//简单模式
public static void main(String[] args) {
	//1-10的和
	/*int sum = 0;
	for (int i = 1;i <= 10 ;i++ ) {
		sum = sum + i;
	}

	System.out.println("sum = " + sum);*/

	//1-100的偶数和
	/*int sum = 0;
	for (int i = 1;i <= 100 ;i++ ) {
		if (i % 2 == 0) {
			sum = sum + i;
		}
	}

	System.out.println("sum = " + sum);*/

	//1-100的奇数和
	int sum = 0;
	for (int i = 1;i <= 100 ;i+=2 ) {
		/*if (i % 2 != 0) {
			sum = sum + i;
		}*/
		sum = sum + i;
	}

	System.out.println("sum = " + sum);
	}
}

在经过三玖天下第一攻击和人类的本质攻击后,for循环待机休息了,当然还没有结束,只是for循环在攒大招的怒气。

class Test2_Flower {
public static void main(String[] args) {
	for (int i = 100;i <= 999 ;i++ ) {					//获取100到999之间的数
		int ge = i % 10;								//123 % 10 
		int shi = i / 10 % 10;							//12 % 10;
		int bai = i / 10 / 10 % 10;						//1 % 10

		if (ge * ge * ge + shi * shi * shi + bai * bai * bai == i) {
			System.out.println(i);
			}
		}
	}
}
//试探二
class Test3_FlowerCount {
public static void main(String[] args) {
	int count = 0;

	for (int i = 100;i <= 999 ;i++ ) {
		int ge = i % 10;
		int shi = i / 10 % 10;
		int bai = i / 10 / 10;

		if (i == ge * ge * ge + shi * shi * shi + bai * bai * bai) {
			count ++;													//满足条件就自增,计数器思想
		}
	}

	System.out.println(count);
	}
}

喜闻乐见的水仙花数,没什么难度,但在这时,for循环变了个样子,成了惹不起…咳咳,他进入了无限循环中,只要特殊的条件成立是很容易变成死循环的。
暂时for循环没有危险了,就在这时,while跳了出来,他举着do就向你扔了过来,不论如何,do都被执行了一次,while仍完do在break的帮助下撒腿就跑…

class Test1_While {
public static void main(String[] args) {
	/*
	* A:求和思想
		* 求1-100之和
	* B:统计思想
		* 统计”水仙花数”共有多少个
	*/
	
	//求1-100之和
	/*int sum = 0;
	int i = 1;
	while (i <= 100) {
		sum += i;					//sum = sum + i;
		i++;						//让变量i自增
	}

	System.out.println("sum = " + sum);*/

	//统计”水仙花数”共有多少个
	int count = 0;					//计数器
	int i = 100;
	while (i <= 999) {
		int ge = i % 10;
		int shi = i / 10 % 10;
		int bai = i / 100;

		if (i == ge * ge * ge + shi * shi * shi + bai * bai * bai) {
			count ++;
		}

		i++;
	}

	System.out.println("count =" + count);

	//某屌丝为了追求女神,写了一段代码示爱,但是女神也会java,改动一下把屌丝拒绝
	int j = 1;
	//while (j <= 10000) ;{
while (j <= 10000) {
		System.out.println("I Love You!!!");
		j++;
		}
	}
}

这时候得整理一下他们的关系了

  • 三种循环语句的区别:
    • do…while循环至少执行一次循环体。
    • 而for,while循环必须先判断条件是否成立,然后决定是否执行循环体语句。
  • for循环和while循环的区别:
    • 如果你想在循环结束后,继续使用控制条件的那个变量,用while循环,否则用for循环。不知道用谁就用for循环。因为变量及早的从内存中消失,可以提高内存的使用效率。

“噢噢噢噢!”忽地for循环开始了超级赛亚人变身,头发长了(程序员的不可言语),身体进入了青春期…

class Demo_99 {
public static void main(String[] args) {
	for(int i=1;i<=9;i++){
		for(int j=1;j<=i;j++){
			System.out.print(j+"*"+i+"="+j*i+"\t");
		}
		System.out.println();
		}
	
	}
}

九九乘法表攻击啊。。。

class Demo3_For99 {
public static void main(String[] args) {
	/*for (int i = 1;i <= 9 ;i++ ) {					//行数
		for (int j = 1;j <= i ;j++ ) {				//列数
			System.out.print("*" );
		}
		System.out.println();
	}*/

	//System.out.println("\"");				转义双引号
	System.out.println('\'');				//转义单引号
	}
}

满天三角星攻击啊。。。
主角卒,明日续。
ps:朴素贝叶斯。。。
青玉案·东风夜放拟做
  西山挂斗车前路,那停了、千秋树。绿草争芳花且住。流光萤火,凤来朝木,尽惹蛟龙妒。
  小门东去凌波步,寻柳稀稀故间入。室内寻她千百度。忽然回目,把书轻负,人不知何处。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值