How to show the entered scores in order?

import java.util.Scanner;

public class Score {
    int num = 0;//students
    int score = 0;//score
    int[] arr = new int[num];
    
    public int student() {
        Scanner scan = new Scanner(System.in);      
        System.out.println("Number of students");
        num=scan.nextInt();
        int[] arr = new int[num];
        for(int i=0; i<num; i++)
        {
            arr[i]=scan.nextInt();
            score=arr[i];

            System.out.println(i+1+"students socre : "+score);
        }
        return score;
    }

    public void list() {
        int[] arr = new int[num];
        for(int i=0; i<num; i++)
        {
            //arr[i]=score;
            System.out.println(+score);
        }
    }
    
    public static void main(String[] args) {
        Score a =new Score();
        a.student();
        a.list();
    }
}

If I input 5 students with scores 10, 20, 30, 40, 50, so I want console to show

1 students socre : 10 2 students socre : 20 3 students socre : 30 4 students socre : 40 5 students socre : 50

and

10 20 30 40 50

but my code shows

1 students socre : 10 2 students socre : 20 3 students socre : 30 4 students socre : 40 5 students socre : 50

and

50 50 50 50 50

Answer

You are creating a local variable named arr in method student() and in method list(). A local variable only exists inside the method. This is referred to as variable scope. Refer to Variables which is part of Oracle’s java tutorials.

Simply replace the int[] arr with arr so that you are using the class member arr.

Here is your code with my corrections. I have put comments in the code to show which parts I changed.

import java.util.Scanner;

public class Score {
    int num = 0;// students
    int score = 0;// score
    int[] arr = new int[num];

    public int student() {
        Scanner scan = new Scanner(System.in);
        System.out.println("Number of students");
        num = scan.nextInt();
        arr = new int[num]; // CHANGE HERE
        for (int i = 0; i < num; i++) {
            arr[i] = scan.nextInt();
            score = arr[i];
            System.out.println(i + 1 + "students score : " + score); // CHANGE HERE
        }
        return score;
    }

    public void list() {
//        int[] arr = new int[num]; <- Remove this line.
        for (int i = 0; i < num; i++) {
            // arr[i]=score;
            System.out.println(arr[i]); // CHANGE HERE
        }
    }

    public static void main(String[] args) {
        Score a = new Score();
        a.student();
        a.list();
    }
}

Here is the output when I run the above code.

Number of students
5
10
1students score : 10
20
2students score : 20
30
3students score : 30
40
4students score : 40
50
5students score : 50
10
20
30
40
50