在Java中将for循环转换为while和do while循环
我创建了一对循环,用于打印这样的行(每个新编号开始一行,此处不显示): 1在Java中将for循环转换为while和do while循环,java,Java,我创建了一对循环,用于打印这样的行(每个新编号开始一行,此处不显示): 1 22 333 4444 等等,直到它达到9,然后返回到1 我应该把它转换成一个while和do-while循环,在过去的一个小时里我一直在尝试,但似乎做不到 public static void main(String[] args) { for (int x=1; x <= 9; x++) { for (int y = 1; y <=x ; y++){ Sys
22
333
4444 等等,直到它达到9,然后返回到1 我应该把它转换成一个while和do-while循环,在过去的一个小时里我一直在尝试,但似乎做不到
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++;
}
publicstaticvoidmain(字符串[]args){
for(intx=1;xA是一种与A极为相似的构造,只是它提供了一些额外的细节,可以减少
for
循环的开头设置了您的初始变量(在您的例子中是x),您的条件子句(xa)是一个非常类似于a的构造,只是它提供了一些额外的细节,可以减少
for
循环的开头设置您的初始变量(在您的例子中是x),您的条件子句(xafor
for循环语句在for(init;condition;post)中有三个部分
。这些部分用分号分隔。init部分指定初始语句,条件决定是否执行循环体,post指定post循环语句
您可以对while
循环执行相同的操作,不同的是,它实际上是多个语句,而不是单个语句。但是while
循环与for
循环并不完全相同,因为continue
语句及其行为是一个问题。稍后将详细介绍
提示是,for
语句的各个部分用分号分隔,分号在Java中也用于分隔语句
考虑以下循环源代码示例
int i;
for (i = 0; i < 5; i++) {
// for loop body
}
我提到当执行continue
语句时会发生什么,在比较这些循环形式时会产生不同。我们可以这样认为,当执行continue
语句时,会跳转到包含continue
语句的循环语句的右括号。因此需要考虑的事情。
查看上面的示例,但使用continue
语句。在下面的所有示例中,都有continue
语句,当变量i
的值为3时,该语句会导致执行跳到循环体的末尾
通过此更改,for
循环将继续递增变量i
,因为它是post,在循环结束时执行for
语句的第三部分。但是,对于while
循环和do while
循环,变量i
的递增是loo的一部分p body,因此当执行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
for
循环语句在for(init;condition;post)
中有三个部分。这些部分用分号分隔。init部分指定初始语句,条件决定是否执行循环体,post指定post循环语句
您可以对while
循环执行相同的操作,不同的是,它实际上是多个语句,而不是单个语句。但是while
循环与for
循环并不完全相同,因为continue
语句及其行为是一个问题。稍后将详细介绍
提示是,for
语句的各个部分用分号分隔,分号在Java中也用于分隔语句
考虑以下循环源代码示例
int i;
for (i = 0; i < 5; i++) {
// for loop body
}
我提到当执行continue
语句时会发生什么,在比较这些循环形式时会产生不同。我们可以这样认为,当执行continue
语句时,会跳转到包含continue
语句的循环语句的右括号。因此需要考虑的事情。
查看上面的示例,但使用continue
语句。在下面的所有示例中,都有continue
语句,当变量i
的值为3时,该语句会导致执行跳到循环体的末尾
通过此更改,for
循环将继续递增变量i
,因为它是post,在循环结束时执行for
语句的第三部分。但是,对于while
循环和do while
循环,变量i
的递增是loo的一部分p body,因此当执行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
尝试将此作为您的while循环:
int y = 1;
int x = 1;
while (x < 9) {
y = 1;
while (y <= x) {
y++;
System.out.print(x + "");
}
System.out.println();
x++;
}
inty=1;
int x=1;
而(x<9){
y=1;
while(y尝试将此作为while循环:
int y = 1;
int x = 1;
while (x < 9) {
y = 1;
while (y <= x) {
y++;
System.out.print(x + "");
}
System.out.println();
x++;
}
inty=1;
int x=1;
而(x<9){
y=1;
while(y)告诉我们你尝试过什么,我们会帮你的。我尝试过编辑,但它不允许我,但我尝试过:int y=1;int x=1;while(x不是数字的数量
?嗯?@user2836944在你的问题中发布你尝试过的代码对不起,我的措辞很糟糕,我的意思不是“1,22,333,4444”等等。告诉我们你试过什么,我们会帮你的。我试过编辑,但它不允许我,但我试过:int y=1;int x=1;while(x不是数字的相应数量
?嗯?@user2836944在你的问题中发布你的试过的代码对不起,我的措辞很糟糕,我的意思不是“1,22,333,4444”等修改为X修改为X