Java是一種強大的編程語言,可以很方便地輸出班級學號和姓名。
public class Student { String studentNumber; String name; public Student(String studentNumber, String name) { this.studentNumber = studentNumber; this.name = name; } public void printInfo() { System.out.println("學號:" + studentNumber); System.out.println("姓名:" + name); } } public class Main { public static void main(String[] args) { Student student1 = new Student("2019111111", "小明"); Student student2 = new Student("2019111112", "小紅"); student1.printInfo(); student2.printInfo(); } }
以上代碼定義了一個Student類,用來存儲學號和姓名,并提供了一個printInfo()方法,用來輸出學號和姓名。在Main類的main方法中創建了兩個Student對象,分別為student1和student2,并且分別調用了它們的printInfo()方法,輸出了學號和姓名。