Results 1 to 4 of 4
- 11-17-2012, 01:31 AM #1
Member
- Join Date
- Nov 2012
- Posts
- 1
- Rep Power
- 0
Program that converts user input of ints into asterisks
Basically these are the assignment specs :
Create a program that will allow the user to enter an arbitrary number of integers that are in the range 1 to 10 inclusive. After all input has been accepted, display all of the values that were entered in the form of a graph. Use a modular approach in your solution.
graph example:
1. ****
2. ***
3. *********
4. *******
5. ***
6. *
7. *******
8. *********
9. **
10. ****
etc...
So far what i have gotten is :
---------------------------------------------------------------------------------------------
package projects;
public class SimpleGraph
{
static Utilities util = new Utilities();
public static void main(String[] args)
{
int maxSize = 20, count = 0;
int[] aGraph = new int[maxSize];
int input = util.getSafeInt("Enter a whole number between 1-10 [0 to quit]: ");
while (input != 0 && input < 11 && count < maxSize)
{
aGraph[count] = input;
count++;
if (count < maxSize)
{
input = util.getSafeInt("Enter another whole number between 1-10 [0 to quit]: ");
}
}
util.writeln(" ");
for(int index = 0; index < count; index ++)
{
util.writeln((index+1)+ ". " + aGraph[index]);
}
}
}
---------------------------------------------------------------------------------------------
What i have gotten so far asks the user for input in ints (up to 20) and displays the numbers in order back at the user after the user is done entering the numbers. The user may also exit early by pressing 0 which will display the numbers they have entered in order as well.
All input is welcome :)
- 11-17-2012, 02:12 AM #2
Member
- Join Date
- Nov 2012
- Posts
- 14
- Rep Power
- 0
Re: Program that converts user input of ints into asterisks
If you want people to read your code you must put the CODE tags around them.
Java Code:public class SimpleGraph { static Utilities util = new Utilities(); public static void main(String[] args) { int maxSize = 20, count = 0; int[] aGraph = new int[maxSize]; int input = util.getSafeInt("Enter a whole number between 1-10 [0 to quit]: "); while (input != 0 && input < 11 && count < maxSize) { aGraph[count] = input; count++; if (count < maxSize) { input = util.getSafeInt("Enter another whole number between 1-10 [0 to quit]: "); } } util.writeln(" "); for(int index = 0; index < count; index ++) { util.writeln((index+1)+ ". " + aGraph[index]); } } }
- 11-17-2012, 02:12 AM #3
Member
- Join Date
- Oct 2012
- Location
- Tempe, Arizona
- Posts
- 77
- Blog Entries
- 12
- Rep Power
- 0
Re: Program that converts user input of ints into asterisks
You only need an array of 10 that will accept an infinite amount of inputs. All you need to do is just:
This will store then number of occurances to the corresponding array index; minus 1 of course.Java Code:arrayOf10Name[userInput - 1]++;
The way that you are proceeding would force you to look through the entire array and count the occurances for each number before displaying the correct amount of asterisks. Using the way I depicted would be much easier, and allow an unlimited number of inputs.Catching exceptions is for communists
- 11-17-2012, 09:07 AM #4
Re: Program that converts user input of ints into asterisks
Why do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Program not waiting for user input
By kkid in forum New To JavaReplies: 2Last Post: 11-11-2012, 05:17 AM -
Java Program that converts numbers to binary using strings concatenation some how.
By lemony7 in forum New To JavaReplies: 5Last Post: 03-07-2012, 04:10 AM -
user input program
By myalani in forum New To JavaReplies: 4Last Post: 10-28-2011, 04:07 AM -
how to input unspecified number if ints with a scanner
By ftrengnr in forum New To JavaReplies: 2Last Post: 09-14-2010, 01:24 AM -
How do I write to an existing file in Java with a program that asks for user input?
By gmoney8316 in forum New To JavaReplies: 13Last Post: 04-16-2010, 02:51 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks