首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Java中将for循环转换为while并执行while循环

问在Java中将for循环转换为while并执行while循环
EN

Stack Overflow用户
提问于 2013-10-02 01:18:55
回答 3查看 2K关注 0票数 0

我为循环创建了两个这样的打印行(每个新的数字开始一个新的行,这里没有显示):

1

22

333

4444

等到9,然后再降到1。

我应该把它翻译成一段时间和一段时间,在过去的一个小时里一直在尝试,但似乎做不到。

代码语言:javascript
复制
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++;
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-10-02 02:12:23

尝试将此作为自己的while循环:

代码语言:javascript
复制
int y = 1;
int x = 1;

while (x < 9) {

    y = 1;
    while (y <= x) {
            y++;

        System.out.print(x + "");


    }
    System.out.println();
    x++;
}
票数 0
EN

Stack Overflow用户

发布于 2013-10-02 01:42:31

for循环语句在for (init; condition; post)中有三个部分。这些部分用分号隔开。init部分指定初始语句,条件决定是否执行循环主体,post指定post循环语句。

您可以使用一个while循环做同样的事情,只不过它不是一条语句,而是几条语句。但是,while循环并不完全像for循环,因为continue语句和它的行为方式是一个问题。稍后再谈这个。

提示是,for语句的各个部分由分号分隔,分号也用于分隔Java中的语句。

考虑下面的for循环源代码示例。

代码语言:javascript
复制
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循环主体上,而其他类型的循环则不是这样。

代码语言:javascript
复制
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的增量也被跳过。

代码语言:javascript
复制
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的值之前增加它。

代码语言:javascript
复制
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 true
票数 4
EN

Stack Overflow用户

发布于 2013-10-02 01:36:19

循环播放是一个与循环播放非常相似的构造,只不过它提供了一些额外的细节来减少样板码。

for循环的开头设置了初始变量(在您的例子中是x)、条件子句(x <= 9)和增量器(x++)。while循环不执行这些操作,它只是在满足()中的条件子句时运行一个代码块。

将for循环转换为while很简单-

for(int x = 0; x < 10; x++) {

代码语言:javascript
复制
int x = 0;
while(x < 10) {
    x++;
}

while循环具有与for循环相同的特性,但没有语法糖。这将帮助您转换问题中的循环,以及通常情况下的循环。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19128542

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档