十、循环语句
1.循环结构
2.for循环
public class Testfor{
public static void main (String[] args){
System.out.println("ready to itrate:");
for(int i = 0; i < 100; i++){
System.out.println(i);
}
System.out.println("finished itrate!");
}
}
注意:for(赋值;判断条件;改变变量){ 语句; } for括号里的语句用分号隔开,最后的不用分号。
The for
statement also has another form designed for iteration throughCollections and arrays This form is sometimes referred to as the enhanced for statement, and can be used to make your loops more compact and easy to read. To demonstrate, consider the following array, which holds the numbers 1 through 10:
int[] numbers = {1,2,3,4,5,6,7,8,9,10};
The following program, EnhancedForDemo
, uses the enhanced for
to loop through the array:
class EnhancedForDemo {
public static void main(String[] args){
int[] numbers =
{1,2,3,4,5,6,7,8,9,10};
for (int item : numbers) {
System.out.println("Count is: " + item);
}
}
}
In this example, the variable item
holds the current value from the numbers array. The output from this program is the same as before:
Count is: 1
Count is: 2
Count is: 3
Count is: 4
Count is: 5
Count is: 6
Count is: 7
Count is: 8
Count is: 9
Count is: 10
We recommend using this form of the for
statement instead of the general form whenever possible.
3.while循环
public class Testwhile{
public static void main (String[] args){
int i = 0; //声明变量并赋值
System.out.println("ready to iterate");
while(i < 50){ //判断是否符合条件
System.out.println(i);
i += 2;//改变变量
}
System.out.println("finish iterate");
}
}
do…while
The Java programming language also provides a do-while
statement, which can be expressed as follows:
do {
statement(s)
} while (expression);
The difference between do-while
and while
is that do-while
evaluates its expression at the bottom of the loop instead of the top. Therefore, the statements within thedo
block are always executed at least once, as shown in the followingDoWhileDemo
program:
class DoWhileDemo {
public static void main(String[] args){
int count = 1;
do {
System.out.println("Count is: " + count);
count++;
} while (count < 11);
}
}
十一、练习题三(练习使用for循环)
写程序应该应该把程序拆分成很多的步骤,按步骤完成程序的编写。
练习一步骤:
1.定义一个类,名为TestPrimeNumber;
2.在类当中定义主函数;
3.用for循环打出所有在100-200之间的数字;
4.在for循环中,每执行一次就判断一次循环变量的值是否为素数,是素数则打印当前值
a)判断i 是否为素数,首先用2除i,除不尽再用3,以此类推,如果从2到i-1都无法除尽,那么i就是素数。
public class TestPrimeNumber{
public static void main(String[] args){
for(int i = 100;i <= 200;i++){
boolean b = false;
for(int k = 2 ; k < i; k++){
int j = i % k;
if (j == 0){
b = true;
}
}
if(!b){
System.out.println(i);
}
}
}
}
练习二步骤:
1.定义一个类,名为TestTriangle;
2.定义主函数;
3.使用for循环打印四行,每行一个星号;
4.打印四行,每行打印行数个“* ”;
5.每行之前打印4减行数个空格;
public class TestTriangle{
public static void main(String[] args){
for(int i = 1; i <= 4; i++){
for(int k = 1; k <= 4 - i; k++){
System.out.print(" ");
}
for(int j = 1; j <= i; j++){
System.out.print("* ");
}
System.out.println("");
}
}
}
作业:
1.优化本节课中的TestPrimeNumber.java
解答:
2.打印任意行与练习二中类似的图形。
解答:修改n的数值即可。
public class TestTriangle{
public static void main(String[] args){
int n = 6;
for(int i = 1; i <= n; i++){
for(int k = 1; k <= n - i; k++){
System.out.print(" ");
}
for(int j = 1; j <= i; j++){
System.out.print("* ");
}
System.out.println("");
}
}
}