.Net-C# 第一学期-第二章 上机练习+课后练习(简答题)

上机练习




上机练习1-
实现MyBank系统取款功能

using System;

namespace demo8
{
    public class Show{

        //用户名
        public string name;
        //账户名
        public string id;
        //存款金额
        public double price;
        //账户密码
        public string pwd;
        //确认密码
        public string pwd2;
        //判断是否正确
        public Boolean isNam;

        //登陆账户名
        public string landName;
        //登陆密码
        public string landPwd;
        public void show1()
        {
            Console.WriteLine("1.注册账号");
            Console.WriteLine("2.取款");
            string num = Console.ReadLine();
            switch(num)
            {
                case "1":
                    show2();
                    break;
                case "2":
                    show4();
                    break;
            }
        }
        //注册
        public void show2()
        {
            while (true)
            {
            Console.WriteLine("请输入账号:");
            id = Console.ReadLine();
            Console.WriteLine("请输入用户名:");
            name = Console.ReadLine();
            Console.WriteLine("请输入密码:");
            pwd = Console.ReadLine();
            Console.WriteLine("请再次确认密码:");
            pwd2 = Console.ReadLine();
           
                show3();
                if (isNam==true)
                {
                    Console.WriteLine("请输入存款金额:");
                    price = double.Parse(Console.ReadLine());
                    Console.WriteLine("账户:{0} ,用户名:{1} ,存款金额:
                    {2} ,创建成功!", id, name, price);
                    Console.WriteLine("1.注册账号");
                    Console.WriteLine("2.取款");
                    string num1 = Console.ReadLine();
                    switch (num1)
                    {
                        case "1":
                            show2();
                            return;
                        case "2":
                            show4();
                            return;
                    }
                    break;
                }
                Console.WriteLine("1.注册账号");
                Console.WriteLine("2.取款");
                string num = Console.ReadLine();
                switch (num)
                {
                    case "1":
                        show2();
                        return;
                    case "2":
                        show4();
                        return;
                }
            }
        }
        //注册判断
        public void show3()
        {

            if (pwd.Equals(pwd2))
            {
                isNam = true;
            }
            else
            {
                Console.WriteLine("两次密码输入不一致");
                isNam = false;
                
            }
         }
	//登陆
        public void show4()
        {
            Console.WriteLine("请输入账号:");
            landName = Console.ReadLine();
            Console.WriteLine("请输入密码:");
            landPwd = Console.ReadLine();
            show5();
        }
	//登陆判断+取款
        public void show5()
        {
            if(landName.Equals(id))
            {
                if (landPwd.Equals(pwd))
                {
                    Console.WriteLine("请输入取款金额:");
                    double price2 = double.Parse(Console.ReadLine());
                    if (price>=price2)
                    {
                        Console.WriteLine("取款成功");
                        Console.WriteLine("当前余额:" + (price - 
                        price2));
                    }
                    else
                    {
                        Console.WriteLine("取款金额大于当前存款金
                        额!@");
                    }
                }
                else
                {
                    Console.WriteLine("密码输入错误");
                    Console.WriteLine("1.注册账号");
                    Console.WriteLine("2.取款");
                    string num = Console.ReadLine();
                    switch (num)
                    {
                        case "1":
                            show2();
                            return;
                        case "2":
                            show4();
                            return;
                    }
                }
                

            }
            else
            {
                Console.WriteLine("账号不存在");
                Console.WriteLine("1.注册账号");
                Console.WriteLine("2.取款");
                string num = Console.ReadLine();
                switch (num)
                {
                    case "1":
                        show2();
                        return;
                    case "2":
                        show4();
                        return;
                }
            }
        }
    }
class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            Console.WriteLine("");
            Show show = new Show();
            show.show1();
        }
    }
}

上级练习2-
输出MyBank系统菜单

using System;

namespace demo9
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            Console.WriteLine("欢迎使用自动银行服务\n");
            Console.Write("1.开户 ");
            Console.Write("2.存款 ");
            Console.Write("3.取款 ");
            Console.Write("4.转账 ");
            Console.Write("5.查询余额 ");
            Console.Write("6.修改密码 ");
            Console.Write("0.退出 \n");
            Console.WriteLine("\n请选择:");
            string num = Console.ReadLine();
            switch (num)
            {
                case "1":
                    break;
                case "2":
                    break;
                case "3":
                    break;
                case "4":
                    break;
                case "5":
                    break;
                case "6":
                    break;
                case "0":
                    break;
                default:
                    Console.WriteLine("请输入正确编号!@");
                    break;
            }
        }
    }
}

上机练习3-
初始化MyBank系统账户信息

省略

上机练习4-
实现MyBank系统

所有业务

using System;

namespace demo9
{
    class Program
    {
        public class Show
        {

            //添加账号位置
            public int index;
            public string accountsName;

            public string[] name=new string[10];
            public string[] pwd = new string[10];
            public double[] price = new double[10];
            public string[] user = new string[10];
            //开户账号
            public string name1;
            //开户密码
            public string pwd1;
            //开户确认密码
            public string pwd2;
            //开户名
            public string user2;
            //存款
            public string depositName;
            public string depositPwd;
            public double price2;
            //首页
            public void home()
            {
                while (true)
                {
                    Console.WriteLine("\n欢迎使用自动银行服务");
                    Console.Write("1.开户 ");
                    Console.Write("2.存款 ");
                    Console.Write("3.取款 ");
                    Console.Write("4.转账 ");
                    Console.Write("5.查询余额 ");
                    Console.Write("6.修改密码 ");
                    Console.Write("0.退出 \n");
                    Console.WriteLine("\n请选择:");
                    string num = Console.ReadLine();
                    switch (num)
                    {
                        case "1":
                            show1();
                            break;
                        case "2":
                            show2();
                            break;
                        case "3":
                            show3();
                            break;
                        case "4":
                            show4();
                            break;
                        case "5":
                            show5();
                            break;
                        case "6":
                            show6();
                            break;
                        case "0":
                            return;
                        default:
                            Console.WriteLine("请输入正确编号!@");
                            break;
                    }
                }
            }
            //开户
            public void show1()
            {
                Console.WriteLine("\nMyBank>开户");
                Console.WriteLine("\n请输入昵称:");
                user2 = Console.ReadLine();
                Console.WriteLine("请输入账号");
                name1 = Console.ReadLine();
                if (JudgeName())
                {
                    user[index] = user2;
                    name[index] = name1;
                    Console.WriteLine("请输入密码");
                    pwd1 = Console.ReadLine();
                    Console.WriteLine("请确认密码");
                    pwd2 = Console.ReadLine();
                    if(judgePwd())
                    {
                        Console.WriteLine("按任意键返回上一级");
                        string choice = Console.ReadLine();
                        if (choice.Equals("f"))
                        {
                            return;
                        }
                        else
                        {
                            return;
                        }
                    }
                    else
                    {
                        Console.WriteLine("两次密码不一致!@(自动返回上一级)\n");
                        return;
                    }
                    
                   
                }
                else
                {
                    Console.WriteLine("该账号已存在(自动返回上一级)\n");
                    return;
                }

                
                
            }

            //判断账号是否存在
            public Boolean JudgeName()
            {
                for (int i = 0; i <= index; i++)
                {
                    if (name[i]!=null && name[i].Equals(name1))
                    {
                        return false;
                    }
                }
                return true;

            }

            //判断两次密码是否一致,并添加
            public Boolean judgePwd()
            {
                if (pwd1.Equals(pwd2))
                {
                    for (int i = 0; i <= index; i++)
                    {
                        pwd[index] = pwd1;
                        Console.WriteLine("开户成功!@");
                        index++;
                        break;
                    }
                    return true;
                }

                return false;
            }

            //存款
           
            public void show2()
            {
                Console.WriteLine("\nMyBank>存款");
                Console.WriteLine("\n请输入账号:");
                depositName = Console.ReadLine();
                judgeDeposit();
                
            }

            //存款判断是否有该账号
            public void judgeDeposit()
            {
                int num=0;
                for (int i = 0; i < index; i++)
                {
                    num++;
                    if (name[i].Equals(depositName))
                    {
                        Console.WriteLine("请输入密码:");
                        depositPwd = Console.ReadLine();
                        for (int k = 0; k < index; k++)
                        {
                            if (depositPwd.Equals(pwd[i]))
                            {
                                for (int j = 0; j < index; j++)
                                {
                                        Console.WriteLine("欢迎回来:" + user[i]);
                                        Console.WriteLine("当前余额:" + price[i]);
                                        Console.WriteLine("\n存入金额:");
                                        price2 = double.Parse(Console.ReadLine());
                                        if (price2==0)
                                        {
                                            Console.WriteLine("禁止输入0");
                                            break;
                                        }
                                        else
                                        {
                                            price[i] += price2;
                                            Console.WriteLine("当前余额:" + price[i]);
                                            Console.WriteLine("自动返回主页@");
                                            break;
                                        }                               
                                }
                                break;
                            }
                            else
                            {
                                Console.WriteLine("密码输入错误");
                                break;
                            }
                        }
                        return;     
                    }
                }
                if(num==index)
                {
                    Console.WriteLine("无该账号记录!@(请重新输入)");
                    return;
                }
            }

            //取款
            public void show3()
            {
                Console.WriteLine("MyBank>取款");
                Console.WriteLine("\n请输入账号:");
                depositName = Console.ReadLine();
                qMoney();
            }

            //取款判断是否存在该账号
            public void qMoney()
            {
                int num = 0;
                for (int i = 0; i < index; i++)
                {
                    if (depositName.Equals(name[i]))
                    {
                        Console.WriteLine("请输入密码:");
                        depositPwd = Console.ReadLine();
                        for (int k = 0; k < index; k++)
                        {
                            if (depositPwd.Equals(pwd[i]))
                            {
                                for (int j = 0; j < index; j++)
                                {
                                    Console.WriteLine("欢迎回来:" + user[i]);
                                    
                                    if (price[i]==0)
                                    {
                                        Console.WriteLine("当前余额:" + price[i]);
                                        Console.WriteLine("按任意键返回@");
                                        string num2 = Console.ReadLine();
                                        if (num2.Equals("l"))
                                            
                                        {
                                            return;
                                        }
                                        else
                                        {
                                            return;
                                        }
                                    }
                                    else
                                    {
                                        while (true)
                                        {
                                            Console.WriteLine("当前余额:" + price[i]);
                                            Console.WriteLine("\n取款金额(或按000退出):");
                                            price2 = double.Parse(Console.ReadLine());
                                            if (price2 == 000)
                                            {
                                                
                                                Console.WriteLine("已返回");
                                                return;
                                            }
                                            else if (price2 == 0)
                                            {
                                                Console.WriteLine("禁止输入0");

                                            }
                                            else if (price2 > price[i])
                                            {
                                                Console.WriteLine("取款金额大于当前金额@");

                                            }
                                            else
                                            {
                                                price[i] = price[i] - price2;
                                                Console.WriteLine("当前余额:" + price[i]);
                                                Console.WriteLine("自动返回主页@");
                                                return;
                                            }
                                        }
                                    }
                                    
                                }
                            }
                        }
                    }
                }
                if (num == index)
                {
                    Console.WriteLine("无该账号记录!@(请重新输入)");
                    return;
                }
            }
            //转账
            public void show4()
            {
                Console.WriteLine("MyBank>转账");
                Console.WriteLine("\n请输入您账号");
                depositName = Console.ReadLine();
                accounts();
            }
			//转账判断
            public void accounts()
            {
                int num = 0;
                int num2 = 0;
                for (int i = 0; i < index; i++)
                {
                    num++;
                    if (depositName.Equals(name[i]))
                    {
                        Console.WriteLine("请输入密码:");
                        depositPwd = Console.ReadLine();
                        
                        if (price[i]==0)
                        {
                            Console.WriteLine("当前余额;" + price[i]);
                            Console.WriteLine("余额为“0”无法进行转账(自动返回)");
                            return;
                        }
                        else
                        {
                            if (depositPwd.Equals(pwd[i]))
                            {
                                Console.WriteLine("登陆成功!");
                                Console.WriteLine("当前余额;" + price[i]);
                                Console.WriteLine("请输入转账号码:");
                                accountsName = Console.ReadLine();
                                for (int j = 0; j < index; j++)
                                {
                                    num2++;
                                    if (accountsName.Equals(name[j]))
                                    {
                                        while (true)
                                        {
                                            Console.WriteLine("请输入转账金额 (按000返回主页):");
                                            price2 = double.Parse(Console.ReadLine());
                                            if (price2 > price[i])
                                            {
                                                Console.WriteLine("转账金额大于当前余额");
                                                Console.WriteLine("当前余额:" + price[i]);
                                            }
                                            else if (price2==000)
                                            {
                                                Console.WriteLine("已返回");
                                                return;

                                            }
                                            else if (price2 == 0)
                                            {
                                                Console.WriteLine("禁止输入0");
                                            }
                                            else
                                            {
                                                price[j] += price2;
                                                price[i] = price[i] - price2;
                                                Console.WriteLine("转账成功!@");
                                                Console.WriteLine("当前余额:" + price[i]);
                                                Console.WriteLine("自动返回上一级@");
                                                return;
                                            }
                                        }
                                    }
                                }
                                if (num2 == index)
                                {
                                    Console.WriteLine("没有该账号记录(已自动返回)");
                                    return;
                                }
                            }
                            else
                            {
                                Console.WriteLine("密码输入错误!@");
                                break;
                            }
                        }
                    }
                }
                if (num == index)
                {
                    Console.WriteLine("没有该账号(已自动返回)");
                    return;
                }
            }
            //查询余额
            public void show5()
            {
                Console.WriteLine("MyBank>查询余额");
                Console.WriteLine("请输入账号:");
                depositName = Console.ReadLine();
                query();
            }

            //查询判断
            public void query()
            {
                int num2 =0;
                for (int i = 0; i < index; i++)
                {
                    num2++;
                    if (depositName.Equals(name[i]))
                    {
                        for (int j = 3; j >= 0; j--)
                        {
                            Console.WriteLine("请输入密码:");
                            depositPwd = Console.ReadLine();
                            if (depositPwd.Equals(pwd[i]))
                            {
                                Console.WriteLine("当前余额:" + price[i]);
                                Console.WriteLine("按任意键返回:");
                                string num = Console.ReadLine();
                                if (num.Equals("f"))
                                {
                                    Console.WriteLine("已返回@");
                                    return;
                                }
                                else
                                {
                                    Console.WriteLine("已返回@");
                                    return;
                                }
                            }
                            else
                            {
                                Console.WriteLine("密码输入错误!还有"+j+"次机会");
                            }
                        }
                    }
                }
                if (num2==index)
                {
                    Console.WriteLine("没有该账号!@");
                    return;
                }
            }

            public string modifyPwd;
            //修改密码
            public void show6()
            {
                Console.WriteLine("MyBank>修改密码");
                Console.WriteLine("请输入账号:");
                depositName = Console.ReadLine();
                modify();
            }
            //修改密码判断
            public void modify()
            {
                for (int i = 0; i < index; i++)
                {
                    if (depositName.Equals(name[i]))
                    {
                        for (int j = 3; j >= 0; j--)
                        {
                            Console.WriteLine("请输入密码:");
                            depositPwd = Console.ReadLine();
                            if (depositPwd.Equals(pwd[i]))
                            {
                                for (; ; )
                                {
                                    Console.WriteLine("请输入修改密码:");
                                    modifyPwd = Console.ReadLine();
                                    if (modifyPwd.Equals(pwd[i]))
                                    {
                                        Console.WriteLine("当前密码与原来密码一致,请更换");
                                    }
                                    else
                                    {
                                        Console.WriteLine("修改成功!@");
                                        pwd[i] = modifyPwd;
                                        return;
                                    }
                                }
                               
                                
                            }
                            else
                            {
                                Console.WriteLine("密码输入错误!还有{0}次机会",j);
                            }
                        }
                    }

                    if (i==index)
                    {
                        Console.WriteLine("没有该账号记录");
                        return;
                    }
                }
               
            }
        }
        static void Main(string[] args)
        {
            Show show= new Show();
            show.home();
        }
    }
}


上机练习6-
排序

using System;

namespace demo10
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            int[] numArray = new int[6];
            for (int i = 0; i < numArray.Length; i++)
            {
                Console.WriteLine("请输入{0}个数:",i+1);
                int num = int.Parse(Console.ReadLine());
                numArray[i] = num;
            }
            Console.WriteLine("排序前:");
            foreach (var item in numArray)
            {
                Console.Write("{0} ", item);
            }
            int index=0;
            Console.WriteLine("\n排序后");
            for (int i = 0; i < numArray.Length-1; i++)
            {
                for (int j = 0; j < numArray.Length-1-i; j++)
                {
                    if (numArray[j]<numArray[j+1])
                    {
                        index = numArray[j + 1];
                        numArray[j + 1] = numArray[j];
                        numArray[j] = index;
                    }
                }
            }
            foreach (var item in numArray)
            {
                Console.Write("{0} ", item);
            }

        }
    }
}

在这里插入图片描述

简答题

1.学过那些循环

for循环,foreach循环,while循环,do-while循环,

2.输出数组中的数据

在这里插入图片描述
3.输出图案
在这里插入图片描述

4.-
平均成绩

 			//3科总分
            int sum = 0;
            //全班总分
            int sum2 = 0;
            //3科平均
            double age = 0;
            //全班平均
            double age2 = 0;
            //全班人数
            int num2 = 0;
            //最高分
            int highest2 = 0;
            for (int i = 0; i < 5; i++)
            {
                num2++;
                Console.WriteLine("\n学生姓名:");
                string name = Console.ReadLine();
                for (int j = 0; j < 3; j++)
                {
                    Console.WriteLine("第{0}门成绩是:",j+1);
                    int num = int.Parse(Console.ReadLine());
                    sum += num;
                    sum2 += sum;
                    
                }
                if (highest2<sum)
                {
                    highest2 = sum;
                }
                age = sum / 3;
                Console.WriteLine("三门总分:{0},平均分是:{1}", sum, 
                age);
                sum = 0;

            }
            age2 = sum2 / 5;         
            Console.WriteLine("班级人数:{0},最高分:{1},平均分:{2}",
             num2, highest2, age2);

5.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值