0

好的,所以我有这个简单但巨大的问题,我真的找不到解决方案。我已经在 main 方法之外声明了所有变量,然后在 main 方法中为它们赋值,但我似乎无法链接它们,所以当我在其他类中调用它们时,字符串 author1 和 2 为空,我的数组也是艺术家。我将非常感谢您的任何帮助,这让我发疯,我很着急。

package Medienbibliothek;

导入 java.util.Arrays;导入 java.util.Scanner;

公共类艺术家扩展作者{

 static String FirstName1;
 static String NickName1;
 static String LastName1;
 static String artist1;

 static String FirstName2;
 static String NickName2;
 static String LastName2;
 static String artist2;


public static String[] artists = new String[]{artist1, artist2};

public static void main(String args[]){



Scanner Person = new Scanner(System.in);

System.out.println("Enter FirstName: ");
FirstName1 = Person.nextLine();

System.out.println("Enter LastName: ");
LastName1 = Person.nextLine();

System.out.println("Enter NickName: ");
NickName1 = Person.nextLine();

String artist1 = (FirstName1 + " " + "\"" + NickName1 + "\"" + " " + LastName1);

System.out.println("Your first artist is: " + artist1);

System.out.println("Enter FirstName: ");
FirstName2 = Person.nextLine();

System.out.println("Enter LastName: ");
LastName2 = Person.nextLine();

System.out.println("Enter NickName: ");
NickName2 = Person.nextLine();

String artist2 = (FirstName2 + " " + "\"" + NickName2 + "\"" + " " + LastName2);

System.out.println("Your second artist is: " + artist2);



System.out.println("Your artists are: " + "\n1." + artist1 + "\n2." + artist2);

final String[] artists = new String[]{artist1, artist2};

System.out.println(Arrays.asList(artists));
4

1 回答 1

0

当您String artist1在函数内部编写时,它会声明新的局部变量,即 'shadows' static String artist1;。这意味着在下面的代码artist1中将引用局部变量而不是静态变量。删除这些不必要的声明。

于 2013-01-13T12:42:23.630 回答