Jay Kandy
Posts: 77
Nickname: jay
Registered: Mar, 2002
|
|
Re: OK So everyone hates me now
|
Posted: Mar 29, 2002 7:07 AM
|
|
I think I can almost see frustration on your face. Actually you were close to getting it working.
Also, I did not clearly understand what you wanted. I only got an idea from your code. So, here goes:
import java.io.*;
class Histogram
{
//----------------------------------------------------------
// Program to display histogram of range of entered numbers.
//----------------------------------------------------------
public static int number = -1;
private static BufferedReader stdin;
public static void main (String[] args) throws IOException
{
int[] count = new int [10];
/**
* I am not sure why you need this line:
*
* int x = (number - 1)/ 10;
*
* So hack it
*/
stdin = new BufferedReader( new InputStreamReader( System.in ) );
System.out.println( "Enter numbers between 1 and 100, or 0 to quit" );
/**
* New Code. index was not declared
*/
int index = 0;
/**
* Addition ends
*/
while (number != 0)//sentinal to stop entering numbers
{
number = Integer.parseInt( stdin.readLine() );
/**
* New Code
*/
if( number > 0 )
{
count[ index ] = number;
index ++;
}
/**
* Addition ends
*/
}
// Loop to generate labels of rows
for (int row = 0; row < count.length; row++)
{
System.out.print((row * 10 + 1) + "- " + ((row + 1) * 10) + " | ");
/**
* I am not sure why you need these lines:
*
* if (x == count[row])
* System.out.print ('*');
* count[row]++;
*
* Instead I added these lines:
*/
int temp = count[ row ];
for( int i=0; i < temp ; i++ )
{
System.out.print( "*" );
}
System.out.println();
}
}
}
Sincerely, Jay
|
|