Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Number of inputs represented as * in java

  • 13-12-2010 1: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: 168 ✭✭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