0

目标:
1234
2345
3456
4567
5678

我有模式下来,但它没有在长度(4)后打印:

    int i;
    int a;

    for (i = 1; i <= 5; i++) 
    {
      for (a = i;a<=i+3;a++)
      {
        System.out.print(a);
      }
    }

我的输出是:12342345345645675678

4

9 回答 9

4

只需在第二个循环之后添加它:

int i;
int a;

for (i = 1; i <= 5; i++) {
  for (a = i;a<=i+3;a++) {
    System.out.print(a);
  }
  System.out.println();
}
于 2010-02-25T07:19:46.643 回答
3
int i;
int a;

for (i = 1; i <= 5; i++) 
{
  for (a = i;a<=i+3;a++)
  {
    System.out.print(a);
  }
  System.out.println(); // add this code
{
于 2010-02-25T07:20:03.650 回答
2

不需要有两个 for 循环,试试:

for (i = 1; i <= 5; i++) {
   int j = i;
   System.out.println(j++ + "" + j++ + "" + j++ + "" + j);
}

编辑:我知道这会限制灵活性,但这只是一个玩具问题。

于 2010-02-25T07:37:55.143 回答
1
int i;
int a;

for (i = 1; i <= 5; i++) 
{
  for (a = i;a<=i+3;a++)
  {
    System.out.print(a);
  }
  System.out.println();
}
于 2010-02-25T07:19:41.917 回答
1

System.out.println()在内循环之后添加。

于 2010-02-25T07:20:10.747 回答
1

尝试:

int i;
int a;

for (i = 1; i <= 5; i++) {
    for (a = i;a<=i+3;a++) {

        System.out.print(a);
    }
    System.out.println(); // this will print a new line.
}
于 2010-02-25T07:20:41.120 回答
0

System.out.Println()在内循环之后添加。这会将光标移动到下一行

于 2010-02-25T07:49:39.343 回答
0

Java 解决方案

  int f, g, h,T;
        f = 12345;
        h = 11111;
        for (g = 1; g <= 5; g++)
        {
         T = f + ((g - 1) * h);
         System.out.print(  T + "\n")
         }
于 2019-04-14T06:30:32.190 回答
0
  for(int i =1;i<=4;i++) {
        System.out.print(i);
    }
    System.out.println();
    for(int i =2;i<=5;i++) {
        System.out.print(i);
    }
    System.out.println();

    for(int i =3;i<=6;i++) {
        System.out.print(i);
    }
    System.out.println();
    for(int i =4;i<=7;i++) {
        System.out.print(i);
    }
    System.out.println();
于 2021-08-02T15:45:42.633 回答