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

VS2019(c#) 自动售货机

  1. 思路
    假设售货机只有一种商品,价格为8$;
    首先自动售货机会显示价格,余额,总消费,可购买食品数量,已购买的食品数量…等。
    其次是向自动售货机输入相关动作,充值,选择购买数量…等。
  2. 初始化
    新建售货机相关变量并声明赋值。
public int price = 8;//假设售货机只有一类商品
public double balance=0.0;
public int canTimes,buyTimes;
  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. 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. 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. 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. 完整代码
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. 测试
    c#实现一个自动售货机的基础动作功能

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

上一篇:CF1539E Game with Cards 题解


下一篇:hash table(开放寻址法-二次探查实现的哈希表)