【日撸 Java 三百行】Day 6(For语句)
目录
Day 6:For 语句的基本使用方法
一、基础知识及案例分析
二、代码及测试
拓展:流程控制语句专题补充
小结
Day 6:For 语句的基本使用方法
Task:
- 循环语句是程序的核心.
- 算法的时间复杂度一般根据循环语句来计算.
一、基础知识及案例分析
1. 关于循环语句
循环语句是计算机编程中计算大数据的内容的优势之处,是同时也是简化重复流程的重要方法。
java的循环语句使用依旧继承类C语言体系,具体循环语句有:
- while 循环
- do…while 循环
- for 循环
今天主要涉及for循环的使用。for循环在while循环的基础上扩充了固定的初始化部分和递增部分,令定长增长的循坏代码编写变得简单。其余的循环语句请参考相关专题补充。
2. 程序分析
本测试依旧通过编写函数来模拟每次算法代码核心,同时也将简单的打印操作与赋值操作封装为另外函数完成。main函数只负责简单的驱动作用。
核心 for 循环函数构造如下:
/************************ 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 ireturn 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 ireturn resultSum;}// Of addToNWithStepLengthpublic class HelloWorld {public static void main(String[] args) {System.out.println("Hello, World !");}//of main
}//of class HelloWorld
这里分别构造的两个函数循环的操作都是求1到N和的操作,分别使用了默认的步长1的递增for循环,以及自定义步长值的for循环操作。
可以用以下两个数学计算式来表示这两种步长的计算方式:
1)1到N以步长为1求和:
2)1到N以步长为p求和:
二、代码及测试
代码如下:
package basic;/*** The usage of sth.** @author: Changyang Hu joe03@foxmail.com* @date created: 2025-05-08*/
public class ForStatement {/*** * @Title: main* @Description: No practical function, only to drive* @param args Not used now* @return void * @throws*/public static void main(String args[]) {forStatementTest();}// Of mainpublic static void forStatementTest() {int tempN = 10;System.out.println("1 add to " + tempN + " is: " + addToN(tempN));tempN = 0;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+ addToNWithStepLength(tempN, tempStepLength));tempStepLength = 2;System.out.println("1 add to " + tempN + " with step length " + tempStepLength+ addToNWithStepLength(tempN, tempStepLength));}// Of forStatement/*** * @Title: addToN* @Description: Add from 1 to N.* @param paraN* @return int * @throws*/public static int addToN(int paraN) {int resultSum = 0;for (int i = 1; i <= paraN; i++) {resultSum += i;} // Of for ireturn resultSum;}// Of addToN/*** * @Title: addToWithStepLength* @Description: Add from 1 to N with a step length* * @param paraN The given upper bound* @param paraStepLength The given step length* @return the sum* @return int * @throws*/public static int addToNWithStepLength(int paraN, int paraStepLength) {int resultSum = 0;for (int i = 1; i <= paraN; i += paraStepLength) {resultSum += i;} // Of for ireturn resultSum;}// Of addToNWithStepLength
}// Of class ForStatement
运行结果如下:
需要注意的是:
你可能发现1到0求和不难道是0吗?其实不然,循环有个基本条件,就是循环因子要小于等于限定的N才能进入循环,而我们的代码循环因子是从1开始的,而这里第二案例N=0,不满足循环的条件,自然是没有循环的发生,答案也自然是0。
其余答案带入上述的公式中可以很快验证其正确性。
拓展:流程控制语句专题补充
【Java 专题补充】流程控制语句-CSDN博客
小结
循环是编写程序的一个非常基本的能力,灵活利用各种循环的嵌套和循环的组合是一个程序员的基本功。