[Solved] Begining Java calling an integer to another method (I think)


Lots of errors in the code.This works check it.
Instead of using 5 different variables i have used an array of size 5 to take input.

for(int i=0;i<5;i++)
{
    System.out.print("Please, Enter a number between 1 - 30 "); 
    nb[i]=input.nextInt();
}

As you need to print the nb[i] number of asterisks where nb[i] is ith number in the array so you need to iteratate nb[i] times for each nb[i] in the array

In simple words extracting each nb[i] of the array and iterate nb[i] times

for(int i = 0; i <nb.length; i++)//iterarting over the array to get nb[i]
{
    for(int j=1;j<=nb[i];j++)//iterating nb[i] times where nb[i] is the ith element of the array
    {
        System.out.print("*"); 
    }
    System.out.println(); 
} 

Here is full working code.

import java.util.Scanner; 
public class AsteriskGenerator { 
    public static void main(String[] args){ 

    AsteriskGenerator asteriskGenerator = new AsteriskGenerator(); 

    int nb[]=new int[5];
    Scanner input = new Scanner (System.in); 
    for(int i=0;i<5;i++)
    {
        System.out.print("Please, Enter a number between 1 - 30 "); 
        nb[i]=input.nextInt();
    }
    input.close();


    asteriskGenerator.asteriskGenerator(nb);
    } 
    void asteriskGenerator(int nb[])
    { 
        for(int i = 0; i <  nb.length; i++)
        {
            for(int j=1;j<=nb[i];j++)
            {
                System.out.print("*"); 
            }
            System.out.println(); 
        } 
    } 

} 

Hope it helps.Happy Coding!!

2

solved Begining Java calling an integer to another method (I think)