我如何把这个循环到Java中的while循环(使用数组)? [英] How do I convert this for loop into a while loop (with arrays) in Java?

查看:883
本文介绍了我如何把这个循环到Java中的while循环(使用数组)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我必须做学校的分配,我创建一个阵列方案。我练习如何为循环转换为while循环,但我不能把握的概念。如果我有循环:

  INT []列表=新INT [5];的for(int i = 0;我小于5;我++){
    名单由[i] = I + 2;
}

我将如何使它成为一个while循环?

还有,对不起,这是使用该网站我第一次:)

编辑:这是我的尝试

  INT []列表=新INT [5];
INT I = 0;而(ⅰ小于5){
    名单由[i] = I + 2;
    我++;
}
 System.out.print(名单[I] +);

这是我认为应该做的事,但它在我的电脑上的错误出现。

编辑#2:
错误说:异常线程mainjava.lang.ArrayIndexOutOfBoundsException:5在Arrays2.main(Arrays2.java:21)

打印code:

  System.out.print(名单[I] +);
//这是行21 ^^


解决方案

一个基本的for语句的一般结构是:

 为(ForInit;防爆pression; ForUpdate)声明


  • ForInit 是初始化。这是第一次运行设置变量等。

  • 防爆pression 是一个布尔条件进行检查,看是否语句应该运行

  • 语句是,如果防爆pression 是真的要运行code块

  • ForUpdate 语句后运行来如更新变量作为必要

ForUpdate 已运行,防爆pression 重新评估。如果它仍然是真实的,语句再次执行,那么 ForUpdate ;重复上述操作直至防爆pression 是假的。

您可以按如下调整这是一个while循环:

  ForInit;
而(前pression){
  声明;
  ForUpdate;
}

为了这种模式应用到真正的循环,刚刚替补您的块如上述。

对于上面的例子:


  • ForInit => INT I = 0

  • 防爆pression => I< 5

  • ForUpdate => 我++

  • 语句 => 列表[我] = I + 2;

将其组合在一起:

  INT I = 0;
而(ⅰ小于5){
  名单由[i] = I + 2;
  我++;
}

So I have to do an assignment for school and I am creating an arrays program. I am practicing how to convert for loops to while loops, but I cannot grasp the concept. If I have the for loop:

int [] list = new int [5];

for (int i = 0; i < 5; i++) {
    list [i] = i + 2;
}

How would I make it a while loop?

also, sorry this is my first time using the site :)

edit: here's my attempt

int [] list = new int [5];
int i = 0;

while (i<5) {
    list [i] = i + 2;
    i++;
}
 System.out.print(list[i] + " ");

This is what I think should be done, but it comes up as an error in my computer.

edit #2: The error says: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 at Arrays2.main(Arrays2.java:21)

The print code:

System.out.print(list[i] + " ");
//this is line 21^^     

解决方案

The general structure of a basic for statement is:

for ( ForInit ; Expression ; ForUpdate ) Statement

  • ForInit is the initializer. It is run first to set up variables etc.
  • Expression is a boolean condition to check to see if Statement should be run
  • Statement is the block of code to be run if Expression is true
  • ForUpdate is run after the Statement to e.g. update variables as necessary

After ForUpdate has been run, Expression is evaluated again. If it is still true, Statement is executed again, then ForUpdate; repeat this until Expression is false.

You can restructure this as a while loop as follows:

ForInit;
while (Expression) {
  Statement;
  ForUpdate;
}

In order to apply this pattern to a "real" for loop, just substitute your blocks as described above.

For your example above:

  • ForInit => int i = 0
  • Expression => i < 5
  • ForUpdate => i++
  • Statement => list [i] = i + 2;

Putting it together:

int i = 0;
while (i < 5) {
  list[i] = i + 2;
  i++;
}

这篇关于我如何把这个循环到Java中的while循环(使用数组)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆