Java篇--Java语言的基本语法3

本文详细介绍了Java中的分支结构(包括switch语句)和循环结构(for、while、do-while),并通过多个实例展示了这些结构的具体应用。此外,还探讨了break和continue等特殊流程控制语句的使用方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

分支结构2switch语句


/*
switch(变量){
	case 1:
	case 值2:

      break
	case 值3:
              
	default:
      

}
1.根据变量的值,选择相应的case去判断,一旦满足case条件,就执行case的相应语句。如果没有break或者已经
到结尾的话,会继续执行其下的case语句。
2.default:是可选的,而且位置是灵活的。
3.变量可以是哪些类型?char byte short int 枚举 String(jdk1.7)
4.case 条件:其中条件只能是值,不能是取值范围!
*/
class TestSwitch1 {
	public static void main(String[] args) {
		int i = 2;
		switch(i){
			
			case 0:
				System.out.println("zero");
				break;
			case 1:
				System.out.println("one");
				break;
			case 2:
				System.out.println("two");
				break;
			case 3:
				System.out.println("three");
				break;
			default:
				System.out.println("other");
				break;
		}
		/*
		switch(i){
			case (i > 2):
				System.out.println("a");
				break;
			case (i > 5):
				System.out.println("b");
				break;
			default:
				System.out.println("c");
				break;
		}
		*/
	}
}


switch语句应用举例

class TestSwitch2{
	public static void main(String[] args) {
		
		String season = "SPRING";

		switch(season){
			case "SPRING":
				System.out.println("春天");
				break;
			case "SUMMER":
				System.out.println("夏天");
				break;
			case "AUTUMN":
				System.out.println("秋天");
				break;
			case "WINTER":
				System.out.println("冬天");
				break;
			default:
				System.out.println("输入有误");
				break;
		
		}	
		
	}
}


编写程序:从键盘上输入2016年的“month”和“day”,要求通过程序输出输入的日期为2016年的第几天。

/*
编写程序:从键盘上输入2014年的“month”和“day”,要求通过程序输出输入的日期为2014年的第几天
*/
import java.util.Scanner;
class TestSwitch4{
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		System.out.println("请输入月份:");
		int month = s.nextInt();
		System.out.println("请输入日期:");
		int day = s.nextInt();

		int sum = 0;//用来记录month月day日是2016年第几天
		//使用switch-case
		switch(month){
		
		case 12:
			sum += 30;//30:十一月份的总天数
		case 11:
			sum += 31;
		case 10:
			sum += 30;
		case 9:
			sum += 31;
		case 8:
			sum += 31;
		case 7:
			sum += 30;
		case 6:
			sum += 31;
		case 5:
			sum += 30;
		case 4:
			sum += 31;
		case 3:
			sum += 28;
		case 2:
			sum += 31;//31:是一月份的天数
		case 1:
			sum += day;
	
		}



		System.out.println(sum);
	}
}



循环结构

循环语句功能

  在某些条件满足的情况下,反复执行特定代码的功能

循环语句的四个组成部分

  1初始化部分(init_statement

  2)循环条件部分(test_exp

  3)循环体部分(body_statement

  4)迭代部分(alter_statement

循环语句分类

  for 循环

  while 循环

  do/while 循环 


for循环


/*
for循环结构:
1.格式:
①初始化条件
②循环条件
③迭代条件
④循环体
for(①;②;③){
	//④
}
2.执行过程:①-②-④-③-②-④-③-。。。-④-③-②
   即直至循环条件不满足,退出当前的循环
*/
class TestFor{
	public static void main(String[] args) {

		System.out.println("Hello World!" );
		System.out.println("Hello World!" );
		System.out.println("Hello World!" );
		System.out.println("Hello World!" );
		
		System.out.println();

		for(int i = 0;i < 4;i++){
			System.out.println("Hello World!" );
		}
		//System.out.println(i);
		int j = 1;
		for(System.out.print('a');j < 3;System.out.print('b'),j++){
			System.out.print('c');
		}
		//acbcb
		System.out.println();
		//题目:输出100以内的所有偶数及所有偶数的和(累加的思想)及偶数的个数
		int sum = 0;//用来记录所有偶数的和
		int count = 0;
		for(int i = 1;i <= 100;i++){//100以内的自然数的遍历
			if(i % 2 == 0){
				System.out.println(i);
				sum += i;
				count++;
			}
		}
		System.out.println("总和为:" + sum);
		System.out.println("100以内偶数的个数为:" + count);
	}
}


练习:输出所有的水仙花数,所谓水仙花数是指一个3

   位数,其各个位上数字立方和等于其本身。

    例如: 153 = 1*1*1 + 3*3*3 + 5*5*5

/*
输出所有的水仙花数,所谓水仙花数是指一个3
   位数,其各个位上数字立方和等于其本身。
    例如: 153 = 1*1*1 + 3*3*3 + 5*5*5
*/
class ShuiXianHua{
	public static void main(String[] args) {
		
		for(int i = 100;i < 1000;i++){//实现100-999的遍历  456 - 400 = 56 - 50
			int j1 = i / 100;//百位
			int j2 = (i - j1*100)/10;//十位
			int j3 = i % 10;//个位

			if(i == j1*j1*j1 + j2*j2*j2 + j3*j3*j3){
				System.out.println(i);
			}
		}
	}
}


While循环

语法格式

  [初始化语句]

while( 布尔值测试表达式)

        语句或语句块;

[更改语句;]

}

While案例:

/*
①初始化条件
②循环条件
③迭代条件
④循环体
1.格式:
①
while(②){
	④
	③
}
2.for循环与while可以相互转换。
*/
class TestWhile{
	public static void main(String[] args) {
		//100以内的偶数的输出,及其和
		int i = 1;
		int sum = 0;
		while(i <= 100){
			if(i % 2 == 0){
				System.out.println(i);
				sum += i;
			}
			i++;
	
		}
		System.out.println(sum);
		//System.out.println(i);
	}
}


do-while 循环语句

语法格式

[初始化语句]

do

        语句或语句块;

        [更改语句;]

while(布尔值测试表达式); 

/*
①初始化条件
②循环条件
③迭代条件
④循环体

1.格式:
①
do{
	④
	③
}while(②);

2.do-while与while的区别:do-while循环至少会执行一次!
*/

class TestDoWhile{
	public static void main(String[] args) {
		int i = 1;
		do{
			if(i % 2 == 0){
				System.out.println(i);
			}
			i++;
		}while(i <= 100);
		
		
		//do-while与while的区别
		int j = 10;
		do{
			System.out.println(j);
			j++;
		}while(j < 10);


		while(j < 10){
			System.out.println(j);
			j++;
		}
	}
}


嵌套循环

将一个循环放在另一个循环体内,就形成了嵌套循环。其中,for ,while ,dowhile均可以作为外层循环和内层循环。

案例:

/*
嵌套循环:循环中还可以声明循环。相当于内层循环的整体充当外层循环的循环体
for(;;){
	for(;;){
		
	}
}

或者
while(){
	for(;;){}
}
*/

class TestForFor{
	
	public static void main(String[] args) {
		/*
		*****
		*****
		*****
		*****
		*/
		for(int j = 0;j < 4;j++){//外层循环控制行数
			for(int i = 0;i < 5;i++){//内层循环控制列数
				System.out.print("*");
			}
			System.out.println();
		}


/*
		*
		**
		***
		****
		*/
		for(int j = 0;j < 4;j++){//外层循环控制行数
			for(int i = 0;i < j + 1;i++){//每行输出j + 1个
				System.out.print("*");
			}
			System.out.println();
		}
		/*
		*****
		****
		***
		**
		*
		*/
		for(int i = 0; i < 5;i++){
			for(int j = 0;j < 5-i;j++){//for(int j = i;j < 5;j++)
				System.out.print("*");
			}
			System.out.println();
		}

		/*
		*
		**
		***
		****
		*****
		****
		***
		**
		*
		*/
		for(int j = 0;j < 4;j++){//外层循环控制行数
			for(int i = 0;i < j + 1;i++){//每行输出j + 1个
				System.out.print("*");
			}
			System.out.println();
		}
		for(int i = 0; i < 5;i++){
			for(int j = 0;j < 5-i;j++){//for(int j = i;j < 5;j++)
				System.out.print("*");
			}
			System.out.println();
		}



/*
1 * 1 = 1
2 * 1 = 2  2 * 2 = 4
...
9 * 1 = 9   ....    9 * 9 = 81
*/

class TestJiuJiu{
	public static void main(String[] args){
		for(int i = 1;i <= 9;i++){//一共有9行
			for(int j = 1;j <= i;j++){//每行有i个等式
				System.out.print(i + "*" + j + "=" + i*j + "\t");
			}
			System.out.println();
		}
	}
}


特殊流程控制语句1

break 语句

 

break语句用于终止某个语句块的执行

  {    …… 

     break;

     ……

}

break 语句用法举例

 public class TestBreak{

public static void main(String args[]){

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

     if(i==3)

      break;

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

    }

    System.out.println("Game Over!");

}

}

 

 

continue 语句

continue语句用于跳过某个循环语句块的一次执行

continue语句出现在多层嵌套的循环语句体中时,可以通过标签指明要跳过的是哪一层循环

 

continue语句用法举例

public class ContinueTest {

        public static void main(String args[]){

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

                 if (i%10==0)

        continue;

                         System.out.println(i);

                        }  }  } 


/*
break:使用在switch-case中或者循环中
如果使用在循环中,表示:结束"当前"循环

continue:使用在循环结构中
表示:结束"当次"循环

关于break和continue中标签的使用。(理解)
*/
class TestBreakContinue{
	public static void main(String[] args) {
		/*
		for(int i = 1;i <= 10;i++){
			if(i % 4 == 0){
				//break;
				continue;
			}

			System.out.println(i);
		}
		*/
		for(int i = 1;i < 5;i++){
			for(int j = 1;j <= 10;j++){
				if(j % 4 == 0){
					//break;
					//continue;
					//break label;
					continue;
				}
				System.out.print(j);
			}
			System.out.println();
		}

		System.out.println();
		for(int i = 1;i <= 10;i++){
			if(i % 4 == 0){
				break;
				//continue;
				//在break和continue语句之后不能添加其他语句,应为永远也不可能被执行!
				System.out.println("林志玲晚上要约我");
			}

			System.out.println(i);
		}
	}
}

break用法:

break的使用场景:

    在选择结构switch语句中

    在循环语句中

    离开使用场景的存在是没有意义的

break的作用:

    跳出单层循环

    跳出多层循环

    带标签的跳出

        格式:标签名: 循环语句

        标签名要符合Java的命名规则

/*
	控制跳转语句:
		break:中断
		continue:继续
		return:返回
	
	break:中断的意思
	使用场景:
		A:switch语句中
		B:循环语句中。
			(循环语句中加入了if判断的情况)
		注意:离开上面的两个场景,无意义。
		
	如何使用呢?
		A:跳出单层循环
		B:跳出多层循环
			要想实现这个效果,就必须知道一个东西。带标签的语句。
			格式:
				标签名: 语句
*/
class BreakDemo {
	public static void main(String[] args) {
		//在 switch 或 loop 外部中断
		//break;
		
		//跳出单层循环
		for(int x=0; x<10; x++) {
			if(x == 3) {
				break;
			}
			System.out.println("HelloWorld");
		}
		
		System.out.println("over");
		System.out.println("-------------");
		
		wc:for(int x=0; x<3; x++) {
			nc:for(int y=0; y<4; y++) {
				if(y == 2) {
					//break nc;
					break wc;
				}
				System.out.print("*");
			}
			System.out.println();
		}
	}
}

跳转控制语句(continue)

ontinue的使用场景:

      在循环语句中

      离开使用场景的存在是没有意义的

continue的作用:

      单层循环对比break,然后总结两个的区别

      break  退出当前循环

      continue  退出本次循环


break只能用于switch语句和循环语句中。

continue 只能用于循环语句中。

二者功能类似,但continue是终止本次循环,break是终止本层循环。

breakcontinue之后不能有其他的语句,因为程序永远不会执行其后的语句。

标号语句必须紧接在循环的头部。标号语句不能用在非循环语句的前面。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值