Use a variable that is in the main method in another method

I am trying to create a simple program to output the number of stars entered by the user. I am trying to learn how to use more than one method for this. Here's my code

import java.util.Scanner;
public class Alpha 
{
    public static void main(String args[])
    {
        Scanner input = new Scanner(System.in);
         int n;

        System.out.println("Enter no. of stars");
        n = input.nextInt();

    }
    public static void Loop ()
    {
        for (int counter = 1; counter <= n; counter++)
        {
            System.out.println("*");
        }
    }
}

      

The problem I am facing is that in the Loop method I cannot use the variable n Is there a way to use the variable that is in the main method in the other? Ty

-Pingu

+3


source to share


6 answers


import java.util.Scanner;
public class Alpha 
{
public static void main(String args[])
{
    Scanner input = new Scanner(System.in);
     int n;

    System.out.println("Enter no. of stars");
    n = input.nextInt();

    Loop(n); //calls Loop function and passes parameter n
}
public static void Loop(int n)  //this function now expects a number n
{
    for (int counter = 1; counter <= n; counter++)
    {
        System.out.println("*");
    }
}
}

      



+2


source


just pass it as a parameter:



public static void main(String args[])
    {
        Scanner input = new Scanner(System.in);
         int n;

        System.out.println("Enter no. of stars");
        n = input.nextInt();
        Loop(n);

    }
    public static void Loop (int count)
    {
        for (int counter = 1; counter <= count; counter++)
        {
            System.out.println("*");
        }
    }

      

+2


source


Pass it as a parameter

import java.util.Scanner;
public class Alpha 
{
    public static void main(String args[])
    {
        Scanner input = new Scanner(System.in);
         int n;    
        System.out.println("Enter no. of stars");
        n = input.nextInt();    
        loop(n); // added this

    }
    public static void loop (int n) // changed here
    {
        for (int counter = 1; counter <= n; counter++)
        {
            System.out.println("*");
        }
    }
}

      

+2


source


I think you should use it as an instance variable, and for better understanding, name your class like StarClass

, it can provide better understanding. Good programming practice.

But you should avoid over-executing the instance variable without any logic.

+1


source


I also think that you can declare n as a public variable. This should make it available throughout your code.

public int n;

But my guess is that passing it as a parameter is best practice since you are not creating a difference inside your code. What I mean is that if something changes with a variable, you are breaking the function. It is good practice to always keep things "modular" in your code, so it makes it more resilient to change and debug. It's better if you get used to it from the start =)

0


source


Two ways. one has already been posted as a response and the other will use a variable as a field. This way you can access (and change) it in every method without having to pass it.

public class Alpha 
{

    static int n;
    public static void main(String args[])
    {
        Scanner input = new Scanner(System.in);

        System.out.println("Enter no. of stars");
        n = input.nextInt();
        loop();
    }
    public static void loop ()
    {
        for (int counter = 0; counter < n; counter++)
        {
            System.out.println("*");
        }
    }
}

      

And please run the method names in lower case and count them with 0. This is common practice and it helps a lot to use standards from the beginning.

0


source







All Articles