我需要循环功能的特定部分

惠蒂1

这是对先前问题的跟进,得到了一些很好的反馈,我正在学习,寄存器的工作原理和计算结果很好。我想问问是否可以仅循环一个函数的一部分,我插入了一个if语句,语句检查该人给的钱是否少于总数,以及如果给的钱要更多。我想再次要求现金,并收到等于或大于总额的金额继续进行找零。这是代码:

using System.IO;
using System.Linq;
using System.Collections.Generic;

namespace CSharp_Shell
{

    public static class Program 
    {
        public static void Main() 
        {



           Console.WriteLine("                    *****--------------------*****");
           Console.WriteLine("                          Wit Groceries 2020");
           Console.WriteLine("                    *****--------------------*****");
           Console.WriteLine("            Welcome, would you like to do some shopping today?");
           Console.WriteLine("");

           var STOCK = new List<Items>
           {
            new Items{ id = 1, Name = "Bun", Price = 150},
            new Items{ id = 2, Name = "Soda", Price = 100},
            new Items{ id = 3, Name = "Cheese", Price =70},
            new Items{ id = 4, Name = "Tissue", Price = 50},
            new Items{ id = 5, Name = "Fabuloso", Price = 180},
            new Items{ id = 6, Name = "Grace Mackerel", Price = 70},
            new Items{ id = 7, Name = "Rice", Price = 50},
            new Items{ id = 8, Name = "Flour", Price = 40},
            new Items{ id = 9, Name = "Sugar", Price = 30},
           };



           var STOCKDict = STOCK.ToDictionary(i=>i.id);

           var selecteditems = new List<Items>();

           STOCK.ForEach(x => Console.WriteLine($"ID {x.id} - Name: {x.Name.PadRight(20)} Price: {string.Format("{0:C2}", x.Price)}"));
           Console.WriteLine();

           Console.WriteLine(" Enter the number for the item you want, enter 'x' when finihshed");


           do
           {
            var choice = Console.ReadLine();
            if (choice.Trim() =="x")
            {
                break;
            }
            int id;
            if (!int.TryParse(choice, out id))
            {
               Console.WriteLine("Enter number or 'x' ");
               continue;
            }
            if (STOCKDict.ContainsKey(id))
            {
                var item = STOCKDict[id];
                selecteditems.Add(item);
                Console.WriteLine(item.Name + " is added to your list");
            }
            else
            {
                Console.WriteLine(id + " is not available");
            }
           }
           while (true);

           decimal subt = selecteditems.Sum(i=>i.Price);
           decimal GCT = 16.50m;
           decimal GCTamt = (subt*GCT/100);
           decimal total = subt+GCTamt;
           Console.WriteLine("                  Sub-total = " + "$" + subt);
           Console.WriteLine("                  GCT = " + "$" + GCTamt);
           Console.WriteLine("                  You pay " + "$" +total);
           Console.WriteLine();



           Console.WriteLine("                  Please tender cash");
           decimal tend =decimal.Parse(Console.ReadLine());
            if (tend>total)
            {
           decimal change = tend - total;
           Console.WriteLine("         You gave " + " $"+tend + " Your change is " + "$"+change);
            }
            else
            {
                Console.WriteLine(" Please tender more cash");
            }



        }

    }
    public class Items
    {
        public int id {get; set;}
        public string Name{get; set;}
        public decimal Price {get;set;}
    }
}
亚历克斯·里奥

从您的需求看来,您只关心这部分代码:

Console.WriteLine("                  Please tender cash");
   decimal tend =decimal.Parse(Console.ReadLine());
    if (tend>total)
    {
   decimal change = tend - total;
   Console.WriteLine("         You gave " + " $"+tend + " Your change is " + "$"+change);
    }
    else
    {
        Console.WriteLine(" Please tender more cash");
    }

应用现金找零逻辑的地方。如果我正确理解,可以执行以下操作:

            decimal total = 15.75m;

            while (true)
            {
                Console.WriteLine("                  Please tender cash");
                decimal tend = decimal.Parse(Console.ReadLine());

                if (tend == total)
                {
                    Console.WriteLine("         You gave " + " $" + tend + "the exact amount.");
                    Console.ReadLine();
                    break;
                }

                if (tend > total)
                {
                    decimal change = tend - total;
                    Console.WriteLine("         You gave " + " $" + tend + " Your change is " + "$" + change);
                    Console.ReadLine();
                    break;
                }

                total -= tend;
                Console.WriteLine("         You gave " + " $" + tend);
                Console.WriteLine("         You still own " + " $" + total);
            }

在while循环的每次迭代中-我们请求现金-如果现金大于或等于其拥有的金额-循环完成并提供反馈。否则,我们从总数中减去给定的数量并重复。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何使特定部分功能运行并返回主循环?

来自分类Dev

只加载我需要的功能

来自分类Dev

我需要游戏循环吗?

来自分类Dev

我需要游戏循环吗?

来自分类Dev

我需要对 for 循环的解释

来自分类Dev

简化我的功能(循环,数组)?

来自分类Dev

我只需要打破特定页面上的循环(wordpress / woocommerce)

来自分类Dev

for循环在异步功能中提供部分输出

来自分类Dev

替换循环?此功能有效,但需要很长时间。我正在寻找改善它的方法

来自分类Dev

相同功能需要采取特定措施

来自分类Dev

特定成员功能的部分专业化

来自分类Dev

我需要更改DateTime变量的日期部分

来自分类Dev

在循环中剪切String的特定部分

来自分类Dev

我需要静态功能的互斥量吗?

来自分类Dev

需要改进我的javascript代码功能

来自分类Dev

我的功能需要同步吗?

来自分类Dev

需要我的pdo登录功能的帮助

来自分类Dev

更新我的Google Play“新功能”部分

来自分类Dev

更新我的Google Play“新功能”部分

来自分类Dev

我需要循环这个值吗?

来自分类Dev

我需要“ Foreach循环”任务的建议

来自分类Dev

我需要循环这个数组

来自分类Dev

我需要帮助理解这个“for”循环

来自分类Dev

我正在尝试使用do while循环来重复执行程序的特定部分,但它拒绝正确执行

来自分类Dev

Android布局问题在屏幕的特定部分,我有时需要显示一个按钮,有时需要显示一个TextView

来自分类Dev

我如何实现此功能的while循环

来自分类Dev

jQuery:需要制作动态功能以实施FAQ部分

来自分类Dev

我怎么知道我需要哪个 Modernizr 部分?

来自分类Dev

我需要这个特定的颜色,我有一个十六进制数,是否可以在这部分代码中添加它?

Related 相关文章

  1. 1

    如何使特定部分功能运行并返回主循环?

  2. 2

    只加载我需要的功能

  3. 3

    我需要游戏循环吗?

  4. 4

    我需要游戏循环吗?

  5. 5

    我需要对 for 循环的解释

  6. 6

    简化我的功能(循环,数组)?

  7. 7

    我只需要打破特定页面上的循环(wordpress / woocommerce)

  8. 8

    for循环在异步功能中提供部分输出

  9. 9

    替换循环?此功能有效,但需要很长时间。我正在寻找改善它的方法

  10. 10

    相同功能需要采取特定措施

  11. 11

    特定成员功能的部分专业化

  12. 12

    我需要更改DateTime变量的日期部分

  13. 13

    在循环中剪切String的特定部分

  14. 14

    我需要静态功能的互斥量吗?

  15. 15

    需要改进我的javascript代码功能

  16. 16

    我的功能需要同步吗?

  17. 17

    需要我的pdo登录功能的帮助

  18. 18

    更新我的Google Play“新功能”部分

  19. 19

    更新我的Google Play“新功能”部分

  20. 20

    我需要循环这个值吗?

  21. 21

    我需要“ Foreach循环”任务的建议

  22. 22

    我需要循环这个数组

  23. 23

    我需要帮助理解这个“for”循环

  24. 24

    我正在尝试使用do while循环来重复执行程序的特定部分,但它拒绝正确执行

  25. 25

    Android布局问题在屏幕的特定部分,我有时需要显示一个按钮,有时需要显示一个TextView

  26. 26

    我如何实现此功能的while循环

  27. 27

    jQuery:需要制作动态功能以实施FAQ部分

  28. 28

    我怎么知道我需要哪个 Modernizr 部分?

  29. 29

    我需要这个特定的颜色,我有一个十六进制数,是否可以在这部分代码中添加它?

热门标签

归档