当前位置:   article > 正文

c#实现一个自动售货机的基础动作功能_c#自动售货机代码

c#自动售货机代码

VS2019(c#) 自动售货机

  1. 思路
    假设售货机只有一种商品,价格为8$;
    首先自动售货机会显示价格,余额,总消费,可购买食品数量,已购买的食品数量…等。
    其次是向自动售货机输入相关动作,充值,选择购买数量…等。
  2. 初始化
    新建售货机相关变量并声明赋值。
public int price = 8;//假设售货机只有一类商品
public double balance=0.0;
public int canTimes,buyTimes;
  • 1
  • 2
  • 3
  1. 整理方法
    新建1)关于余额的相关展示及操作的方法inShow();
    新建2)展示可购买数量的方法times(double balance);
    新建3)关于购买数量的方法wantBuy();
    新建4)Main整合上述方法,实现完整自动售货机的功能。
  2. inShow()方法
    c#中要进行键盘输入的话,就要有Convert.ToDouble(Console.ReadLine());
    ToDouble可根据输入格式更改,还有:ToString,ToInt32…等。
    新建的double型变量t是为了对键入的balance进行替换后相加;,
    for循环是为了进行balance与price的判定动作,键入balance总额<price则一直进行的循环,以及为了后续退出循环操作的break和continue操作;
public double inShow()
        {
            double t = balance;
            Console.WriteLine("your balance is:{0}", balance);
            Console.WriteLine("plesea enter your money:");
            balance = Convert.ToDouble(Console.ReadLine());
            balance = t + balance;
            for (int i =0; ; i++)
            {
                double t1 = balance;
                if (balance < price)
                {
                    Console.Write("your balance was not enough,enter again:");
                    balance = Convert.ToDouble(Console.ReadLine());
                    balance = t1 + balance;
                    if (balance>price)
                    {
                        Console.WriteLine("balance is enough:{0}", balance);
                        break;
                    }
                    continue;                   
                }
                else
                {
                    Console.WriteLine("balance is enough:{0}", balance);
                    break;
                }
            }           
            return balance;
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  1. times(double balance)方法

该方法拥有一个参数,即inShow()方法返回的最终值balance,
向该方法传入balance的参数,进行计算得到能够购买食品数量的值,
因为balance是double型的,与int型不能进行计算,所以,对它进行强制转换(int)。

public int times(double balance)
        {
            int t = (int)balance;
            canTimes = t / price;
            Console.WriteLine("you can buy this food {0} times", canTimes);
            return canTimes;
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  1. wantBuy()方法

for循环同上,读下print易知其操作功能(我英语不好,看懂大概就行了QAQ)

public int wantBuy()
        {            
            for(int i =0; ; i++)
            {
                Console.WriteLine("please enter you want buyTimes:");
                buyTimes = Convert.ToInt32(Console.ReadLine());
                if (buyTimes <= canTimes)
                {
                    Console.WriteLine("you buy this food {0} times", buyTimes);
                    balance = balance - price * buyTimes;
                    Console.WriteLine("now you have {0} doller", balance);break;
                }
                else
                {
                    Console.WriteLine("your money is not enough,please enter following {0} times", canTimes+1);
                    break;
                }                
            }
            return buyTimes;
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  1. Main整合
    新建对象Class1 c1 = new Class1();
    先调用inShow()方法,然后就是再写一个switch循环询问是否要再次进行充值动作,
    其次就是调用times(double balance)方法以及wanBuy()方法。
    顺序完成一个自动售货机的基础功能。
static void Main(string[] args)
        {
            Class1 c1 = new Class1();
            c1.inShow();
            Console.WriteLine("would you want to enter one more time for recharge more money? enter 'yes' continue recharge,enter 'no' out");
            string word = Convert.ToString(Console.ReadLine());
            switch (word)
            {
                case "yes":
                    c1.inShow(); break;
                case "no":
                    break;
                default:
                    Console.WriteLine("enter error!");
                    break;
            }
            double a = c1.balance;
            c1.times(a);
            c1.wantBuy();
            Console.WriteLine("thank you for your patronage!");
            Console.WriteLine("you buy the food for {0}$", c1.price * c1.buyTimes);
            Console.ReadKey();
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  1. 完整代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

/**zohing原创**/

namespace Project1
{
    public class Class1
    {
        //自动售货机
        public int price = 8;//假设售货机只有一类商品
        public double balance=0.0;
        public int canTimes,buyTimes;
        public double inShow()
        {
            double t = balance;
            Console.WriteLine("your balance is:{0}", balance);
            Console.WriteLine("plesea enter your money:");
            balance = Convert.ToDouble(Console.ReadLine());
            balance = t + balance;
            for (int i =0; ; i++)
            {
                double t1 = balance;
                if (balance < price)
                {
                    Console.Write("your balance was not enough,enter again:");
                    balance = Convert.ToDouble(Console.ReadLine());
                    balance = t1 + balance;
                    if (balance>price)
                    {
                        Console.WriteLine("balance is enough:{0}", balance);
                        break;
                    }
                    continue;                   
                }
                else
                {
                    Console.WriteLine("balance is enough:{0}", balance);
                    break;
                }
            }           
            return balance;
        }
        public int times(double balance)
        {
            int t = (int)balance;
            canTimes = t / price;
            Console.WriteLine("you can buy this food {0} times", canTimes);
            return canTimes;
        }
        public int wantBuy()
        {            
            for(int i =0; ; i++)
            {
                Console.WriteLine("please enter you want buyTimes:");
                buyTimes = Convert.ToInt32(Console.ReadLine());
                if (buyTimes <= canTimes)
                {
                    Console.WriteLine("you buy this food {0} times", buyTimes);
                    balance = balance - price * buyTimes;
                    Console.WriteLine("now you have {0} doller", balance);break;
                }
                else
                {
                    Console.WriteLine("your money is not enough,please enter following {0} times", canTimes+1);
                    break;
                }                
            }
            return buyTimes;
        }
        static void Main(string[] args)
        {
            Class1 c1 = new Class1();
            c1.inShow();
            Console.WriteLine("would you want to enter one more time for recharge more money? enter 'yes' continue recharge,enter 'no' out");
            string word = Convert.ToString(Console.ReadLine());
            switch (word)
            {
                case "yes":
                    c1.inShow(); break;
                case "no":
                    break;
                default:
                    Console.WriteLine("enter error!");
                    break;
            }
            double a = c1.balance;
            c1.times(a);
            c1.wantBuy();
            Console.WriteLine("thank you for your patronage!");
            Console.WriteLine("you buy the food for {0}$", c1.price * c1.buyTimes);
            Console.ReadKey();
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  1. 测试
    在这里插入图片描述

  2. 提示
    读下print易知其操作功能(我英语不好,看懂大概就行了QAQ)
    代码还可以继续增添功能及优化,不好的地方恳请指正,
    一起加油,让我们一起冲冲冲!!!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/380284
推荐阅读
相关标签
  

闽ICP备14008679号