Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Doing this program without a "do" statement

$
0
0
I'd like to not make use of the "do" statement and instead only use a while. The problem is I don't know how to execute the while statement without looping forever.


import java.util.*;

public class ArbitraryNumbers
{

public static void main(String[] args)
		{
			// local variables
			Scanner scan = new Scanner (System.in); // used to read integers being entered
			int[] numbersArray;                     // iniatilizes array for numbers entered
			int[] histogram = new int[11];          // new array for groups of numbers 0 - 11
			// group of numbers to be counted
			String[] groups = {"0 | ", "1 | ", "2 | ", "3 | ", "4 | ", "5 | ", "6 | ", "7 | ", "8 | ", "9 | ", "10 | " };
			String msg;                             // Used to scan lines entered by user
			boolean isDone = false;                 // initialize exit statement to false

			/********************   Start main method  *****************/

			// Give instructions on how to input numbers
			System.out.println("\n\n                          Number Counting \n\n" +
	                           "                                                \n" +
	                           "                   After each Input press ENTER \n\n" +
	                           "                          Enter Numbers: ");

			// DO Scan the next line of input
			do
			{
				// Scan the first input
				msg = scan.nextLine();

				// IF the line of text is not "done"
				if (!msg.equals("done"))
	            {
					// Pass the integer into the conditions below
					int number = Integer.parseInt(msg);
						// IF the number is between 0 and 10
						if (number > -1 && number < 11)
					    {
						 // Add a counter to the number entered
						 histogram [number] += 1;
					    }
			             // Tell user he has enetered an invalid number
			             else System.out.println ( " Please enter a valid number " );
				}

				// ELSE done becomes true
				else
				{
				  // Set Boolean to true
			      isDone = true;
			    }
			 }
				// WHILE user decides to enter Numbers
				while (!isDone);
				{


					// Display blank lines to show different Output Screen
					for (int enoughLines = 0; enoughLines <= 30; enoughLines++)

						// Print an empty space
						System.out.println();
						System.out.println("\t\t\t\t" + " RESULTS " );

						// FOR each number between 0 and 11
						for (int NumberGroups = 0; NumberGroups<histogram.length; NumberGroups++)
						{
							// Display the Numbers 0 through 11

							System.out.print("\t\t\t\t" + groups[NumberGroups]);

							// Initialize count to equal the number of groups we have
							int count = histogram[NumberGroups];

								// FOR each number
								for(int asteriks = 0; asteriks < count; asteriks++)
								{
									// Display an asteriks for that number
									System.out.print("*");
								}
									// Skip a line for the next number to have an asterik
									System.out.print("\n");
						}
					}
		}
}

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>