输入学生序号和名字,使用ArrayList集合存储
输入学生个数和名字,使用ArrayList集合存储学生序号和名字,并显示所有的学生信息。
package hello; import java.util.ArrayList; import java.util.Scanner; public class Student { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("How many students?"); int a = in.nextInt(); if(a>0) { System.out.println("Please input the names of students:"); String n; in.nextLine(); ArrayList<Name> arrayList = new ArrayList<Name>(); for (int i = 0; i < a; i++) { n = in.nextLine(); Name s = new Name(i+1,n); arrayList.add(s); } System.out.println("All the students bellow:"); for (Name i : arrayList) { System.out.println(i); } }else { System.out.println("All the students bellow:"); } in.close(); } } class Name { public int xuhao; public String name; Name(int x, String n) { xuhao = x; name = n; } public String toString() { return xuhao+":"+name; } }
下一篇:
oracle 小项目实战总结