编程语言
首页 > 编程语言> > 跟着老师学 Java 06-10(基本语法)

跟着老师学 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

注意:

标签:10,Java,int,tempN,while,循环,tempValue,06,tempSum
来源: https://blog.csdn.net/weixin_41857483/article/details/120421734