java打印從1到100的值(break,return斷句)
首先來(lái)講這個(gè)沒有什么難點(diǎn),就是分析下break和return的效果有什么不一樣,通過最后的打印結(jié)果可以看出:
1、break只是跳出了循環(huán)會(huì)繼續(xù)執(zhí)行函數(shù)內(nèi)、循環(huán)外的代碼。
2、return是直接函數(shù)返回了,循環(huán)內(nèi)和函數(shù)內(nèi)的后面的代碼都不會(huì)在執(zhí)行了。
代碼:
package com.itheima; /** * 8、 先寫一個(gè)程序,打印從1到100的值。之后修改程序,通過使用break關(guān)鍵詞,使得程序在打印到98時(shí)退出。然后嘗試使用return來(lái)達(dá)到相同的目的。 * @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"); } }
打印結(jié)果:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 nom end! 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 break end! 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
相關(guān)文章
SpringBoot淺析安全管理之Spring Security配置
安全管理是軟件系統(tǒng)必不可少的的功能。根據(jù)經(jīng)典的“墨菲定律”——凡是可能,總會(huì)發(fā)生。如果系統(tǒng)存在安全隱患,最終必然會(huì)出現(xiàn)問題,這篇文章主要介紹了SpringBoot安全管理Spring Security基本配置2022-08-08阿里Sentinel支持Spring Cloud Gateway的實(shí)現(xiàn)
這篇文章主要介紹了阿里Sentinel支持Spring Cloud Gateway的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2019-04-04Java內(nèi)存之happens-before和重排序
在JMM(Java內(nèi)存模型)中,如果一個(gè)操作執(zhí)行的結(jié)果需要對(duì)另一個(gè)操作可見,那么這兩個(gè)操作之間必須存在happens-before關(guān)系。下面小編來(lái)簡(jiǎn)單介紹一下2019-05-05IDEA中Maven依賴包無(wú)法下載或?qū)氲慕鉀Q方案(系統(tǒng)缺失文件導(dǎo)致)
在配置Maven環(huán)境時(shí),可能會(huì)遇到各種報(bào)錯(cuò)問題,首先確保Maven路徑配置正確,例如使用apache-maven-3.5.0版本,則需要在系統(tǒng)環(huán)境變量的Path中添加其bin目錄路徑,并上移優(yōu)先級(jí),接下來(lái),在Maven的conf目錄下修改settings.xml文件,將鏡像源改為阿里云2024-09-09SpringCloud Gateway HttpWebHandlerAdapter鏈路調(diào)用請(qǐng)求流程介
Spring Cloud Gateway旨在為微服務(wù)架構(gòu)提供一種簡(jiǎn)單有效的、統(tǒng)一的 API 路由管理方式。Spring Cloud Gateway 作為 Spring Cloud 生態(tài)系中的網(wǎng)關(guān),它不僅提供統(tǒng)一的路由方式,并且基于 Filter 鏈的方式提供了網(wǎng)關(guān)基本的功能,例如:安全、監(jiān)控/埋點(diǎn)和限流等2022-10-10