java打印从1到100的值(break,return断句)

2025-05-29 0 112

首先来讲这个没有什么难点,就是分析下breakreturn的效果有什么不一样,通过最后的打印结果可以看出:

1、break只是跳出了循环会继续执行函数内、循环外的代码。
2、return是直接函数返回了,循环内和函数内的后面的代码都不会在执行了。

代码:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51
package com.itheima;

/**

* 8、 先写一个程序,打印从1到100的值。之后修改程序,通过使用break关键词,使得程序在打印到98时退出。然后尝试使用return来达到相同的目的。

* @author 281167413@qq.com

*/

public class Test8 {

public static void main(String[] args)

{

nomDisplay();

breakDisplay();

returnDisplay();

}

public static void nomDisplay()

{

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

{

System.out.print(i);

}

System.out.print(" nom end!\\n");

}

public static void breakDisplay()

{

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

{

if (98 == i)

{

break;

}

System.out.print(i);

}

System.out.print(" break end!\\n");

}

public static void returnDisplay()

{

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

{

if (98 == i)

{

return;

}

System.out.print(i);

}

System.out.print(" return end!\\n");

}

}

打印结果:

?

1

2

3
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 nom end!

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 break end!

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 java打印从1到100的值(break,return断句) https://www.kuaiidc.com/118602.html

相关文章

发表评论
暂无评论