我为循环创建了两个这样的打印行(每个新的数字开始一个新的行,这里没有显示):
1
22
333
4444
等到9,然后再降到1。
我应该把它翻译成一段时间和一段时间,在过去的一个小时里一直在尝试,但似乎做不到。
public static void main(String[] args) {
for (int x=1; x <= 9; x++) {
for (int y = 1; y <=x ; y++){
System.out.print( x + "");
}
System.out.println();
}
// TODO code application logic here
for (int x=9; x >=1; x--) {
for (int y = 1; y <=x ; y++){
System.out.print( x + "");
}
System.out.println();
} int y =1;
int x = 1;
while (x <9){
while (y <=x){
y++;
System.out.print(x +"");{
}
System.out.println();
}
x++;
}发布于 2013-10-02 02:12:23
尝试将此作为自己的while循环:
int y = 1;
int x = 1;
while (x < 9) {
y = 1;
while (y <= x) {
y++;
System.out.print(x + "");
}
System.out.println();
x++;
}发布于 2013-10-02 01:42:31
for循环语句在for (init; condition; post)中有三个部分。这些部分用分号隔开。init部分指定初始语句,条件决定是否执行循环主体,post指定post循环语句。
您可以使用一个while循环做同样的事情,只不过它不是一条语句,而是几条语句。但是,while循环并不完全像for循环,因为continue语句和它的行为方式是一个问题。稍后再谈这个。
提示是,for语句的各个部分由分号分隔,分号也用于分隔Java中的语句。
考虑下面的for循环源代码示例。
int i;
for (i = 0; i < 5; i++) {
// for loop body
}因此,在循环语句之前有一个init语句,然后是i = 0语句本身,它本身包含条件i < 5,并且作为结束大括号之前的循环的最后一行,您将把post循环条件i++。
由于计算do while条件的时间,while要复杂一些。在for循环和while循环中,将计算条件,如果表达式不为真,则根本不执行循环体,则跳过。在for循环的情况下,执行init语句,然后对条件进行评估,以确定是否应该执行for循环体。由于while循环没有init语句作为while语句的一部分,因此将计算条件,如果不为真,则不执行while循环体。
do while循环具有直到第一次通过do while循环体后才计算的条件。因此,do while循环中的语句总是至少执行一次。然后计算do while条件,如果为真,则执行返回到do所在的顶部,并再次执行do while循环体。
一些循环变体的代码,init是i = 0,条件是i < 5,post是i++。在所有情况下,我都在循环体的外部定义了变量i。对于for循环,在for语句中定义变量i将导致变量i的作用域及其可见性被限制在for循环主体上,而其他类型的循环则不是这样。
int i;
for (i = 0; i < 5; i++) {
// for loop body
}
int i = 0;
while (i < 5) {
// while loop body
i++;
}
int i = 0;
do {
// do while loop body
i++;
} while (i < 5);我提到过,当执行continue语句时,在比较这些循环形式时会发生什么变化。可以这样想,当执行continue语句时,就会跳转到包含continue语句的循环语句的结束大括号。这就引出了一些需要考虑的问题。
请看上面的示例,但是使用一个continue语句。在下面的所有示例中,都有一个continue语句,当变量i的值为3时,它将导致执行跳到循环主体的末尾。
通过这种更改,for循环将继续递增变量i,因为它的post ( for语句的第三部分)将在循环的末尾执行。但是,对于while循环和do while循环,变量i的增量是循环主体的一部分,因此当执行continue跳过循环结束时,变量i的增量也被跳过。
int i;
// first time init variable i to zero then evaluate condition
for (i = 0; i < 5; i++) { // evaluate condition and execute loop body if true
// for loop body
if (i == 3)
continue; // when i == 3 continue is executed to skip to end of loop
} // at point of braces, post executed, condition evaluated
int i = 0;
while (i < 5) { // evaluate condition and execute loop body if true
// while loop body
if (i == 3)
continue; // when i == 3 continue is executed to skip to end of loop
i++; // variable i only incremented when this statement is executed
} // braces indicate end of loop so jump back to top of loop
int i = 0;
do {
// do while loop body
if (i == 3)
continue; // when i == 3 continue is executed to skip to end of loop
// more statements which may be skipped by the continue
i++; // variable i only incremented when this statement is executed
} while (i < 5); // evaluate condition and jump to top of loop if true您可以更改do while循环while条件,将变量i的增量移动到while条件计算中,方法是对变量使用增量前运算符++操作符,如下所示。我们使用预增量运算符,因为我们希望在检查变量i的值之前增加它。
int i = -1; // need to start at -1 since the while will increment at beginning of the loop
while (++i < 5) { // increment variable i, evaluate condition and body of loop if true
// while loop body
if (i == 3)
continue; // when i == 3 continue is executed to skip to end of loop
} // braces indicate end of loop so jump back to top of loop
int i = 0;
do {
// do while loop body
if (i == 3)
continue; // when i == 3 continue is executed to skip to end of loop
// more statements which may be skipped by the continue
} while (++i < 5); // increment variable i, evaluate condition and jump to top of loop if truehttps://stackoverflow.com/questions/19128542
复制相似问题