java 在另一个方法中使用主方法中的变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31610174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use Variable Which is in main method in Another Method
提问by nootnoot
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
我面临的问题是,在 Loop 方法中,我无法使用变量 n 有没有办法在另一个方法中使用 main 方法中的变量?泰
-Pingu
-平谷
采纳答案by Maslor
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("*");
}
}
}
回答by Stefan Beike
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("*");
}
}
回答by CoderNeji
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("*");
}
}
}
回答by OldSchool
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.
我认为您应该将它用作实例变量,并且为了更好地理解您的类,就像StarClass
它可以提供更好的理解一样。良好的编程习惯。
But you should avoid unneccesserily making instance variable without any logic behind it.
但是你应该避免在没有任何逻辑的情况下不必要地创建实例变量。
回答by dly
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.
并且请以小写字母开头并以 0 计数的方法名称。这是常见的做法,从一开始就使用标准有很大帮助。
回答by Antonio Marino
I also think you could declare n as a public variable. That should make it accessible throughout the code.
我还认为您可以将 n 声明为公共变量。这应该使它在整个代码中都可以访问。
public int n;
公共国际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 =)
但是我想将它作为参数传递是更好的做法,因为您不会在代码中创建依赖项。我的意思是,如果变量发生变化,你就会破坏函数。在您的代码中始终保持“模块化”是一种很好的做法,这样可以使其对更改和调试更具弹性。如果你从一开始就习惯它会更好=)