简体   繁体   English

所有奇数的总和 C#

[英]Sum of All Odd numbers C#

I have an assignment to input random numbers from the keyboard that is different from 0 and random number k.我有一个任务是从键盘输入不同于 0 和随机数 k 的随机数。 I need to find the sum of the odd numbers + k(if k is also odd).我需要找到奇数的总和 + k(如果 k 也是奇数)。 Also when typing the numbers only when 0 is being typed the typing of numbers is interrupted.此外,仅在键入 0 时才键入数字时,数字的键入会中断。 This is what I've got so far!这是我到目前为止所得到的!

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

class Program
{
    static void Main()
    {
        int k;
        int min;
        int max;
        int odd = 0;

        Console.WriteLine("Enter the value of k: ");
        k = int.Parse(Console.ReadLine());
        Console.WriteLine("Enter minimum integer: ");
        min = int.Parse(Console.ReadLine());
        Console.WriteLine("Enter maximum integer: ");
        max = int.Parse(Console.ReadLine());

        Console.Write("Odd: ");
        for (int x = min; x <= max; x++)
        {
            if (x % 2 != 0)
            {
                Console.Write(x);
                Console.Write(" + ");
                odd += x;
            }
        }

        Console.WriteLine();
        Console.Write("Odd Numbers + K: ");
        Console.WriteLine();
        {
            if (k % 2 !=0)
            {
                Console.Write(k);
                Console.Write(" + ");
                odd += k;
            }
        }

        Console.Write("= ");
        Console.Write(odd + "\n");
    }
}

This code does what you need.此代码可以满足您的需求。 It checks the bounds min and max.它检查边界最小值和最大值。 It finishes when zero is entered and it also keeps the total sum of the odd numbers.它在输入零时结束,并且还保留奇数的总和。

Replace your static void Main() function with this one.用这个替换你的静态 void Main() 函数。

    static void Main()
    {
        //int k;
        int min;
        int max;
        int odd = 0;


        Console.WriteLine("Enter minimum integer: ");
        min = int.Parse(Console.ReadLine());
        Console.WriteLine("Enter maximum integer: ");
        max = int.Parse(Console.ReadLine());

        Console.WriteLine("Enter your number: ");
        bool userIsTyping = true;
        while (userIsTyping)
        {
            Console.WriteLine("Enter another number: ");            
            int userNumber = int.Parse(Console.ReadLine());

            if (userNumber == 0)
            {
                userIsTyping = false;
            }
            else if (userNumber > max)
            {
                Console.WriteLine("The number is out of bounds: greater than max.");
            }
            else if (userNumber < min)
            {
                Console.WriteLine("The number is out of bounds: less than min.");
            }
            else
            {

                if (userNumber % 2 != 0)
                {
                    odd += userNumber;
                    Console.WriteLine("Current Total: " + odd.ToString());
                }
                else
                {
                    Console.WriteLine("That is not an odd number.");
                }
            }

        }
        Console.WriteLine("The final result is: " + odd.ToString());
        Console.ReadLine();
    }

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

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