Results 1 to 5 of 5
Thread: star array
- 11-23-2008, 08:17 AM #1
Member
- Join Date
- Oct 2008
- Posts
- 19
- Rep Power
- 0
star array
Hi,
Iam writting program with array. This program should count the numbers of star equal to each element in the array.
the output should be as:
0|****(4)
1|**(2)
3|*********(9)
4|************(12)
5|********(8)
6|***(3)
7|*******(7)
Java Code:/** This program will calculate the numbers of stars equal to each element of the array. */ public class StarNumber { public static void main(String args[]) { int[] numbers={4,2,9,12,8,3,7}; for (int index=0; index<numbers.length; index++) System.out.println((index) + "|*" +numbers[index]); } }
0|*4
1|*2
2|*9
3|*12
4|*8
5|*3
6|*7
-
We wish you well with your program.
BTW, did you have a question?
- 11-23-2008, 08:39 AM #3
Member
- Join Date
- Oct 2008
- Posts
- 19
- Rep Power
- 0
Hi,
ofcourse I have a question ??
I want to know what is missing in my program to get the required output ?
- 11-23-2008, 08:41 AM #4
Member
- Join Date
- Aug 2008
- Posts
- 31
- Rep Power
- 0
Nanna,
I had this problem a long time back. Try using another for loop inside your existing for loop.
Remember what you're trying to achieve. The first for loop wants to traverse through the total number of array elements (your initial for loop is a great start!). Now, to print the asterisks (*), you need to use a second for loop inside your existing loop.
What's the purpose of your second for loop? We want to create the asterisks and the number of asterisks depends on the value at a certain index of your array. Iterate that many times. For example, at index [0], we have a value of 4. Iterate from 0 to 4, and at each iteration print out an asterisk.
- ofcourse I have a question ??
Similar Threads
-
Star pattern help?
By GeeKunMow in forum New To JavaReplies: 13Last Post: 07-21-2011, 06:59 AM -
creating a Star
By jhen in forum New To JavaReplies: 2Last Post: 11-19-2008, 04:41 AM -
Replace number to the star
By linux0kernel in forum New To JavaReplies: 5Last Post: 10-15-2008, 10:39 AM -
help with an array.
By f_the_cook in forum New To JavaReplies: 7Last Post: 06-03-2008, 05:05 AM -
Array Reflection: Multi Array Reflection
By Java Tip in forum java.langReplies: 0Last Post: 04-23-2008, 09:08 PM
Bookmarks