多个输入总计 [英] Total multiple inputs

查看:76
本文介绍了多个输入总计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您如何汇总多个输入?我已经有一段时间没有使用C#了,需要一点帮助.另外,如何通过代码使程序不退出.

这是我的代码...如果有帮助的话:

How do you total multiple inputs? I haven''t used C# in a while and need a little help. Also, how do you get the program to not exit when the code is through.

Here is my code...if it will help:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SalesTax
{
    class Program
    {
        static void Main(string[] args)
        {
            string product;
            float amount;
            string add = "no";
            string answer = "yes";
            float price;
            float salesTax = 10 / 100F;
            float impTax = 5 / 100F;
            float total = 0F;
            int count = 0;
            Console.WriteLine("Would you like to add a product? Y/N");
            add = Console.ReadLine();
            while (add == "Y")
            {
                Console.WriteLine("\nIs this product books, food, or medical supplies? Y/N");
                answer = Console.ReadLine();
                if (answer == "Y")
                {
                    Console.WriteLine("\nIs the product imported? Y/N");
                    if (answer == "Y")
                    {
                        answer = Console.ReadLine();
                        Console.WriteLine("\nWhat is the product you want?");
                        product = Console.ReadLine();
                        Console.WriteLine("\nHow much do of the product do you want?");
                        amount = Single.Parse(Console.ReadLine());
                        Console.WriteLine("\nWhat is the price?");
                        price = Single.Parse(Console.ReadLine());
                        Console.WriteLine(price);
                        Console.WriteLine("\nHow much is product with sales tax?");
                        price = (amount * price * impTax) + price;
                        Console.WriteLine(price);
                        count += 1;
                        Console.WriteLine("\nWould you like to add a product? Y/N");
                        add = Console.ReadLine();
                    }
                    else
                    {
                        answer = Console.ReadLine();
                        Console.WriteLine("\nWhat is the product you want?");
                        product = Console.ReadLine();
                        Console.WriteLine("\nHow much do of the product do you want?");
                        amount = Single.Parse(Console.ReadLine());
                        Console.WriteLine("\nWhat is the price?");
                        price = Single.Parse(Console.ReadLine());
                        Console.WriteLine(price);
                        Console.WriteLine("\nHow much is product with sales tax?");
                        price = (amount * price * salesTax) + price;
                        price = price;
                        Console.WriteLine(price);
                        count += 1;
                        Console.WriteLine("\nWould you like to add a product? Y/N");
                        add = Console.ReadLine();
                    }
                }
                else
                {
                    Console.WriteLine("Is the product imported? Y/N");
                    if (answer == "Y")
                    {
                        answer = Console.ReadLine();
                        Console.WriteLine("\nWhat is the product you want?");
                        product = Console.ReadLine();
                        Console.WriteLine("\nHow much do of the product do you want?");
                        amount = Single.Parse(Console.ReadLine());
                        Console.WriteLine("\nWhat is the price?");
                        price = Single.Parse(Console.ReadLine());
                        Console.WriteLine(price);
                        Console.WriteLine("\nHow much is product with sales tax?");
                        price = (amount * price * salesTax * impTax) + price;
                        Console.WriteLine(price);
                        count += 1;
                        Console.WriteLine("\nWould you like to add a product? Y/N");
                        add = Console.ReadLine();
                    }
                    else
                    {
                        answer = Console.ReadLine();
                        Console.WriteLine("\nWhat is the product you want?");
                        product = Console.ReadLine();
                        Console.WriteLine("\nHow much do of the product do you want?");
                        amount = Single.Parse(Console.ReadLine());
                        Console.WriteLine("\nWhat is the price?");
                        price = Single.Parse(Console.ReadLine());
                        Console.WriteLine(price);
                        Console.WriteLine("\nHow much is product with sales tax?");
                        price = (amount * price * salesTax) + price;
                        Console.WriteLine(price);
                        count += 1;
                        Console.WriteLine("\nWould you like to add a product? Y/N");
                        add = Console.ReadLine();
                    }
                }
            }
            Console.WriteLine("Total:");

        }
    }
}

推荐答案

add变量不是"Y"且必须为大写"Y"时,程序将停止.如果键入"y",则程序将停止.

您可以更改
The program will stop when add variable is not "Y", and it has to be Upper case "Y". If you type "y" then the program will stop.

You can change
while (add == "Y")




To

while (add.ToUpper() == "Y")



关于总共多个输入.您可以创建一个新类,其中包含添加的所有产品的所有信息.



About total multiple inputs. You can make a new class that holds all information on all product added.

public class ProductInvoice
{
  public bool Imported;
  public string Product; // Name of product
  public float Price;
  public float PriceWithSalesTax;
  // .. etc
}



然后将所有信息保存在列表< productinvoice> [



And then hold all info in a List<productinvoice>[^] list and do call calculations there.


您是在学习编程,还是在为企业写东西?

我要求这样做的原因不是要侮辱或判断,而仅仅是因为更多地了解您在做什么以及为什么可以帮助他人决定如何最好地帮助您.例如,如果您正在学习,人们可能想给您一些小窍门,以介绍您到目前为止所做的事情,而不是为您编写解决方案.如果您是为企业而做,那么在为代码担心之前,设计还有很长的路要走,您将需要从与应用程序流程有关的一些问题开始(方法,而不是长的if/else块等). ).

话虽这么说,我只想指出几个引起我注意的快速事情.

首先,如果您想在某个时候回忆特定信息,则需要将其存储.如果您创建一个名为"price"的变量,并且经常将price设置为某个新值,那么您从中获得的唯一值就是它被设置为的最后一个值.要保留信息,请考虑将数组用于启动器.

其次,在某一点上您说price = price,希望此时您看到该行的问题. :)这样做没有好处,因为价格"已经等于价格".

因此,提供一些有关您具体想做什么,为什么做的详细信息,并尝试将其分解为有关如何做某些事情的具体问题,您可能会得到更多针对性的帮助.

祝你好运.
Are you learning to program, or actually writing something for a business?

The reason I ask is not to be insulting or judgemental, but only because knowing more about what you are doing and why could help others decide how best to help you. For example, if you are learning, people may want to give you small tips regarding pieces of what you have done so far, but not write a solution for you. If you are doing this for a business, there is a long way to go in the design before worry about the code, you''ll want to start with some questions regarding application flow (methods rather than long if/else blocks, etc.).

That being said, I will just point out a couple of quick things that caught my eye.

First, if you want to recall specific information at some point, you need to store it. If you create a variable called "price", and you are setting price to some new value every so often, the only value you will get out of it is the last one it was set to. To retain information, look into using arrays for starters.

Second, at one point, you say price = price, hopefully you see the problem with that line at this point. :) It does no good, as ''price'' is already equal to ''price''.

So, give some more details on what specifically you are wanting to do, why, and try and break it down into specific questions on how to do certain things, you may receive more pointed assistance.

Best of luck.


这篇关于多个输入总计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆