跟着老师学 Java 06-10(基本语法)
2021/9/22 20:42:59
本文主要是介绍跟着老师学 Java 06-10(基本语法),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
6. day 6 基本 for 语句
for 语句是一种循环结构,其形式一般如下:
/* ① 初始条件 ② 循环条件(为 boolean 类型) ③ 循环体 ④ 迭代条件 */ for (①; ②; ④){ ③; }
在上述结构中,程序执行的过程如下:
① → ② → ③ → ④ → ② → ③ → ④ → … → ②
抄写老师的代码:
public class ForStatement { /** ********************* * The entrance of the program. * * @param args Not used now. ********************* */ public static void main(String[] args) { forStatementTest(); }// Of main /** ********************* * Method unit test. ********************* */ public static void forStatementTest() { int tempN = 10; System.out.println("1 add to " + tempN + " is: " + addToN(tempN)); tempN = 0; // 从1加到0,不满足for循环中的条件,故不会执行语句,直接跳出循环 System.out.println("1 add to " + tempN + " is: " + addToN(tempN)); int tempStepLength = 1; tempN = 10; System.out.println("1 add to " + tempN + " with step length " + tempStepLength + " is: " + addToNWithStepLength(tempN, tempStepLength)); tempStepLength = 2;//将步长设置为2,实现了将1-10中的奇数相加 System.out.println("1 add to " + tempN + " with step length " + tempStepLength + " is: " + addToNWithStepLength(tempN, tempStepLength)); }// Of forStatementTest /** ********************* * Add from 1 to N. * * @param paraN The given upper bound. * @return The sum. ********************* */ public static int addToN(int paraN) { int resultSum = 0; for (int i = 1; i<= paraN; i++) { resultSum += i; }// Of for i return resultSum; }// Of addToN /** ********************* * Add from 1 to N with a step length. * * @param paraN The given upper bound. * @param paraStepLength The given step length. * @return The sum. ********************* */ public static int addToNWithStepLength(int paraN, int paraStepLength) { int resultSum = 0; for (int i=1; i <= paraN; i += paraStepLength) { resultSum += i; } // Of for i return resultSum; }// Of addToNWithStepLength } // Of class ForStatement
9. day 9 while 语句
while 语句是另一种循环结构,一般有如下形式:
/* ① 初始条件 ② 循环条件(为 boolean 类型) ③ 循环体 ④ 迭代条件 */ ① while (②){ ③; ④; }
在上述结构中,程序执行的过程如下:
① → ② → ③ → ④ → ② → ③ → ④ → … → ②
跟写老师的代码:
package com.teachercode; public class WhileStatement { /** ********************* * The entrance of the program. * * @param args Not used now. ********************* */ public static void main(String[] args) { whileStatementTest(); }// Of main /** ********************* * The sum not exceeding a given value. ********************* */ public static void whileStatementTest() { int tempMax = 5; int tempValue = 0; int tempSum = 0; // Approach 1 while (tempSum <= tempMax) { tempValue++; tempSum += tempValue; System.out.println("tempValue = " + tempValue + ", tempSum = " + tempSum);//输出每次循环对应的tempValue和tempSum的值 }// Of while tempSum -= tempValue;//tempSum的值一旦大于tempMax就退出循环,故此时tempSum减去最后那一个tempValue才是不超过tempMax System.out.println("The sum not exceeding " + tempMax + " is: " + tempSum); // Approach 2 System.out.println("\r\nAlternative approach."); tempValue = 0; tempSum = 0; while (true) {// 无限循环的格式,不确定循环多少次时使用 tempValue++; tempSum += tempValue; System.out.println("tempValue = " + tempValue + ", tempSum = " + tempSum); if (tempMax < tempSum) { break;//一旦tempSum的值大于tempMax,执行到break语句,就跳出循环 }// Of if }// Of while tempSum -= tempValue; System.out.println("The sum not exceeding " + tempMax + " is: " + tempSum); }// Of whileStatementTest }// Of class WhileStatement
注意:
- 在使用 while 循环的时候,循环的迭代条件漏写可能导致程序陷入死循环;
- for 和 while 这2种循环可以相互转换
- 关于
while (true)
:
可以看作“无限循环”的形式,在 for 循环中就是for(;;)
。这种形式在不明确循环次数的时候使用,一般通过循环内部的条件,来控制循环的结束。比如上面代码中,在不满足tempMax >= tempSum
这个条件时,就通过break
跳出循环; - 对于循环结构,结束循环的方式不唯一:
① 循环条件为 false 时;
② 在循环体中,执行到 break.
这篇关于跟着老师学 Java 06-10(基本语法)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-05小米13T Pro系统合集:性能与摄影的极致融合,值得你升级的系统ROM
- 2024-10-01基于Python+Vue开发的医院门诊预约挂号系统
- 2024-10-01基于Python+Vue开发的旅游景区管理系统
- 2024-10-01RestfulAPI入门指南:打造简单易懂的API接口
- 2024-10-01初学者指南:了解和使用Server Action
- 2024-10-01Server Component入门指南:搭建与配置详解
- 2024-10-01React 中使用 useRequest 实现数据请求
- 2024-10-01使用 golang 将ETH账户的资产平均分散到其他账户
- 2024-10-01JWT用户校验课程:从入门到实践
- 2024-10-01Server Component课程入门指南