Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JAVA无法从方法访问main中的数组_Java_Arrays_Methods - Fatal编程技术网

JAVA无法从方法访问main中的数组

JAVA无法从方法访问main中的数组,java,arrays,methods,Java,Arrays,Methods,我刚刚开始使用Java,我尝试使用我的一个方法从主类中读取数组,然后从文件中获取数组的值。该文件只是一个排名前5的分数列表,名称和数字之间用一行分隔。我正在做的方法需要检查给它的分数是否高于该位置已有的分数,并替换该值。唯一的问题是我无法从我的方法访问数组值 这是我的密码: public static void main(String[] args) { String [] nameArray = new String[5]; int [] scoreArray = new

我刚刚开始使用Java,我尝试使用我的一个方法从主类中读取数组,然后从文件中获取数组的值。该文件只是一个排名前5的分数列表,名称和数字之间用一行分隔。我正在做的方法需要检查给它的分数是否高于该位置已有的分数,并替换该值。唯一的问题是我无法从我的方法访问数组值

这是我的密码:

public static void main(String[] args) {

    String [] nameArray = new String[5];

    int [] scoreArray = new int[5];


    File score = new File("score.txt");

    if (score.exists())
    {
        try {
            nameArray[0] = Files.readAllLines(Paths.get("score.txt")).get(0);
            nameArray[1] = Files.readAllLines(Paths.get("score.txt")).get(2);
            nameArray[2] = Files.readAllLines(Paths.get("score.txt")).get(4);
            nameArray[3] = Files.readAllLines(Paths.get("score.txt")).get(6);
            nameArray[4] = Files.readAllLines(Paths.get("score.txt")).get(8);

            scoreArray[0] = Integer.parseInt(Files.readAllLines(Paths.get("score.txt")).get(1));
            scoreArray[1] = Integer.parseInt(Files.readAllLines(Paths.get("score.txt")).get(3));
            scoreArray[2] = Integer.parseInt(Files.readAllLines(Paths.get("score.txt")).get(5));
            scoreArray[3] = Integer.parseInt(Files.readAllLines(Paths.get("score.txt")).get(7));
            scoreArray[4] = Integer.parseInt(Files.readAllLines(Paths.get("score.txt")).get(9));
        } catch (IOException ex) {
            System.out.println("FILE IO EXCEPTION");
        }
    }




    if (!score.exists())
    {

        nameArray[0] = "---";
        nameArray[1] = "---";
        nameArray[2] = "---";
        nameArray[3] = "---";
        nameArray[4] = "---";

        scoreArray[0] = 0;
        scoreArray[1] = 0;
        scoreArray[2] = 0;
        scoreArray[3] = 0;
        scoreArray[4] = 0;

        BufferedWriter outputWriter;
        try {
            outputWriter = new BufferedWriter(new FileWriter("score.txt"));
                // I am aware I could have used a loop to do this
            outputWriter.write(nameArray[0]);
            outputWriter.newLine();
            outputWriter.write(Integer.toString(scoreArray[0]));
            outputWriter.newLine();

            outputWriter.write(nameArray[1]);
            outputWriter.newLine();
            outputWriter.write(Integer.toString(scoreArray[1]));
            outputWriter.newLine();

            outputWriter.write(nameArray[2]);
            outputWriter.newLine();
            outputWriter.write(Integer.toString(scoreArray[2]));
            outputWriter.newLine();

            outputWriter.write(nameArray[3]);
            outputWriter.newLine();
            outputWriter.write(Integer.toString(scoreArray[3]));
            outputWriter.newLine();

            outputWriter.write(nameArray[4]);
            outputWriter.newLine();
            outputWriter.write(Integer.toString(scoreArray[4]));
            outputWriter.newLine();

            outputWriter.flush();
            outputWriter.close();
        } catch (IOException ex) {
            System.out.println("FILE IO EXCEPTION");
        }
    }


}


static void playerScore(String name, int score)
{
  if (score > scoreArray[0])
  {
      scoreArray[0] = score;
  }
  //etc...
}

/* static String [] getTopNames()
{

}

 static int [] getTopScores()
{

}
*/

在类中而不是在主方法本身中定义数组。

在类中而不是在主方法本身中定义数组。

您可以将数组包含在访问数组所需的方法参数中,也可以在类的顶部定义数组,以便所有组件都可以访问它


还考虑添加一个for循环,因为您的代码是它应该的五倍,并且使用一个简单的循环,这将是很容易固定的。

您可以将它包含在访问数组所需的方法的参数中,或者将其定义在类的顶部,以便所有组件都可以访问它。

还考虑添加一个for循环,因为您的代码是它应该的五倍,并且使用一个简单的循环,这将是很容易固定的。

确实,在任何方法之外声明它作为类变量(但作为静态的,像这样)


实际上,在任何方法之外将其声明为类变量(但声明为静态,如下所示)


对于其他有类似问题的人,我在使用我的方法之前也使用了静态方法。在这种情况下,我们的方法可以是静态的。你应该阅读这篇文章来更好地理解static的作用:对于其他有类似问题的人,我也在我的方法之前使用了static。在这种情况下,我们的方法可以是静态的。您应该阅读本文,以更好地了解静态的作用:
public class Sorting {
    public static String [] nameArray = new String[5];

    public static void main(String[] args){
        Sorting a=new Sorting();
        a.readFile();
        nameArray=null;
    }