Adriano Cruz
Adriano Cruz

Reputation: 13

input from the keyboard and print out asterisks

Hi there I'm trying to input 3 integers from the keyboard and print rows of asterisks equal to the integers input from keyboard. I really appreciate if someone could help on this, thanks in advance.

public class Histogram1 {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Please input the first integer for histogram1: ");
        int a1 = in.nextInt();

        System.out.print("Please input the second integer for histogram1: ");
        int b1 = in.nextInt();
        System.out.print("Please input the third integer for histogram1: ");
        int c1 = in.nextInt();
        histogram1();            
    }

    public static void histogram1(){            
        int n =0;
        for (int i = 0; i <= n; i++)
        {
            for( int j = 1; j <=n; j++)
            {
                System.out.println("*");
            }
            System.out.println();
        }
    }           
}

Upvotes: 1

Views: 2607

Answers (1)

zmo
zmo

Reputation: 24812

is it what you want ?!

  1. your n variable was always =0 : I think you wanted an argument instead
  2. you had 2 loops imbricated : you were printing out (n*(n-1)) *, one per line (but as n=0 none appeared)
  3. for that purpose, do you really need a scanner ?.. System.in.read() can do the job, and you don't have to manage your Scanner.
  4. as you asked for not having an argument, you can use a static variable in your class. I also changed the names to more meaningful variable names, as it is always a good idea to correctly choose good names for variables.

    public class Histogram1 {

    static int nb_stars=0;
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
    
        Scanner in = new Scanner(System.in);
        System.out.print("Please input the first integer for histogram1: ");
        nb_stars = in.nextInt();
        show_histogram();
    
        System.out.print("Please input the second integer for histogram1: ");
        nb_stars = in.nextInt();
        show_histogram();
    
        System.out.print("Please input the third integer for histogram1: ");
        nb_stars = in.nextInt();
        show_histogram();
    
    }
    public static void show_histogram(){
            for(int j=1; j<=nb_stars; ++j)
            {
                System.out.print("*");
            }
            System.out.println();
        }
    }
    

    }

Upvotes: 1

Related Questions