Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I'm working on a java program that creates a histogram. An asterisk is printed whenever someone inputs an integer in a certain set of numbers (i.e. 1-10; 11-20, etc.). I made that. No problem. Now another project wants me to revise what I originally did and make an asterisk add once when a number is inputted from a range 5 time. It should not print any of the left overs. (i.e. Someone inputs 6 numbers from a range of 1-10...An asterisk should be printed only once.) I'm having a lot of trouble with this...Here's the original code. (I'm new to coding so this solution might not be the most efficient.) I tried using mod (%), which almost worked, but not quite.

Java
import java.util.*;
public class sixFour
{
  public static void main(String[] args)
  {
{
  Scanner scan = new Scanner (System.in);
  int count1 = -1;
  int count2 = -1;
  int count3 = -1;
  int count4 = -1;
  int count5 = -1;
  int count6 = -1;
  int count7 = -1;
  int count8 = -1;
  int count9 = -1;
  int count10 = -1;
  
  int[] array = new int [10];
  for (int count = 0; count < array.length; count++)
  {
    System.out.println("Enter an int from 1-100.");
    int input = scan.nextInt();
    if (input > 0 && input <= 100)
      
    {
      array[count] = input;     
      if (array[count] >=1 && array[count] <= 10)
      { 
       count1 ++;
      }
      else if (array[count] >= 11 && array[count] <= 20)
      {
        count2 ++;
      }
      else if (array[count] >= 21 && array[count] <= 30)
      {
        count3++;
      }
      else if (array[count] >= 31 && array[count] <= 40)
      {
        count4 ++;
      }
      else if (array[count] >= 41 && array[count] <= 50)
      {
        count5++;
      }
      else if (array[count] >= 51 && array[count] <= 60)
      {
        count6 ++;
      }
      else if (array[count] >= 61 && array[count] <= 70)
      {
        count7++;
      }
      else if (array[count] >= 71 && array[count] <= 80)
      {
        count8++;
      }
       else if (array[count] >= 81 && array[count] <= 90)
      {
        count9++;
      }
       else if (array[count] >= 91 && array[count] <= 100)
      {
        count10++;
       }
    }
    else
    {
      System.out.println("NO.");
    }
  }
  System.out.print("1-10:  ");
    for (int final1 = 0; final1 <= count1; final1 ++)
    {
      System.out.print("*");
    }
  
  System.out.println();
  
  System.out.print("11-20: ");
  for (int final2 = 0; final2 <= count2; final2 ++)
  {
    System.out.print("*");
  }
  
  System.out.println();
  
  System.out.print("21-30: ");
  for (int final3 = 0; final3 <= count3; final3 ++)
  {
    System.out.print("*");
  }
  
  System.out.println();
  
  System.out.print("31-40: ");
  for (int final4 = 0; final4 <= count4; final4 ++)
  {
    System.out.print("*");
  }
  
  System.out.println();
  
  System.out.print("41-50: ");
  for (int final5 = 0; final5 <= count5; final5 ++)
  {
    System.out.print("*");
  }
  
  System.out.println();
  
  System.out.print("51-60: ");
  for (int final6 =  0; final6 <= count6; final6 ++)
  {
    System.out.print("*");
  }
  
  System.out.println();
  
  System.out.print("61-70: ");
  for (int final7 = 0; final7 <= count7; final7 ++)
  {
    System.out.print("*");
  }
  
  System.out.println();
  
 
  System.out.print("71-80: ");
  for (int final8 = 0; final8 <= count8; final8 ++)
  {
    System.out.print("*");
  }
  
  System.out.println();
  
  System.out.print("81-90: ");
  for (int final9 = 0; final9 <= count9; final9 ++)
  {
    System.out.print("*");
  }
  
  System.out.println();
  
  System.out.print("91-100: ");
   for (int final10 = 0; final10 <= count10; final10 ++)
  {
    System.out.print("*");
  }
  
  }
}
}
Posted
Updated 15-Feb-14 6:05am
v2

1 solution

where did you put the mod(%)?

I would think putting them in your for-loops would do it:

C#
for (int final1 = 0; final1 < (count1%5); final1 ++)
{
  System.out.print("*");
}


depending on if you want a "*" for anything below or above 5, you would need either < or <=
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900