java循环结构 for if,顺序结构,选择结构(if,switch)循环结构(for,while,do while),死循环...

程序的结构:

顺序结构,选择结构,循环结构

顺序结构:

AAffA0nNPuCLAAAAAElFTkSuQmCC

选择结构: if:

package com.newedu.jb.day02;

import java.util.Scanner;

/**

* 选择结构

* if

*

* 三种格式:

*

* @author 小可爱

*

*/

public class ChooseDemo {

public static void main(String[] args) {

/**

* 第一种if格式

* if(关系表达式){

//code block,可以是单行语句,也可以是多行语句

}

*/

int a = 10;

int b = 20;

if(a>b){//关系表达式的值为true,才执行下面的代码块

System.out.println("最大值是:"+a);

}

/**

* 第二种if else格式

*

* if(关系表达式){

//code block,可以是单行语句,也可以是多行语句

}else{

//code block,可以是单行语句,也可以是多行语句

}

*/

int x = 1;

int y = 2;

if(x>y){

//code block,可以是单行语句,也可以是多行语句

System.out.println("最大值:"+x);

}else{

//code block,可以是单行语句,也可以是多行语句

System.out.println("最大值:"+y);

}

/**

* 第三种if else if ... esle格式

*

* if(关系表达式1){

//code block,可以是单行语句,也可以是多行语句

}else if( 关系表达式2){

//code block,可以是单行语句,也可以是多行语句

}

else if( 关系表达式2){

//code block,可以是单行语句,也可以是多行语句

}

...

else{

//code block,可以是单行语句,也可以是多行语句

}

*

*/

Scanner sc = new Scanner(System.in);

System.out.println("输入分数:");

int score = sc.nextInt();

if(score>90&& score <=100){

//code block,可以是单行语句,也可以是多行语句

System.out.println("优秀");

}else if( score>80&& score <=90){

//code block,可以是单行语句,也可以是多行语句

System.out.println("良好");

}

else if( score>70&& score <=80){

//code block,可以是单行语句,也可以是多行语句

System.out.println("中等");

}

else{

//code block,可以是单行语句,也可以是多行语句

System.out.println("继续努力吧,少年");

}

sc.close();

}

}

switch:

package com.newedu.jb.day02;

import java.util.Scanner;

/**

* Switch 选择结构

*

* 用switch语句实现键盘录入月份,输出对应的季节

* @author 小可爱

*

*/

public class ChooseDemo2 {

public static void main(String[] args) {

//ctrl+shift +o :自动导入确实的包

Scanner sc = new Scanner(System.in);

System.out.println("请输入月份");

int month = sc.nextInt();

switch(month){

default:

//code blcok

System.out.println("输入有误!");

break;

case 12:

//code block

// System.out.println("大约是冬季");

// break;

case 1:

//code block

// System.out.println("大约是冬季");

// break;

case 2:

//code block

System.out.println("大约是冬季");

break;

case 3:

//code block

System.out.println("大约是春季");

break;

case 4:

//code block

System.out.println("大约是春季");

break;

case 5:

//code block

System.out.println("大约是春季");

break;

case 6:

//code block

System.out.println("大约是夏季");

break;

case 7:

//code block

System.out.println("大约是夏季");

break;

case 8:

//code block

System.out.println("大约是夏季");

break;

case 9:

//code block

System.out.println("大约是秋季");

break;

case 10:

//code block

System.out.println("大约是秋季");

break;

case 11:

//code block

System.out.println("大约是秋季");

break;

}

sc.close();

}

}

循环结构

for

格式

AAffA0nNPuCLAAAAAElFTkSuQmCC

for循环练习

package com.newedu.jb.day03;

/**

* 演示for循环:

*

* for格式:

* for(初始化语句;判断条件语句;控制条件语句){

* 循环体语句(code block)可以是单行,也可以是多行

* }

*

* 执行顺序:

* A:执行初始化语句

* B:判断条件语句

* 1:如果执行结果为false,结束循环

* 2:如果执行结果为true,继续执行C

* C:执行循环体语句

* D:控制条件语句

* E:回到B重复执行。

* @author 小可爱

*

*/

public class ForDemo {

public static void main(String[] args) {

//请在控制台输出数据1-10

for(int i=1;i<=10;i++){

System.out.print(i+"\t");

}

System.out.println();

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

//请在控制台输出数据10-1

for(int i=10;i>=1;i--){

System.out.print(i+"\t");

}

System.out.println();

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

int sum =0;

//求出1-10之间数据之和

for(int i=1;i<=10;i++){

sum +=i;

}

System.out.println("1-10之间数据之和:"+sum);

//求出1-100之间偶数和

/**

* 分析一下

*/

sum = 0;

for(int i=2;i<=100;i=i+2){

//1,2,3,4,5,6,7,8,9,10

sum +=i;

}

System.out.println("1-100之间偶数和:"+sum);

//求出1-100之间奇数和

sum = 0;

for(int i=1;i<=100;i=i+2){

//1,2,3,4,5,6,7,8,9,10

sum +=i;

}

System.out.println("1-100之间奇数和:"+sum);

int sum1=0;

int sum2=0;

for(int i=1;i<=100;i++){

if(i%2==0){//偶数,2,4,6,8....

sum1 +=i;

}else{

sum2+=i;//奇数,1,3,5,7....

}

}

//求5的阶乘5*4*3*2*1

sum =1;

for(int i=5;i>=1;i--){

sum *=i;

}

System.out.println("5的阶乘:"+sum);

}

}

package com.newedu.jb.day03;

/**

* for循环练习题

* 请在控制台输出满足如下条件的五位数10000 99999

* 个位等于万位 12345 first = 12345%10 sencond = 1234%10

* 十位等于千位

* 个位+十位+千位+万位=百位

*

* 分析:

* num = 12345

* first = num%10

* num /=10; //1234

* second = num%10

* num /=10;

* ....

*

* 结束条件

* num = 0;就结束

* @author 小可爱

*

*/

public class ForDemo1 {

public static void main(String[] args) {

// String s = "12345";

//

// char f1 = s.charAt(0);

// char f5 = s.charAt(4);

// f1.equals(f5);

String str1 = "str";

String str2 = "str";

str1.equals(str2);

// int num = 12345;

// int first = num%10;

// num /=10;

// int second = num%10;

// num /=10;

// int third = num%10;

// num /=10;

// int fourth = num%10;

// num/=10;

// int fifth = num%10;

//

// System.out.println("first:"+first);

// System.out.println("second:"+second);

// System.out.println("third:"+third);

// System.out.println("fourth:"+fourth);

// System.out.println("fifth:"+fifth);

for(int i=10000;i<=99999;i++){

int num = i;

int first = num%10;

num /=10;

int second = num%10;

num /=10;

int third = num%10;

num /=10;

int fourth = num%10;

num/=10;

int fifth = num%10;

//ctrl+d :删除当前光标所在行

if(first == fifth &&

second == fourth &&

first+second+fourth+fifth == third ){

System.out.println(i);

}

}

}

}

while

格式

AAffA0nNPuCLAAAAAElFTkSuQmCC

while循环练习:

package com.newedu.jb.day03;

/**

* while循环

*

* 格式:

* 基本格式

while(判断条件语句) {

循环体语句;

}

扩展格式

初始化语句;

while(判断条件语句) {

循环体语句;

控制条件语句;

}

* @author 小可爱

*

*/

public class WhileDemo {

public static void main(String[] args) {

//输出1-10的数到控制台

// int i=1;

// while(i<=10){

// System.out.print(i+"\t");

// i++;

// }

int j=10000;

while(j<=99999){

int num = j;

int first = num%10;

num /=10;

int second = num%10;

num /=10;

int third = num%10;

num /=10;

int fourth = num%10;

num /=10;

int fifth = num%10;

//ctrl+d :删除当前光标所在行

if(first == fifth &&

second == fourth &&

first+second+fourth+fifth == third ){

System.out.println(j);

}

j++;

}

}

}

for 与 while的区别

AAffA0nNPuCLAAAAAElFTkSuQmCC

do while

AAffA0nNPuCLAAAAAElFTkSuQmCC

do while练习:

package com.newedu.jb.day03;

/**

* do while 循环

*

* @author 小可爱

*

*/

public class DoWhileDemo {

public static void main(String[] args) {

// 输出1-10之间的数到控制台

int i = 1;

do {

System.out.print(i + "\t");

i++;

} while (i <= 0);

while(i<=0){

System.out.print(i + "\t");

i++;

}

}

}

死循环:

while(true){}

for(;;){}

break,continue,return 关键字

break;//跳出当前循环

continue;//跳出当前循环,继续下一个循环

return;//跳出当前的方法执行,直接返回到调用者

总结:

三种循环语句其实都可以完成一样的功能,也就是说可以等价转换,但还是有小区别的:

do…while循环至少会执行一次循环体。

for循环和while循环只有在条件成立的时候才会去执行循环体

嵌套循环:

练习:

package com.newedu.jb.day03;

/**

* 循环结构(嵌套循环的练习)

*

* @author 小可爱

*

*/

public class LoopDemo {

public static void main(String[] args) {

//请输出一个4行5列的星星(*)图案

// System.out.println("*****");

// System.out.println("*****");

// System.out.println("*****");

// System.out.println("*****");

/*for(int i=0;i<=4;i++){ // 循环 控制打印一行*

System.out.print("*");

}

System.out.println();

for(int i=0;i<=4;i++){ // 循环 控制打印一行*

System.out.print("*");

}

System.out.println();

for(int i=0;i<=4;i++){ // 循环 控制打印一行*

System.out.print("*");

}

System.out.println();

for(int i=0;i<=4;i++){ // 循环 控制打印一行*

System.out.print("*");

}*/

for(int j=0;j<=3;j++){//外循环 控制打印多少行

for(int i=0;i<=4;i++){ // 内循环 控制打印一行中列(*)

System.out.print("*");

}

System.out.println();

}

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值