zoukankan      html  css  js  c++  java
  • 例题:把一个超市购物的题做成函数。这个题做成函数没有必要,但可以了解输入参数,输出参数,返回值,函数体,还有结构体,ArryList集合,for循环算结果,以及集合里 .count的使用

        class Program

        {

            struct shangpin                  //定义个结构体

            {

                public string mingcheng;

                public int jiage;

                public int shuliang;

            }

            public double  chaoshi(double zongjia)    //定义函数名

            {

                    ArrayList al = new ArrayList();   //定义一个集合

                                  while (true)

                    {

                        shangpin sp = new shangpin();  

                        Console.Write("请输入商品名称:");

                        sp.mingcheng = Console.ReadLine();  

                       Console.Write("请输入商品价格:");

                        sp.jiage = Convert.ToInt32(Console.ReadLine());

                        Console.Write("请输入商品数量:");

                        sp.shuliang = Convert.ToInt32(Console.ReadLine());

                        al.Add(sp);

                        Console.Write("请问是否要结账(Y,N):");

                        if (Console.ReadLine().ToUpper() == "Y")

                        {  

                           break;

                        } 

                      }

                 for (int i = 0; i <al.Count; i++)

                    {

                        zongjia += ((shangpin)al[i]).shuliang * ((shangpin)al[i]).jiage;

                    }

                    return zongjia;

                       }

           static void Main(string [] args)

            {

                           while (true)

               {  

                  Console.WriteLine("小小超市欢迎您!");

                   double zongjia1 = 0;

                   double  zongjia2=new Program().chaoshi(zongjia1);//定义一个变量zongjia2用来接收变量zongjia1调用chaoshi 这个函数计算出来的结果

                   Console.WriteLine("您要付出的总价格为" + zongjia1);              

                  Console.ReadLine();

               }         }     } }

  • 相关阅读:
    BestCoder Round #71 (div.2) (hdu 5621)
    BestCoder Round #71 (div.2) (hdu 5620 菲波那切数列变形)
    BestCoder Round #69 (div.2) Baby Ming and Weight lifting(hdu 5610)
    BestCoder Round #70 Jam's math problem(hdu 5615)
    BestCoder Round #68 (div.2) tree(hdu 5606)
    poj 2299 Ultra-QuickSort(归并排序或是bit 树+离散化皆可)
    hdu 3874 Necklace(bit树+事先对查询区间右端点排序)
    HDU 4300 Clairewd’s message(KMP)
    HDU 3308 LCIS(线段树单点更新区间合并)
    PKU 3667 Hotel(线段树)
  • 原文地址:https://www.cnblogs.com/275147378abc/p/4442832.html
Copyright © 2011-2022 走看看