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; }
该方法拥有一个参数,即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; }
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; }
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(); }
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(); } } }
测试
提示
读下print易知其操作功能(我英语不好,看懂大概就行了QAQ)
代码还可以继续增添功能及优化,不好的地方恳请指正,
一起加油,让我们一起冲冲冲!!!