简体   繁体   English

使用循环 c# 求和整数

[英]Sum integers using a loop c#

I am new to programming and I think I have confused myself I'm trying to make a loop that asks users for integers when the user inputs a integer greater than 100 then the console displays the amount of integers the user has input and the sum of these integers.我是编程新手,我想我自己弄糊涂了我正在尝试创建一个循环,当用户输入大于 100 的整数时要求用户输入整数,然后控制台显示用户输入的整数数量和总和这些整数。 I know it's basic but I can't figure where I went wrong.我知道这是基本的,但我不知道我哪里出错了。

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

any help would be appreciated thanks everyone!任何帮助将不胜感激谢谢大家!

You're on the right track... a couple of things:你走在正确的轨道上......有几件事:

Do... While is used when you always want to run through the block at least once, so your first 'get' from the user can be inside the block. Do... While用于当您总是希望至少运行一次块时,因此您从用户那里获得的第一个“获取”可以在块内。 You can code whatever you want to happen after the condition fails right after the block, instead of checking the same condition inside it.您可以在块之后的条件失败后立即编写任何您想要发生的代码,而不是检查其中的相同条件。

Make sure if you're simply using Parse that you wrap it in a try...catch , because your user could type in anything (not just numbers).如果您只是使用Parse ,请确保将其包装在try...catch ,因为您的用户可以输入任何内容(不仅仅是数字)。 Personally I usually use TryParse instead.我个人通常使用TryParse代替。

Finally, make sure you're comparing to the correct variable.最后,确保您正在与正确的变量进行比较。 Checking that i < 100 will keep looping until 100 numbers have been entered;检查i < 100将继续循环直到输入 100 个数字; you want to compare the user's input instead.你想比较用户的输入。

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 

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

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM