如何将一种方法中的变量用于另一种方法?
How do I use variables in one method into another method?
所以我想知道是否有人可以告诉我如何 call/reference 将变量从一种方法转换为另一种方法。例如,
public static void main(String[] args)
{
System.out.println("Welcome to the game of sticks!");
playerNames();
coinToss();
}
public static void playerNames()
{
Scanner input = new Scanner(System.in);
System.out.println();
System.out.print("Enter player 1's name: ");
String p1 = input.nextLine();
System.out.print("Enter player 2's name: ");
String p2 = input.nextLine();
System.out.println();
System.out.println("Welcome, " + p1 + " and " + p2 + ".");
}
public static void coinToss()
{
System.out.println("A coin toss will decide who goes first:");
System.out.println();
Random rand = new Random();
int result = rand.nextInt(2);
result = rand.nextInt(2);
if(result == 0)
{
System.out.println(p1 + " goes first!");
}
else
{
System.out.println(p2 + " goes first!");
}
}
我想在 coinToss() 中使用 playerNames() 中的 p1 和 p2,这样我就可以简单地宣布谁先走,但我就是不知道如何调用变量。
我的问题与其他人相比并没有什么不同,但是我无法理解其他人给出的答案。一旦我发布了这个,我就从一群好心人那里得到了答案:)
我假设您是 Java 的新手,因为您似乎不熟悉 字段 的概念(即您可以将变量 外部 方法)。
public class YourClass {
static String p1;
static String p2;
public static void main(String[] args)
{
System.out.println("Welcome to the game of sticks!");
playerNames();
coinToss();
}
public static void playerNames()
{
Scanner input = new Scanner(System.in);
System.out.println();
System.out.print("Enter player 1's name: ");
p1 = input.nextLine();
System.out.print("Enter player 2's name: ");
p2 = input.nextLine();
System.out.println();
System.out.println("Welcome, " + p1 + " and " + p2 + ".");
}
public static void coinToss()
{
System.out.println("A coin toss will decide who goes first:");
System.out.println();
Random rand = new Random();
int result = rand.nextInt(2);
result = rand.nextInt(2);
if(result == 0)
{
System.out.println(p1 + " goes first!");
}
else
{
System.out.println(p2 + " goes first!");
}
}
}
您要搜索的是实例变量,请查看。
https://www.tutorialspoint.com/java/java_variable_types.htm
我所要做的就是在外部创建 instance/static 变量!像这样:
static String name1;
static String name2;
非常简单。感谢大家的帮助!
所以我想知道是否有人可以告诉我如何 call/reference 将变量从一种方法转换为另一种方法。例如,
public static void main(String[] args)
{
System.out.println("Welcome to the game of sticks!");
playerNames();
coinToss();
}
public static void playerNames()
{
Scanner input = new Scanner(System.in);
System.out.println();
System.out.print("Enter player 1's name: ");
String p1 = input.nextLine();
System.out.print("Enter player 2's name: ");
String p2 = input.nextLine();
System.out.println();
System.out.println("Welcome, " + p1 + " and " + p2 + ".");
}
public static void coinToss()
{
System.out.println("A coin toss will decide who goes first:");
System.out.println();
Random rand = new Random();
int result = rand.nextInt(2);
result = rand.nextInt(2);
if(result == 0)
{
System.out.println(p1 + " goes first!");
}
else
{
System.out.println(p2 + " goes first!");
}
}
我想在 coinToss() 中使用 playerNames() 中的 p1 和 p2,这样我就可以简单地宣布谁先走,但我就是不知道如何调用变量。
我的问题与其他人相比并没有什么不同,但是我无法理解其他人给出的答案。一旦我发布了这个,我就从一群好心人那里得到了答案:)
我假设您是 Java 的新手,因为您似乎不熟悉 字段 的概念(即您可以将变量 外部 方法)。
public class YourClass {
static String p1;
static String p2;
public static void main(String[] args)
{
System.out.println("Welcome to the game of sticks!");
playerNames();
coinToss();
}
public static void playerNames()
{
Scanner input = new Scanner(System.in);
System.out.println();
System.out.print("Enter player 1's name: ");
p1 = input.nextLine();
System.out.print("Enter player 2's name: ");
p2 = input.nextLine();
System.out.println();
System.out.println("Welcome, " + p1 + " and " + p2 + ".");
}
public static void coinToss()
{
System.out.println("A coin toss will decide who goes first:");
System.out.println();
Random rand = new Random();
int result = rand.nextInt(2);
result = rand.nextInt(2);
if(result == 0)
{
System.out.println(p1 + " goes first!");
}
else
{
System.out.println(p2 + " goes first!");
}
}
}
您要搜索的是实例变量,请查看。 https://www.tutorialspoint.com/java/java_variable_types.htm
我所要做的就是在外部创建 instance/static 变量!像这样:
static String name1;
static String name2;
非常简单。感谢大家的帮助!