生活随笔
收集整理的這篇文章主要介紹了
键盘录入学生信息,保存到集合中
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
鍵盤錄入學(xué)生信息,保存到集合中。
- 循環(huán)錄入的方式,1:表示繼續(xù)錄入,0:表示結(jié)束錄入。
- 定義學(xué)生類,屬性為姓名,年齡,使用學(xué)生對象保存錄入數(shù)據(jù)。
- 使用數(shù)組(數(shù)組擴(kuò)容),保存學(xué)生對象,錄入結(jié)束后,遍歷數(shù)組。
public class Student {private String name
;private int age
;public Student() {}public Student(String name
, int age
) {this.name
= name
;this.age
= age
;}public String
getName() {return name
;}public void setName(String name
) {this.name
= name
;}public int getAge() {return age
;}public void setAge(int age
) {this.age
= age
;}@Overridepublic String
toString() {return "Student{" +"name='" + name
+ '\'' +", age=" + age
+'}';}
}
public class Demo4 {public static void main(String
[] args
) {Collection
<Student> coll
= new ArrayList<>();Scanner sc
= new Scanner(System
.in
);boolean b
= true;while (b
) {System
.out
.println("1錄入信息 0結(jié)束錄入。");int a
= sc
.nextInt();switch (a
) {case 1:System
.out
.println("請輸入姓名:");String s
= sc
.next();System
.out
.println("請輸入年齡:");int i
= sc
.nextInt();coll
.add(new Student(s
, i
));break;case 0:System
.out
.println("退出");b
= false;break;default:System
.out
.println("輸入有誤,請重新輸入");continue;}}for (Student stu
: coll
) {System
.out
.println(stu
);}}
總結(jié)
以上是生活随笔為你收集整理的键盘录入学生信息,保存到集合中的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。