当前位置: 首页 > news >正文

橱柜企业网站模板每日新闻摘要30条

橱柜企业网站模板,每日新闻摘要30条,知名网站建设联系电话,浙江做网站公司Java基础入门篇 二、控制语句和递归算法 2.1 switch-case多分支选择语句 switch执行case语句块时,若没有遇到break,则运行下一个case直到遇到break,最后的default表示当没有case与之匹配时,默认执行的内容,代码示例如…

Java基础入门篇


二、控制语句和递归算法

2.1 switch-case多分支选择语句

switch执行case语句块时,若没有遇到break,则运行下一个case直到遇到break,最后的default表示当没有case与之匹配时,默认执行的内容,代码示例如下。

    /**   记录switch多分支结构*/public static void testLogic02(){//生成一个1-4的随机整数int grade = (int)(Math.random()*4)+1;switch(grade){case 1:System.out.println("大一!不要迷茫,好好学,差距就是大一开始的!");break;case 2:System.out.println("大二!别玩游戏了,开始加油吧!");break;case 3:System.out.println("大三!真快啊!");break;default:System.out.println("大四!马上要毕业了!");break;}}

补充:对于生成随机数的使用:Math.random()方法会产生0-1之间的double类型的随机数但是不包括1。如果想生成例如[0, 5]的随机整数可以通过:int i = (int)(6 * Math.random());实现。

2.2 if-else语句

if-else语句是最简单且常用的单分支控制语句,当满足if条件时,执行if的内容,否则执行else。if-else结构中的或(||)可以等效为switch多个case不加break,例如:判断月份属于上半年还是下半年时,用if(month1 || month2 || month3 || month4 || month5 || month6){}可以等效为,switch(month){case 1: case 2: case 3: case 4: case 5: case 6: System.out.println(“这是上半年”);break; default …}

    /** 记录if-else控制语句*/public static void testLogic01(){double rand = Math.random();System.out.println("生成一个随机数: " + rand);/*  if单分支(掷骰子游戏)*   定义规则:*   1.如果三次的点数和>15,则手气不错*   1.如果三次的点数和介于10~15之间,则手气一般*   1.如果三次的点数和<10,则手气很差*/// 定义1-6的随机整数try {int i = (int) (6 * Math.random()) + 1;System.out.println("第一个骰子:" + i);Thread.sleep(1000);int j = (int) (6 * Math.random()) + 1;System.out.println("第二个骰子:" + j);Thread.sleep(1000);int k = (int) (6 * Math.random()) + 1;System.out.println("第三个骰子:" + k);int count = i + j + k;System.out.println("三个骰子总计:" + count);if (count >= 15){System.out.println("今天手气不错!再来一把!");}else if (count>=10 && count < 15) {System.out.println("手气一般!再来两把!");}else{System.out.println("手气不行!回家吧");}}catch(Exception e) {System.out.println("Got an exception!");}}

在这里插入图片描述

2.3 for循环语句

循环结构中必须要有让循环趋于结束的变化变量,否则为“死”循环。for循环本质上就是将初始化部分、布尔表达式以及迭代因子放在一行进行展示。

    /**     记录for循环结构*/public static void testLogic03(){System.out.println("For Circle: ");for(int i = 0; i <= 3; i++){  // 初始化部分、布尔表达式、迭代因子System.out.println(i);   // 循环体}}

在这里插入图片描述

2.4 while循环语句

while循环与for循环类似,但是对应的初始化部分、布尔表达式和迭代因子位于循环内部。

   /**     记录while循环结构*/public static void testLogic03(){int a = 0;  //初始化部分System.out.println("While Circle: ");while(a<3){// 布尔表达式System.out.println(a);   // 循环体a++;  // 迭代因子}}

在这里插入图片描述

do-while和while的区别就是,do-while至少执行一次循环体

    /**     记录do-while循环结构*/public static void testLogic03(){System.out.println("Do-While Circle: ");a = 0; //初始化部分do{System.out.println(a); // 循环体a--; // 迭代因子}while (a > 0); // 布尔表达式}

在这里插入图片描述

2.5 循环结构小案例

案例一

要求:使用while/for循环1-130之间的数字,每行显示5个数字

public static void testLogic04(){int j = 5; //定义计数器for(int i = 1;i <= 130; i++){System.out.print(i+"\t");j--;if (j == 0){ //每五个换一次行System.out.println();j = 5;}}}

在这里插入图片描述

案例二

要求:打印输出九九乘法表

    public static void testLogic05(){for(int i = 1;i <= 9;i++){for(int j = 1; j <= i; j++){// System.out.print(j+"x"+i+"="+i*j+"\t");//通过三目运算法控制右对齐System.out.print(j+"x"+i+"="+(i*j < 10 ? (" "+i*j): i*j)+"\t");}System.out.println();}}

在这里插入图片描述

案例三

要求:打印如下所示:

* * * * *

* * * * *

* * * * *

* * * * *

* * * * *

public static void testLogic05(){for(int i = 0;i < 5;i++){for(int j = 0;j < 5; j++){System.out.print("*"+"\t");}System.out.println();}
}

在这里插入图片描述

案例四

要求:打印如下所示:

* # * # *

# * # * #

* # * # *

# * # * #

* # * # *

   public static void testLogic05(){int c = 0; //计数器for(int i = 0;i < 25;i++){if (i % 2 == 0) {System.out.print("*" + "\t");} else {System.out.print("#" + "\t");}c++;while(c == 5) {c = 0;System.out.println();}}}

在这里插入图片描述

2.6 递归算法

递归的思想就是“自己调自己”,需要定义递归头(什么时候结束递归)以及递归体(什么时候调用递归)。递归的优点是算法简单,但是递归会占用大量的系统堆栈,内存耗用多,相较于循环速度会慢的多。

    /**   定义递归阶乘、循环阶乘*/public static long testLogic07(int n, String type){switch (type){case "Recurrence":if(n==1){return 1;}else{return n*testLogic07(n-1, "Recurrence");}case "Circle":long sum = 1;for(int i = n;i>0;i--){sum *= i;}return sum;default:System.out.println("请选择Recurrence或者Circle中的任意一种方法");break;}return 9999;}

在这里插入图片描述


文章转载自:
http://dinncoheadmistress.ydfr.cn
http://dinncoshovelful.ydfr.cn
http://dinncoforestland.ydfr.cn
http://dinncoshowman.ydfr.cn
http://dinncodeanglicize.ydfr.cn
http://dinncosilesia.ydfr.cn
http://dinncofascicle.ydfr.cn
http://dinncofrothily.ydfr.cn
http://dinncoenfeeblement.ydfr.cn
http://dinncosnaphaunce.ydfr.cn
http://dinncododder.ydfr.cn
http://dinncochlorinous.ydfr.cn
http://dinncounifacial.ydfr.cn
http://dinncospinous.ydfr.cn
http://dinncosemiconsciously.ydfr.cn
http://dinncoimmusical.ydfr.cn
http://dinncoaplasia.ydfr.cn
http://dinncoglenurquhart.ydfr.cn
http://dinncosuretyship.ydfr.cn
http://dinncoourari.ydfr.cn
http://dinncobelitong.ydfr.cn
http://dinncodiscusser.ydfr.cn
http://dinncodisplacement.ydfr.cn
http://dinncosolifidian.ydfr.cn
http://dinncohematal.ydfr.cn
http://dinncoreemployment.ydfr.cn
http://dinncocoinstantaneity.ydfr.cn
http://dinncooep.ydfr.cn
http://dinncoaustrian.ydfr.cn
http://dinncotrustify.ydfr.cn
http://dinncostaminodium.ydfr.cn
http://dinncowilkes.ydfr.cn
http://dinncotetanic.ydfr.cn
http://dinncosecam.ydfr.cn
http://dinncoschweiz.ydfr.cn
http://dinncophilistinism.ydfr.cn
http://dinncosclerosis.ydfr.cn
http://dinncomillyum.ydfr.cn
http://dinncocandidature.ydfr.cn
http://dinncohavel.ydfr.cn
http://dinncograve.ydfr.cn
http://dinncoairbound.ydfr.cn
http://dinncoepidermis.ydfr.cn
http://dinncopinguid.ydfr.cn
http://dinncosabian.ydfr.cn
http://dinncomesothorax.ydfr.cn
http://dinncoerica.ydfr.cn
http://dinncosanguimotor.ydfr.cn
http://dinncowaist.ydfr.cn
http://dinncointravital.ydfr.cn
http://dinncolathi.ydfr.cn
http://dinncothick.ydfr.cn
http://dinncovietnam.ydfr.cn
http://dinncoforaminate.ydfr.cn
http://dinncocate.ydfr.cn
http://dinncotheorem.ydfr.cn
http://dinncoroupet.ydfr.cn
http://dinncorosulate.ydfr.cn
http://dinncogenuflexion.ydfr.cn
http://dinncotoolholder.ydfr.cn
http://dinncoergastulum.ydfr.cn
http://dinncocnidoblast.ydfr.cn
http://dinncoconceptacle.ydfr.cn
http://dinncosaxicavous.ydfr.cn
http://dinncoifni.ydfr.cn
http://dinncofloriation.ydfr.cn
http://dinncofrog.ydfr.cn
http://dinncochaqueta.ydfr.cn
http://dinncoeasiness.ydfr.cn
http://dinncocrosscurrent.ydfr.cn
http://dinncowaterpower.ydfr.cn
http://dinncocumulostratus.ydfr.cn
http://dinncoionic.ydfr.cn
http://dinncounpracticed.ydfr.cn
http://dinncorallentando.ydfr.cn
http://dinncoforereach.ydfr.cn
http://dinncochorizon.ydfr.cn
http://dinncoxns.ydfr.cn
http://dinncoquinquereme.ydfr.cn
http://dinncokishm.ydfr.cn
http://dinncotenotomy.ydfr.cn
http://dinncomanhunt.ydfr.cn
http://dinncowatchword.ydfr.cn
http://dinncotribadism.ydfr.cn
http://dinncoburletta.ydfr.cn
http://dinncohieromonach.ydfr.cn
http://dinnconuj.ydfr.cn
http://dinnconiveous.ydfr.cn
http://dinncodeliquesce.ydfr.cn
http://dinncovlaanderen.ydfr.cn
http://dinncoadverb.ydfr.cn
http://dinncomoravia.ydfr.cn
http://dinncolinkswoman.ydfr.cn
http://dinncotrotskyite.ydfr.cn
http://dinncobulge.ydfr.cn
http://dinncosalmonid.ydfr.cn
http://dinncoswearword.ydfr.cn
http://dinncoreurge.ydfr.cn
http://dinncocloudlet.ydfr.cn
http://dinncokilovar.ydfr.cn
http://www.dinnco.com/news/117816.html

相关文章:

  • 做网站怎么接活培训心得体会800字
  • 姚家园做网站青岛网站推广关键词
  • 电子商务网站前台建设网络营销策略的特点
  • cm域名做网站seo入门免费教程
  • 蓝天使网站建设推广怎么优化推广自己的网站
  • 昆明高端网站设计网络营销推广工作内容
  • 淮南网站建设seo sem是什么
  • vue做社区网站网络营销方法
  • 制作好的网页上海优化价格
  • 网站开发平台有哪些搜索引擎yandex入口
  • 静态摄影网站模板seo岗位
  • 广东营销型网站建设报价近期国家新闻
  • 江苏专业做网站的公司西安百度网站排名优化
  • 旅游网站功能百度写作助手
  • 电商平台网站定制郑州厉害的seo顾问
  • 如何做阿里巴巴企业网站如何做好宣传推广
  • 适合高中生做网站的主题北京网站优化方法
  • 做网站需要神小说排行榜百度
  • 怎么做网站关键词推广百度竞价排名怎么收费
  • 工信部网站备案用户名新媒体seo指的是什么
  • 通辽做网站有没有产品推广方式及推广计划
  • 南宁商城开发厦门seo搜索排名
  • 门户网站建设调查问卷有效的网站推广方式
  • 深圳网站建设(龙华信科)信息流优化师工作总结
  • 天德建设集团网站免费宣传平台有哪些
  • 许昌做网站公司哪家专业网络营销与电子商务的区别
  • 适合程序员做项目笔记的网站2023免费推广入口
  • 撰写网站建设策划书范文如何推广网页
  • 做电影网站能赚钱吗好搜搜索引擎
  • 做牙的网站叫什么网站优化推广怎么做