欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

学习JAVA入门介绍

程序员文章站 2022-03-22 10:59:10
学习JAVA.day01简单通讯录实现功能描述:1.查询通讯录2.添加新用户3.删除旧用户4.编辑旧用户5.退出通讯录通过对通讯录的编辑,练习JAVA基础语法及流程结构附上效果图:附源码:import java.util.Scanner;public class ContactList { static int perNum = 0; public static void main(String[] args) { boolean flagOnli...

学习JAVA.day01
简单通讯录实现
功能描述:
1.查询通讯录
2.添加新用户
3.删除旧用户
4.编辑旧用户
5.退出通讯录
通过对通讯录的编辑,练习JAVA基础语法及流程结构
附上效果图:
学习JAVA入门介绍
附源码:

import java.util.Scanner;

public class ContactList {
    static int perNum = 0;
    public static void main(String[] args) {
        boolean flagOnline = true;
        boolean isFirstInList = true;
        Person firstPer = null;
        while (flagOnline) {
            //1:显示通讯录选项  功能:查看通讯录、添加新用户、删除旧用户、编辑旧用户、退出通讯录
            showOpt();
            //2.初始化用户列表
            if (isFirstInList) {
                firstPer = initList();
                isFirstInList = false;
            }
            //3.等待输入
            boolean isCinOk = true;
            int index = 0;
            while (isCinOk) {
                index = new Scanner(System.in).nextInt();
                if (index > 5 || index < 1) {
                    System.out.println("请求错误!请输入1-5选项");
                } else {
                    isCinOk = false;
                }
            }
            switch (index) {
                case 1:
                    System.out.println("查看通讯录");
                    showList(firstPer);
                    break;
                case 2:
                    System.out.println("添加新用户");
                    addNewPer(firstPer);
                    break;
                case 3:
                    System.out.println("删除旧用户");
                    deletePer(firstPer);
                    break;
                case 4:
                    System.out.println("编辑旧用户");
                    remPer(firstPer);
                    break;
                case 5:
                    System.out.println("退出通讯录");
                    flagOnline = false;
                    break;
                default:
                    System.out.println("输入错误!");
                    break;
            }
            isCinOk = true;
        }
    }
    public static void showOpt() {
        System.out.println("========欢迎进入通讯录========");
        System.out.println("1.查看通讯录");
        System.out.println("2.添加新用户");
        System.out.println("3.删除旧用户");
        System.out.println("4.编辑旧用户");
        System.out.println("5.退出通讯录");
        System.out.println("============================");
    }
    //初始化通讯录
    public static Person initList() {
        Person per = new Person();
        per.num = perNum++;
        per.setName("移动客服");
        per.setAge(120);
        per.setPhoneNum("10086");
        per.pNext = null;
        return per;
    }
    //查看通讯录
    public static void showList(Person firstPer) {
        while (firstPer != null) {
            System.out.println(firstPer.num + "  " + firstPer.getName() + "  " + firstPer.getAge() + "  " + firstPer.getPhoneNum());
            firstPer = firstPer.pNext;
        }
    }
    //添加新用户
    public static void addNewPer(Person firstPer) {
        Person newPer = new Person();
        newPer.num = perNum++;
        System.out.println("请输入新用户姓名:");
        newPer.setName(new Scanner(System.in).nextLine());
        System.out.println("请输入新用户年龄:");
        newPer.setAge(new Scanner(System.in).nextInt());
        System.out.println("请输入新用户电话:");
        newPer.setPhoneNum(new Scanner(System.in).nextLine());
        newPer.pNext = null;
        addPer(firstPer, newPer);
    }
    public static void addPer(Person firstPer, Person newPer) {
        while (firstPer.pNext != null) {
            firstPer = firstPer.pNext;
        }
        firstPer.pNext = newPer;
    }
    //删除旧用户
    public static void deletePer(Person firstPer) {
        int delNum;
        Person delPer = null;
        boolean isFindPer = false;
        System.out.println("请输入待删除用户编号:");
        delNum = new Scanner(System.in).nextInt();
        if (delNum > 0) {
            while (firstPer.pNext != null) {
                if (firstPer.pNext.num == delNum) {
                    delPer = firstPer.pNext;
                    isFindPer = true;
                    firstPer.pNext = delPer.pNext;
                }
                if (isFindPer) {
                    firstPer = firstPer.pNext;
                    firstPer.num--;
                } else {
                    firstPer = firstPer.pNext;
                }
            }
            perNum--;
        }
    }
    //编辑旧用户
    public static void remPer(Person firstPer) {
        int remNum;
        Person remPer = null;
        System.out.println("请输入待编辑用户编号:");
        remNum = new Scanner(System.in).nextInt();
        if (remNum > 0) {
            while (firstPer != null) {
                if (firstPer.num == remNum) {
                    System.out.println("请输入新姓名:");
                    firstPer.setName(new Scanner(System.in).nextLine());
                    System.out.println("请输入新年龄:");
                    firstPer.setAge(new Scanner(System.in).nextInt());
                    System.out.println("请输入新电话:");
                    firstPer.setPhoneNum(new Scanner(System.in).nextLine());
                    break;
                }
                firstPer = firstPer.pNext;
            }
        }
    }
}

本文地址:https://blog.csdn.net/ChuqiYin/article/details/110119805

相关标签: java