Nhập dữ liệu từ bàn phím nhưng nhập xong vẫn báo null và 0

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package GiuaMon;

import java.util.Scanner;

/**
 *
 * @author PLH
 */
public class Student {
    
    static String rollNo;
    static String name;
    static int age;
    static int score;
    static Scanner s = new Scanner(System.in);
    static int i=0;
    static void input(){
        
        for(i=0;i<5;i++){
            System.out.println("*************************");
            System.out.println("nhap thong tin hoc sinh:");
            System.out.print("RollNo: ");
            String rollNo = s.nextLine();
            System.out.print("Name: ");
            String name = s.nextLine();
            System.out.print("Age: ");
            int age = s.nextInt();
            System.out.print("score: ");
            int score = s.nextInt();s.nextLine();
        }
    }
    static void display(){
        System.out.printf("%-15s %-20s %-10s %-10s %n", "RollNo", "Name", "Age", "Score");
        for(i = 0; i < 5; i++){
            System.out.printf("%-15s %-20s %-10s %-10s %n", rollNo, name, age, score);
        }
    }
    public static void main(String[] args) {
        input();
        display();
    }
}
  1. Bạn đã khai báo các biến static bên ngoài rồi bên trong hàm chỉ cần gọi lại biến đó thôi không phải khai báo lại
  2. Sau khi sửa xong bạn in ra 5 lần , thực chất nó chỉ in ra lần cuối bạn nhập vào. Giải pháp là đã tạo class Student sao không có constuctror, get, set, toString() để in ra thông tin của mảng 5 sinh viên ? Đã đụng vô Java là nên tư duy thiết kế theo class, và học kỹ thêm về từ khóa static nhà bạn.

Code tham khảo: Ở đây mình dùng ArrayList để quản lý, bạn thay bằng mảng cũng được.

import java.util.ArrayList;
import java.util.Scanner;

public class Solution {
    private String rollNo;
    private String name;
    private int age;
    private int score;
    public Solution(String rollNo, String name, int age, int score){
        this.rollNo = rollNo;
        this.name = name;
        this.age = age;
        this.score = score;
    }
    @Override
    public String toString(){
        return String.format("%-15s %-20s %-10s %-10s %n\n", rollNo, name, age, score);
    }
    public void display(){
        System.out.println(this.toString());
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("Nhap vao so luong sinh vien: ");
        int n = in.nextInt();
        ArrayList<Solution> dssv = new ArrayList<>();
        in.nextLine();// trach troi dong
        for(int i = 0; i < n; i++){
            System.out.println("*************************");
            System.out.println("nhap thong tin hoc sinh thu :" + (i + 1));
            System.out.print("RollNo: ");
            String rollNo = in.nextLine();
            System.out.print("Name: ");
            String name = in.nextLine();
            System.out.print("Age: ");
            int age = in.nextInt();
            System.out.print("score: ");
            int score = in.nextInt();
            in.nextLine();
            Solution t = new Solution(rollNo, name, age, score);
            dssv.add(t);
        }
        
        System.out.println("DS thong tin cac sinh vien vua nhap: ");
        for(int i = 0; i < dssv.size(); i++){
            dssv.get(i).display();
        }
        
    }

}

mình cảm ơn bạn nhiều. cho mình thêm 1 tí là nếu đề bài yêu cầu mình viết method input() riêng biệt rồi khởi tạo 1 mảng 5 sinh viên trong hàm main và hiển thị ra thì phải làm thế nào

83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?