HashSet ——学生姓名&成绩

 Implement HashSet to store ‘n’ records of students ( Name, Percentage). Write a menu driven program to :

1. Add student

2. Display details of all students

3. Search student

4. Find out highest marks

 

  1 package Aug11.Collection;
  2 
  3 import java.util.*;
  4 
  5 public class Test4 {
  6     public static Set set = new HashSet();
  7 
  8     public static void main(String[] args) {
  9         Scanner reader = new Scanner(System.in);
 10 
 11         while (true) {
 12             System.out.println("---------------------");
 13             System.out.println("1. Add student");
 14             System.out.println("2. Display details of all students");
 15             System.out.println("3. Search student");
 16             System.out.println("4. Find out highest marks");
 17             System.out.println("5. Exit");
 18             System.out.println("---------------------");
 19             int n = reader.nextInt();
 20             switch (n) {
 21             case 1:
 22                 addStu();
 23                 break;
 24             case 2:
 25                 displayAllStu();
 26                 break;
 27             case 3:
 28                 searchStu();
 29                 break;
 30             case 4:
 31                 findHighestMarks();
 32                 break;
 33             case 5:
 34                 Exit();
 35                 break;
 36             }
 37         }
 38 
 39     }
 40 
 41     private static void Exit() {
 42         System.out.println("Exit Success!");
 43         System.exit(0);
 44 
 45     }
 46 
 47     private static void findHighestMarks() {
 48         Student stu;
 49         int max = 0;
 50         Iterator itr = set.iterator();
 51         while (itr.hasNext()) {
 52             stu = (Student) itr.next();
 53             if (stu.getPercentage() > max) {
 54                 max = stu.getPercentage();
 55             }
 56 
 57         }
 58         System.out.println("The  highest mark is " + max);
 59 
 60     }
 61 
 62     private static void searchStu() {
 63         Scanner reader = new Scanner(System.in);
 64         boolean flag = false;
 65         System.out.println("Enter the student's name:");
 66         String name = reader.nextLine();
 67         Iterator itr = set.iterator();
 68         while (itr.hasNext()) {
 69             Student stu = (Student) itr.next();
 70             if (stu.getName().equals(name)) {
 71                 System.out.println(stu.getName() + ":" + stu.getPercentage());
 72                 return;
 73             } else{
 74                 flag=false;
 75             }
 76         }
 77         if(!flag)
 78         System.out.println("There has no such person");
 79     }
 80 
 81     private static void displayAllStu() {
 82         System.out.println("The information of all students:");
 83         Iterator itr = set.iterator();
 84         while (itr.hasNext()) {
 85             Student stu = (Student) itr.next();
 86             System.out.println(stu.getName() + ":" + stu.getPercentage());
 87         }
 88     }
 89 
 90     private static void addStu() {
 91         Scanner reader = new Scanner(System.in);
 92         System.out.println("Enter student's name:");
 93         String name = reader.nextLine();
 94         System.out.println("Enter student's percentage:");
 95         int percentage = reader.nextInt();
 96         Student stu = new Student(name, percentage);
 97         set.add(stu);
 98     }
 99 
100 }
101 
102 // Student class
103 
104 class Student {
105     String name;
106     int percentage;
107     public Student(){
108         
109     }
110     public Student(String name, int percentage) {
111         this.name = name;
112         this.percentage = percentage;
113 
114     }
115 
116     public String getName() {
117         return name;
118     }
119 
120     public void setName(String name) {
121         this.name = name;
122     }
123 
124     public int getPercentage() {
125         return percentage;
126     }
127 
128     public void setPercentage(int percentage) {
129         this.percentage = percentage;
130     }
131 
132 }

 

 

转载于:https://www.cnblogs.com/thrive/p/3905395.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值