Results 1 to 8 of 8
Thread: Turning numbers into asterisks.
- 02-24-2009, 08:01 AM #1
Member
- Join Date
- Feb 2009
- Posts
- 3
- Rep Power
- 0
Turning numbers into asterisks.
I've only been working with java for probably two weeks so you'll have to excuse the mess. The program I have here has an array set for a high number, runs down and asks the user to input numbers (assuming numbers are in increments of 100), then divides the numbers by 100 and outputs them. What I'm looking to do is instead of just outputting the numbers, I'd like to have asterisks represent for numbers. For every 100 that the user types in, will output a single *. So say the user inputs 1400, it divides it by 100, from this point, how do I get an output of **************?
Thanks in advance!
Java Code:import java.util.Scanner; public class Astericks3 { /** * @param args */ public static void main(String[] args) { //priming reed int exitCheck = 0; //used to exit loop when 999 is typed int salesCount = 1; //used to help indicate order during final output int Count = -1; //used to cut off the 999 once exited loop int[] inputSales = new int[9999]; Scanner input = new Scanner(System.in); //initial statement System.out.println("Please enter first sales figure"); //main loop for (int i = 0; i < inputSales.length; i++ ) if(exitCheck != 999) { inputSales[i] = input.nextInt(); System.out.println("Enter next sales figure, or 999 to exit: "); exitCheck = inputSales[i]; ++Count; } //dividing the input by 100 for (int i = 0; i < Count; ++i) { inputSales[i] = inputSales[i]/100; } //system output { for ( int i = 0; i < Count; ++i ) { System.out.println("Number " + salesCount + " sales: " + inputSales[i]); salesCount++; } System.out.println("Thank you!"); } } }
- 02-24-2009, 08:15 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
You can find the whole number for division. Say the input is 1500, then divide by 100 gives the answer is 14. So in a loop print the asterisk sign. Don't use the new line, that's all.
Java Code:for(int index = 0; index < value; index++) { System.out.print("*); }
- 02-24-2009, 08:39 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,542
- Rep Power
- 11
- 02-24-2009, 09:14 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Oops, it's a simple mistake. Once divide by 100, answer should be 15. ;)
- 02-24-2009, 05:15 PM #5
Member
- Join Date
- Feb 2009
- Posts
- 3
- Rep Power
- 0
So from what I'm understanding, you're saying my output statement should look something like this(below). I'm doing that and it's only printing the amount of numbers I put in. Say I input 900, 1000, 1100, it's only outputting three asterisks.
Java Code:for(int i = 0; index < inputSales[i]; i++) { System.out.print("*"); }
Thanks for the help up to this point!
- 02-24-2009, 08:29 PM #6
Java Code:for(int j=0; j<inputSales.length; j++) for(int i = 0; index < inputSales[j]/100; i++) System.out.print("*");USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 02-25-2009, 06:09 AM #7
Member
- Join Date
- Feb 2009
- Posts
- 3
- Rep Power
- 0
Alright, I think I understood it. I took angryboys code fragment and modified it a bit. I included the final program in case anybody else bumps into this during a search.
Thanks for the help guys!
Java Code:import java.util.Scanner; public class Asterisks3 { /** * @param args */ public static void main(String[] args) { //priming reed int exitCheck = 0; //used to exit loop when 999 is typed int salesCount = 1; //used to help indicate order during final output int Count = -1; //used to cut off the 999 once exited loop int[] inputSales = new int[9999]; Scanner input = new Scanner(System.in); //main loop for (int i = 0; i < inputSales.length; i++ ) if(exitCheck != 999) { System.out.println("Enter sales figure " + salesCount + ", or 999 to exit: "); inputSales[i] = input.nextInt(); exitCheck = inputSales[i]; ++Count; ++salesCount; } //resetting salesCount salesCount = 1; //System output for(int r=0; r<Count; r++) { System.out.print("Number " + salesCount + " sales: "); for(int i = 0; i < inputSales[r]/100; i++) { System.out.print("*"); } ++salesCount; System.out.println(" "); } System.out.println("Thank you!"); } }
- 02-25-2009, 07:38 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
While working on read more about arrays. How to use elements and length of it.
Anyway, it's pleasure to help you. If you have solved the problem please mark thread solved.
Similar Threads
-
printing two smallest numbers from a series of numbers
By trofyscarz in forum New To JavaReplies: 2Last Post: 10-14-2008, 11:46 PM -
Prime numbers
By tercius in forum New To JavaReplies: 3Last Post: 05-04-2008, 06:05 AM -
printing an "E" out of asterisks via strings
By hokieman07 in forum New To JavaReplies: 1Last Post: 04-08-2008, 05:45 AM -
asterisks triangles
By Dan121 in forum New To JavaReplies: 1Last Post: 01-12-2008, 07:42 PM -
random numbers
By carlos123 in forum New To JavaReplies: 1Last Post: 12-22-2007, 02:56 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks