Use Variable Which is in main method in Another Method

℡╲_俬逩灬. 提交于 2019-12-23 15:53:33

问题


I'm trying to create a simple program to output the number of stars entered by user. I'm trying to learn how to use more than one method to do 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'm facing is that in the Loop method, I am unable to use the variable n Is there a way to use a variable which is in the main method, in another one? Ty

-Pingu


回答1:


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:


simply pass it as 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("*");
        }
    }



回答3:


Pass it as a paramteer

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("*");
        }
    }
}



回答4:


I think you should use it as a instance variable and for better understanding name your class like StarClass it can provide better understanding. Good programming practice.

But you should avoid unneccesserily making instance variable without any logic behind it.




回答5:


I also think you could declare n as a public variable. That should make it accessible throughout the code.

public int n;

But I guess that passing it as parameter is a better practice, since you don't create a deppendance inside your code. What I mean is, if something changes with the variable you break the function. It's good practice to always keep things "modular" in your code, so it makes it more resilient to changes and debugging. It's better if you get used to it from the beggining =)




回答6:


Two ways.. one has been posted already as answer and the other one would be using the variable as a field. This way you can access (and modify) it in every method without having to pass it on.

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 start method names with lowercase and counting with 0. It's common practise and it helps a lot to use the standards right from the beginning.



来源:https://stackoverflow.com/questions/31610174/use-variable-which-is-in-main-method-in-another-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!