开发者

pyramid sequence in Java

I am new to Java. Just now I'm practing. If I give input as 6, output should be like this:

1
2 3
4 5 6

Here I'm posting code that I tried:

import java.util.Scanner;

public class开发者_JS百科 Number {
    public static void main(String args[]){

        int n;
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        in.close();
        int k = 1;
        for (int i = 1; i <= n; i++) 
        {
            // k=i;
            for (int j = 1; j <= i; j++)
            {
                System.out.print(" " + k);
                if (n==k)
                {
                    break;
                }
                k++;
            }       
            System.out.println("\n");
        }
    }
}

If I input n=4,i t show the output as:

1

2 3

4

4


Your break will only exit the inner loop (the one that loops over j). The outer loop will continue to run, leading to extra numbers being printed.

You need to either replace it with a return; or System.exit(0), or put a label in front of your outer loop (the one that loops over i) and use a labeled break.


Properly indent your code. It helps your brain to understand.

That said, the solution is two loops with three variables.

You need a loop that goes from 1 to n.

An inner loop that goes from 1 to the number of elements per line.

And you need the number of elements per line. This variable increases every time the inner loop is executed.


It's a badly worded question, but I'm going to guess you want to know why the extra 4?

The reason is you have nested loops, so the break only breaks one loop. Here's how you break out of the outer loop:

outer: for (int i = 1; i <= n; i++) {
...
break outer;

The label outer is arbitrary - you can call it fred is you want.


int n = 6; // target

int i = 0;
int nextRowAt = 2;
int currentRow = 1;
while (++i <= n) {
    if (i == nextRowAt) {
        System.out.println();
        nextRowAt = ++currentRow + i;
    }
    System.out.print("" + i + " ");
}

But unless you understand it and can properly explain the code, you will probably get a fail on your assignment.

My suggestion is to start by creating/understanding on pen and paper. Write out the sequences and figure out how the algorithm should work. THEN you start coding it.


int sum =0;
int n =10;
//        n------> number till where you want to print
boolean limtCrossed = false;
for (int i = 0; i < n &&!limtCrossed; i++) {
 for(int j=0;j<=i;j++) {
  sum++;
  if (sum>n) {
    limtCrossed = true;
    break;
  }
  System.out.print(+ sum +" " );
 }

 System.out.println("\n");

} 


public static void main(String[] args) 
{
    int n = 10;
    int k = 1;
    boolean breakOuter = false;
    for (int i = 1; i <= n; i++) 
    {
    for (int j = 1; j <= i; j++)
    {
        System.out.print(" " + k);
        if (n==k)
        {
        breakOuter = true;
        break;
        }
        k++;
    }
    if(breakOuter) break;
    System.out.println("\n");
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜