第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

第五章 學(xué)生選課系統(tǒng),關(guān)于map的使用

標(biāo)簽:
Java

创建了Pupil类用于存储学生信息。
然后在MapTest中对map类进行增、删、改、查。
基本实现功能,代码如下:

package com.Map.test;

public class Pupil {
    int Id;
    String Name;
    public Pupil(int id ,String name){
        Id = id;
        Name = name;
    }
    public Pupil(){

    }
}

MapTest类

package com.Map.test;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

public class MapTest {

    public Map<String,Pupil> Students;

    public MapTest(){
        this.Students = new HashMap<String,Pupil>();
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        MapTest mapt = new MapTest();
        mapt.AddPupil();
        mapt.showPupil();
        mapt.deletePupil();
        mapt.showPupil();
        mapt.modifyPupil();
        mapt.showPupil();
    }

/**
 *  AddPupil函数的作用,使用其添加学生信息,如学生的键,ID和值。
 */
    public void AddPupil(){
        int i=0;
        System.out.println("请输入学生的信息:");
        Scanner Console = new Scanner(System.in);
        while(i<3){
            System.out.println("请输入学生的键:");
            Pupil st = new Pupil();
            int add_Id;
            String add_Name;
            String add_Key;       //获取key
            add_Key = Console.next();
            //判断输入的ID是否已经存在;
            Pupil add_return = Students.get(add_Key);
            if(add_return==null){    //说明输入的Key不存在,可以加入
                System.out.println("输入学生的ID:");
                add_Id = Console.nextInt();
                System.out.println("输入学生的姓名:");
                add_Name = Console.next();
                st.Id = add_Id;
                st.Name = add_Name;
                Students.put(add_Key, st);
            }
            else{
                System.out.println("输入的键值已经存在,请重新输入:");
                continue;
            }
            i++;    
        }
    }

/*
 *  showPupil,通过foreach的方法遍历输出student元素
 */

    public void showPupil(){
        //获得Student中所以得key
        Set<String> StudentKeys = Students.keySet() ;
        System.out.println("学生的信息为:");
        for(String temp : StudentKeys){
            System.out.println("学生的键为:"+temp+",学生的ID为:"+Students.get(temp).Id+
                    ",学生的姓名是:"+Students.get(temp).Name);
        }

    }

    public void deletePupil(){
        String delete_Key;    //输入要删除学生的键
        Scanner conse = new Scanner(System.in);
        while(true){
          System.out.println("请输入要删除学生的键:");
          delete_Key = conse.next();
          Pupil st = new Pupil();
          st = Students.get(delete_Key);
          if(st!=null){
              Students.remove(delete_Key);
           System.out.println("要删除学生的ID是:"+st.Id+",姓名是:"+st.Name);
           break;          
          }
          else
              System.out.println("没有这个键,请重新输入。");

        }
    }
/*
 *  modifyPupil通过给定的键,调用put方向修改在该键处的值
 */

    public void modifyPupil(){
        String modify_key;
        Scanner sca = new Scanner(System.in);
        Pupil temp = new Pupil();

        int modify_Id;
        String modify_Name;
        while(true){
        System.out.println("输入要修改学生的key:"); 
        modify_key = sca.next();
        temp = Students.get(modify_key);    
        if(temp!=null){     //若不为空
            System.out.println("要修改的学生信息为:");
            System.out.println("学生的ID:"+temp.Id+",学生的姓名:"+temp.Name);
            System.out.println("请输入要修改的信息:");
            System.out.println("学生的ID:");
            modify_Id = sca.nextInt();
            System.out.println("学生的姓名:");
            modify_Name = sca.next();
            Pupil NewStudent = new Pupil(modify_Id,modify_Name);
            Students.put(modify_key, NewStudent);
            System.out.println("修改成功!");
            break;
        }
        else{
            System.out.println("没有这个key值");
            continue;
        }   
     }
    }
}
點(diǎn)擊查看更多內(nèi)容
2人點(diǎn)贊

若覺(jué)得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開(kāi)微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消