Tuesday, 15 September 2015

java - How to reverse a string of numbers in a nested loop -



java - How to reverse a string of numbers in a nested loop -

this homework question help, not answer.

i'm trying create 2 triangles out of numbers based on number entered user.

"enter number between 2-9: "3" 1 12 123 1 21 321

ie2:

"enter number between 2-9: "5" 1 12 123 1234 12345 1 21 321 4321 54321

i have been able first triangle complete. when add together nested loop messes first triangle numbers developed nested loop. puts numbers in straight vertical line. i've tried variations different nest loops , tried messing stringbuilder, still unsuccessful. here's have in code far:

import java.util.scanner; public class nestedloops { public static void main(string[] args) { scanner input = new scanner(system.in); system.out.print("enter number between 2-9: "); int width = input.nextint(); string r = ""; (int = 1; <= width; i++) { r = r + i; system.out.println(r); } } }

again, i'm looking help/understanding , not answer.

try

int width = 5; // lines; number of lines = width (int line = 1; line <= width; line++) { // print numbers 1 current line number (int n = 1; n <= line; n++) { system.out.print(n); } // end of line system.out.println(); } // add together empty line between triangles system.out.println(); // lines; number of lines = width (int line = 1; line <= width; line++) { // printing padding spaces, number of spaces = - line number int nspaces = width - line; (int = 0; < nspaces; i++) { system.out.print(" "); } // print numbers number of current line 1 (int n = line; n >= 1; n--) { system.out.print(n); } // end of line system.out.println(); }

java

No comments:

Post a Comment