drjava - Java-Storing values in a while loop -
i beginner in java , have assignment on loops. know question may sound basic couldn't find understandable answer. code looks like;
import java.util.scanner; public class histogram { public static void main( string[] args) { scanner scan = new scanner( system.in); // constant final string ch = "*"; // variables int number; int count; int start; int width; int line; string output; // program code count = 0; number = 0; start = 1; width = 0; line = 0; output = ""; // creating loop while ( start == 1) { system.out.println( "enter numbers"); number = scan.nextint(); if ( number < 0 ) { start = 0; } else { while ( width < number) { output = ch + " " ; width = width + 1 ; system.out.print(output); } width = 0; } } } }
this runs output of stars printed after each user input. want store output each loop , print them @ end when negative integer entered. how can store outputs don't know how many of them entered user? in short want have following output
enter numbers : 3 5 6 4 -1 //numbers user inputs
3 *** 5 ***** 6 ****** 4 ****
this answer:
import java.util.scanner; public class histogram { public static void main( string[] args) { scanner scan = new scanner( system.in); // constant final string ch = "*"; int count = 0; int number = 0; int start = 1; int width = 0; int line = 0; string output = ""; string store=""; // creating loop while ( start == 1) { system.out.println( "enter numbers"); number = scan.nextint(); if ( number < 0 ) { start = 0; } else { while ( width < number) { output = ch + " " ; width = width + 1 ; store+=output; } width = 0; } } system.out.print(store); } }
Comments
Post a Comment