使用循环c#

时间:2015-05-14 18:50:49

标签: c# loops integer sum

我是编程的新手,我想我已经弄糊涂了我正在尝试创建一个循环,当用户输入大于100的整数时,用户输入整数,然后控制台显示用户输入的整数数量这些整数的总和。我知道这是基本的,但我无法弄清楚我哪里出错了。

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);
        }
    }
}

感谢所有人,感谢任何帮助!

2 个答案:

答案 0 :(得分:0)

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

当你总是想要在块中运行至少一次时,使用

Do... While,因此用户的第一个'get'可以在块内。你可以编码你想要发生的任何事情条件在块之后立即失败,而不是在其中检查相同的条件。

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

最后,确保您与正确的变量进行比较。检查i < 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 

        }
    }
}

答案 1 :(得分:0)

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);

    }
}