Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.
Hi all, please see this major site announcement: https://www.boards.ie/discussion/2058427594/boards-ie-2026

Number of inputs represented as * in java

  • 13-12-2010 01:48PM
    #1
    Registered Users, Registered Users 2 Posts: 302 ✭✭


    Hi,

    I am inputting numbers and counting the amount that are entered but I want to represent each number entered as a star. So for each number that is entered a star should appear.

    Here is what I have so far.
    import java.io.*;


    public class Main {

    public static void main(String[] args)throws IOException {

    int mark = 0;
    int no_of_marks1 = 0;
    int no_of_marks2 = 0;
    int no_of_marks3 = 0;
    int no_of_marks4 = 0;

    BufferedReader br = new BufferedReader(newInputStreamReader(System.in));


    while(mark < 101)
    {
    System.out.println("Input mark: ");
    mark = Integer.parseInt(br.readLine());

    if((mark >= 0) && (mark < 30))
    {
    no_of_marks1++;

    }

    if((mark > 29) && (mark < 40))
    {
    no_of_marks2++;

    }

    if((mark > 39) && (mark < 70))
    {
    no_of_marks3++;

    }

    if((mark > 69) && (mark < 101))
    {
    no_of_marks4++;

    }

    if(mark > 100)
    {
    break;
    }


    }



    System.out.println("\nResults:\n");
    System.out.println("0-29: " + no_of_marks1 + "\n");
    System.out.println("30-39: " + no_of_marks2 + "\n");
    System.out.println("40-69: " + no_of_marks3 + "\n");
    System.out.println("70-100: " + no_of_marks4 + "\n");

    So instead of the above display, it would look like the following or something similar:


    0-29 *******

    30-39 ***

    40 - 69 *****

    70-100 *********

    Any help appreciated!


Comments

  • Registered Users, Registered Users 2 Posts: 174 ✭✭pauraic1990


    Try the following

    System.out.println("\nResults:\n");
    System.out.print("0-29: ");
    int i =0;
    while(i < no_of_marks1){
    System.out.print("*");
    i++;
    }
    System.out.println();
    System.out.print("30-39: ");
    int i =0;
    while(i < no_of_marks2){
    System.out.print("*");
    i++;
    }
    System.out.print();
    System.out.println("40-69: ");
    int i =0;
    while(i < no_of_marks3){
    System.out.print("*");
    i++;
    }
    System.out.print();
    System.out.println("70-100:");
    i =0;
    while(i < no_of_marks4){
    System.out.print("*");
    i++;
    }
    System.out.println();


  • Registered Users, Registered Users 2 Posts: 2,781 ✭✭✭amen


    instead of repeating your mark printing you could create a function markPrinting passing the text you want and the number mark counter.

    Be lazy and never write the same code more than once.


Advertisement