【问题标题】:Sum integers using a loop c#使用循环 c# 对整数求和
【发布时间】:2015-05-14 18:50:49
【问题描述】:

我是编程新手,我想我自己搞糊涂了这些整数的总和。我知道这是基本的,但我不知道我哪里出错了。

namespace Wip
{
    class Program
    {
        static void Main(string[] args)
        {
            string strNum1, strNum2;
            int num1, num2;
            int i = 0;
            int sum =0 ;              

            Console.WriteLine("Please enter a integer between 1 and 100"); // asks for user input
            strNum1 = Console.ReadLine();
            num1 = int.Parse(strNum1);

            do //repeat asking for user input
            {
                Console.WriteLine("Please enter another integer between 1 and 100"); // asks for user input
                strNum2 = Console.ReadLine();
                num2 = int.Parse(strNum2); //input is stored as num2
                sum = num2; //store num2 in sum
                i++; 
                if (num2 >= 100) // if num2 int is greater than 100
                {
                    sum = (num1 +num2  +sum); // do calculation
                    Console.WriteLine("No of integers entered is {0} {1}", i, sum); //output calculation 
                }
            }
            while (i < 100);
        }
    }
}

任何帮助将不胜感激谢谢大家!

【问题讨论】:

  • 当输入的值 > 100 时,是否要将其包含在总和中,是否也应退出?如果它不应该退出,它应该继续现有的总和,还是重新开始?现在,您继续前进,直到用户输入 100 个数字。但总的来说,您的问题是您没有在进行过程中对数字求和,因此您在此过程中会损失一些。
  • 您可能指的是sum += num2;,而不是sum = num2;,因为后者只是将每次迭代的总和重置为当前数字2。
  • 您是否尝试过调试代码以查看值设置错误的位置,或者使用断点在相关行停止代码执行?下面的答案会告诉你你做错了什么,但如果你调试自己的代码并发现你的错误,你会更好。
  • 我对 += 不太熟悉,有人可以解释一下吗?对糟糕的问题表示歉意,但我是编程新手。你们真是太棒了,不给我发火!谢谢!
  • @CC1331 是addition assignment operator 基本上是x += y; 只是缺少x = x + y;

标签: c# loops integer sum


【解决方案1】:

你在正确的轨道上......有几件事:

Do... While 用于当您总是想至少运行一次块时,因此您从用户那里获得的第一个“获取”可以在块内。您可以编写任何您想要发生的代码after,条件在块之后立即失败,而不是检查其中的相同条件。

如果您只是使用Parse,请确保将其包装在try...catch 中,因为您的用户可以输入任何内容(不仅仅是数字)。就我个人而言,我通常使用TryParse

最后,确保您与正确的变量进行比较。检查i &lt; 100 是否会一直循环,直到输入了 100 个数字;你想比较用户的输入。

namespace Wip
{
    class Program
    {
        static void Main(string[] args)
        {
            string prompt = "Please enter {0} integer between 1 and 100";
            string strNum;
            int num = 0;
            int i = 0;
            int sum =0 ;              

            do //ask once and repeat while 'while' condition is true
            {
                string pluralPrompt = i > 0 ? "another" : "an";
                prompt = string.Format(prompt,pluralPrompt);
                Console.WriteLine(prompt); // asks for user input
                strNum = Console.ReadLine();
                if (!Int32.TryParse(strNum, out num)) //input is stored as num
                {
                    // warn the user, throw an exception, etc.
                }

                sum += num; //add num to sum
                i++; 

            }
            while (num < 100);

            Console.WriteLine("No of integers entered is {0} {1}", i, sum); //output calculation 

        }
    }
}

【讨论】:

    【解决方案2】:
    namespace Wip
    {
        class Program
        {
            static void Main(string[] args)
            {
                string strNum;
                int num;
                int i = 0;
                int sum = 0;
    
                do //repeat asking for user input
                {
                    Console.WriteLine("Please enter another integer between 1 and 100"); // asks for user input
                    strNum = Console.ReadLine();
                    if (int.TryParse(strNum, out num)) //input is stored as num2
                    {
                        if (num < 101) 
                        {
                            i++;
    
    
         sum += num;
                        continue;
                    }
                    else
                    {
                        Console.WriteLine("No of integers entered is {0} {1}", i, sum); //output calculation 
                            break;
                        }
                    }
                }
                while (i < 100);
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-02
      • 1970-01-01
      相关资源
      最近更新 更多