English 中文(简体)
java:是否有一个循环可以做到这一点?
原标题:java: is there a loop that can do this?
  • 时间:2012-05-26 17:03:43
  •  标签:
  • java
  • loops

需要能够做一个循环, 做类似的事情:

第一次通过环绕 :

for(int i=0;i<5;i++)
{
    example.print(0);
}

第二次通过:

for(int i=0;i<5;i++)
{
    example.print(0);
    example.print(1);
}

以其他示例为例。 打印( ) 每一次都添加 。

In order for the program to work correctly, each "example.print()" has to physically be there is the code. Any ideas?

问题回答

听起来你想要嵌套环:

for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 5; j++) {
        example.print(i + j); // This will need adjusting
    }
}

注:

  • You ll want to adjust the i + j part as necessary to get the desired output. You didn t say what should happen when we got past the first five. :-)
  • You didn t say how many passes you wanted, so I assumed 5. If you want fewer, change the upper limit of the i loop (the outer one), probably.
int loopCounter = 0;

for(int i=0;i<5;i++)
{
    for(int k=0; k<loopCounter; k++)example.print(k);
    loopCounter++;
}

我的版本:

for(int i=0;i<5;i++)
{
    for(int k=0; k<i; k++)
          example.print(k);
}

试试这个

for (int x = 0,y = 0; x < 100; x++,y++) {

        example.print(x + y); // You will need to tweak these values
    }

100是这里的假设值, 因为圆圈会反复循环的时数 。

此处为我的拍摄, * 考虑您发送到打印方法的参数, 是您想要打印的参数 。

int n=3; //n is the highest param value you want your print method to receive, 
         //here it s just 3

for (int i=0; i<n; i++) {
    for (int j=0; j<(i+1)*5; j++) {
        example.print(j/5);
    }
}

谁都能猜到你"身体上"是什么意思 但我会试一试

final Example example = new Example();
for (int i = 0; i < 5; i++)
  switch (i) {
    case 4: example.print(i-4);
    case 3: example.print(i-3);
    case 2: example.print(i-2);
    case 1: example.print(i-1);
    case 0: example.print(i-0);
  }




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签