Javaseday04

xiaoxiao2025-10-09  5

第四章 循环语句

4.1 while循环

while语句

while(循环继续条件){ //循环体 语句(组); }

循环条件总是放在圆括号内。只有当循环体只包含一条语句或不包含语句时,循环体的花括号才可以省略。

代码如下:

public static void main(String[] args){ //1.生成随机数 int number=(int)(Math.random()*101); System.out.println("Guess a magic number between 0 and 100:"); Scannaer scanner=new Scanner(System.in); //获取用户输入的值 //循环初始化 System.out.print("Enter you guess:"); int num=scanner.nextInt(); while(num!=number){//循环条件 if(num<number){ System.out.println("too low!"); }else{ System.out.println("too hight"); } System.out.print("Enter you guess:"); int num=scanner.nextInt(); } System.out.println("Yes the number is "+number); }

4.2 do-while循环

do-while语句

do { //循环体; 语句(组); } while (循环继续条件);

4.3 for循环

for语句

for(初始操作;循环继续条件;每次迭代后的操作){ //循环体 语句(组); }

for嵌套语句

示例:预测未来学费 假设某个大学今年的学费是10000美元,而且以每年7%的速度增加。多少年后学费会翻倍? 代码:

public static void main(String[] args){ double tuition=10000;// year=0 int year=0; while(tuituion<20000){ tuiution=tuition*1.07; year++; } System.out.print("Tuition will be double in "+year+" year"); }

代码:

public static void main(String[] args){ Scanner scanner=new Scanner(System.in); //提示用户输入一个十进制数 System.out.print("Enter a decimal number: "); int number=input.next.Int(); String hex=" "; while(number !=0){ int hexValue=number%16; char hexDidit=(hexValue<=9&&hexValue>=0)?(char)(hexValue+'0'):(char)(hexValue-10+'A'); hex=hexDight+hex; number=number/16; } System.out.println("The hex number is "+hex); }

4.4 嵌套循环

* 示例:打印直角三角形 代码: public static void main(String[] args){ /* * * * ** * *** * **** * ***** */ for(int i=1;i<=5;i++){ for(int j=1;j<=i;j++){ System.out.print("*"); } System.out.println(); } } * 示例:打印菱形 代码: public static void main(String[] args){ for(int i=1;i<=7;i++){ for(int j=1;j<=1-i;j++){ System.out.print(""); } for(int j=8-i;j>=1;j--){ System.out.print("*"); } System.out.println(); } } * 示例:打印空心菱形 代码: public static void main(String[] args) { for(int i=1;i<=9;i++){ for(int k=1;k<=Math.abs(i-5);k++){ System.out.print(" "); } for(int j=1;j<=i&&(i+j)<=10;j++){ if(j==1||j==i||j+i==10){ System.out.print("* "); }else{ System.out.print(" "); } } System.out.println(); } }

4.5 break和在这里插入代码片continue

break语句 跳出整个循环

continue语句 跳出循环当前迭代 continue语句总是在一个循环内。在while和do-while循环中,continue语句之后马上计算循环继续条件;而在for循环中,continue语句之后会立即先执行每次迭代后的动作,再计算循环继续条件。

示例:显示素数 分五行显示前50个素数,每行包括10个数字。 代码:

public static void main(String[] args){ int count=0;//计算素数的个数 int number=1; while(count<=50){//计算素数 boolean flag=true; for(int i=2;i<=number/2;i++){ if(number%i==0){ flag=false; break; } } if(flag){ count++; System.out.print(number+" "); if(count%10==0){ System.out.println(); } } number++; }

4.6 函数的重载

在同一类中 如有同名函数 则称之为函数之间为重载关系 重载的前提是 函数重名 与 修饰符 返回值类型 参数列表没有关系 仅仅和参数列表中 参数类型的排列组合有关系

本章小结

1、循环语句有三类:while循环、do-while循环和for循环。 2、循环中包含重复执行的语句的部分称为循环体。 3、循环体执行一次称为循环的一次迭代。 4、无限循环是指循环语句被无限执行。 5、在设计循环时,既需要考虑循环控制结构,还需要考虑循环体。 6、while循环首先检查循环继续条件。如果条件为true,则执行循环;如果条件为false,则循环结束。 7、do-while循环与while循环类似,只是do-while循环先执行循环体;然后再检查循环继续条件,以确定是继续还是终止。 8、while和do-while循环常用于循环次数不确定的情况。 9、标记值是一个特殊的值,用来标记循环的结束。 10、for循环一般用在循环体执行次数固定的情况。 11、for循环控制由三部分组成。第一部分是初始操作,通常用于初始化控制变量。第二部分是循环继续条件,决定是否执行循环体。第三部分是每次迭代后执行的造作,经常用于调整控制变量。通常,在控制结构中初始化和修改循环控制变量。 12、while循环和for循环都称为前测循环(pretest loop),因为在循环体执行之前,要检测一下循环继续条件。 13、do-while循环称为后侧循环(postteat loop),因为在循环体执行之后,要检测一下这个条件。 14、在循环中可以使用break和continue这两个关键字。 15、在关键字break立即终止包含break的最内层循环。 16、关键字continue只是终止当前迭代。

转载请注明原文地址: https://www.6miu.com/read-5037632.html

最新回复(0)