java - Pyramid Pattern using a Loop -


the expected output follows:

1 12 123 1234 12345 123456 1234567 12345678 123456789 1234567890 12345678901 123456789012 

following starting code use:

import java.util.scanner;  public class pyramid {      /**      * @param args      */     public static void main(string[] args) {         // todo auto-generated method stub         system.out.println("type in integer value");         scanner in = new scanner(system.in);         int input = in.nextint();         string str = "";         for(int i=1;i<=input;i++){             str += i;             system.out.println(str);         }     } } 

following output of now.

type in integer value 15 1 12 123 1234 12345 123456 1234567 12345678 123456789 12345678910 1234567891011 123456789101112 12345678910111213 1234567891011121314 123456789101112131415 

i've been thinking how resolve issue; if write if statement if(i > 9){ = 0; }. reset counter?

how can accomplish task ? missing ?

you can use modulo operator make i loop around 0 once reaches 10:

str += % 10; 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -