C# 如何重复输入的总数?

C# 如何重复输入的总数?,c#,C#,我对c很陌生,我不太快就明白了。如果你能解释为什么喜欢和一个3岁的孩子说话,那就太好了 你如何让输入金额-1停止重复并最终得到所有输入金额的总和?我们称之为循环,小男孩:p 更新我不知道我是否理解你,但是现在代码每次都写sum,如果你输入例如-5,它将是sum=sum-5 class Program { static void Main(string[] args) { // thoose are variables, and they are storing d

我对c很陌生,我不太快就明白了。如果你能解释为什么喜欢和一个3岁的孩子说话,那就太好了


你如何让输入金额-1停止重复并最终得到所有输入金额的总和?

我们称之为循环,小男孩:p 更新我不知道我是否理解你,但是现在代码每次都写sum,如果你输入例如-5,它将是sum=sum-5

class Program
{
    static void Main(string[] args)
    {
        // thoose are variables, and they are storing data
        int input = 0; // input integer number
        int sum = 0; // sum of all numbers

        while (true) //Infinite loop (executes undereneath code until true=true)
        {
            input = int.Parse(Console.ReadLine()); // read the line from user, parse to int, save to input variable
            if (input == -1) break; // if integer input is -1, it stops looping (the loop breaks) and GOES  (two lines down)
            sum = sum+ input; // summing all input (short version -> s+=input)
            Console.WriteLine("Actual Sum: "+sum);  // HERE IS THE UPDATE
        }
          //HERE
        Console.WriteLine("Your final sum is: " + s);
    }
}

幸运的是,我3岁的孩子就坐在这里,所以我让他写下:

var total = 0;  // This will hold the sum of all entries
var result = 0;  // This will hold the current entry

// This condition will loop until the user enters -1
while (result != -1)
{
    // Write the prompt out to the console window
    Console.Write("Enter the amount (-1 to stop): ");

    // Capture the user input (which is a string)
    var input = Console.ReadLine();

    // Try to parse the input into an integer (if TryParse succeeds, 
    // then 'result' will contain the integer they entered)
    if (int.TryParse(input, out result))
    {
        // If the user didn't enter -1, add the result to the total
        if (result != -1) total += result;
    }
    else
    {
        // If we get in here, then TryParse failed, so let the user know.
        Console.WriteLine("{0} is not a valid amount.", input);
    }
}

// If we get here, it means the user entered -1 and we exited the while loop
Console.WriteLine("The total of your entries is: {0}", total);

小男孩,离那台电脑远点!!听起来像是任务?如果你展示了你为解决这个问题所做的努力,你就更有可能得到反馈。你的问题的这个版本很难比这个版本更好。看到了,我对这一切都是陌生的,包括这个网站。我相信你也曾经是新人。我受够了每天花10个小时在这些废话上。这门课没有展示如何做到这一点,在网上找不到垃圾。我得到答案的唯一方法是在这里。•接受从用户处购买的每件物品的美元价值,直到用户完成。•购买完成后,输入sentinel值-1,表示用户已完成如果购买的物品价格为50.00美元或以上,则为客户提供购买物品的10%折扣。•如果客户获得该商品的折扣,则显示原始购买价格和折扣购买价格。否则,显示“未应用折扣”。•当客户输入-1表示用户已完成时,显示所有项目的总成本。如果我需要对金额进行折扣并使用新的总成本显示折扣,该怎么办