How do I print a set amount of characters corresponding to the value of a variable?

StackOverflow https://stackoverflow.com/questions/20314827

  •  07-08-2022
  •  | 
  •  

Pergunta

I'm trying to make a system where, someone inputs a number between 0-100, and then the program will allocate the number to a set boundary, basically a grade storing system. I'll show you my code, then elaborate.

public static void main(String[] args) {
//Boundary0/30/40/70 indicates which band the counter is for. e.g. 0-29, 30-39 etc
int Boundary0 = 0;
int Boundary30 = 0;
int Boundary40 = 0;
int Boundary70 = 0;
int Grade;
int count;
count = 0;
Scanner in = new Scanner(System.in);
//Read in first number
System.out.print("Enter an Integer");
 Grade = in.nextInt();



 while (Grade < 100){    
   //To count number of students
   count++;
   //To allocate each grade to corresponding tier
   if(Grade >= 0 && Grade <= 29){
          Boundary0++;
   }
   if(Grade >= 30 && Grade <= 39){
          Boundary30++;
   }
   if(Grade >= 40 && Grade <= 69){
          Boundary40++;
   }
   if(Grade >= 70 && Grade <= 100){
          Boundary70++;
   }
   Grade = in.nextInt();      
 }

 //To print each boundary seperately with the number of marks in each tier and overall total

    System.out.println("0-29:" + " " + Boundary0 );       
    System.out.println("30-39:" + " " + Boundary30);               
    System.out.println("40-69:" + " "+ Boundary40);        
    System.out.println("70-100:" + " " +Boundary70);        
    System.out.println("Amount of Students: " + count);
}
}

As the user inputs a number, the program will add 1 to the corresponding boundary variable

and then when the user inputs a number greater than 100, the program stops and prints the

tiers and beside each tier, tells the user how many values were in each tier.

So What I'm trying to do is, at the end of the code, where the sout commands are, to

instead of literally saying how many numbers fell into each section, I want to represent

the value with *s for example

0-29: *****
30-39: ****
40-69: ********
70-100: *****

Sorry if I'm not being totally clear, I think that may just be a lack of understanding myself...

Thanks

Foi útil?

Solução

create a reusable method that print's * for you -

public static void printStars(int n){
   for(int i = 0 ; i < n ; i++)
     System.out.print("*");
}

and call it like this -

System.out.println("0-29:" + " " + printStars(Boundary0));
System.out.println("30-39:" + " " + printStars(Boundary30));               
System.out.println("40-69:" + " "+ printStars(Boundary40));        
System.out.println("70-100:" + " " +printStars(Boundary70));

Outras dicas

Just add additional for-cycles:

System.out.print("0-29: ");
for(int i = 0; i < Boundary0; i++){
    System.out.print("*");
}
System.out.println("");

I think you have already achieved what you desire for with your code. You just need to print those :

At the end of your code , add following piece :

System.out.print("0-29: ");
for(int i=0;i<Boundary0;i++)
{
 System.out.print("*");
}
System.out.println("");

System.out.print("30-39: ");
for(int i=0;i<Boundary30;i++)
{
 System.out.print("*");
}
System.out.println("");

System.out.print("40-69: ");
for(int i=0;i<Boundary40;i++)
{
 System.out.print("*");
}
System.out.println("");

System.out.print("70-100: ");
for(int i=0;i<Boundary70;i++)
{
 System.out.print("*");
}
System.out.println("");

This would definitely solve the purpose and goes in sync with the code that you have written.

But my personal suggestion would be to use a HashMap<String,Integer>; to implement it.

Hope this helps

EDIT!!!

This seems wordy. Try the reusable function for printing '*' in the answer by Mohd. Adil.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top