使用用户输入的4位数字的数字总和

时间:2013-09-11 05:54:23

标签: c# sum-of-digits

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

    namespace Assignment_1
    {
        class Program
        {
            static void Main(string[] args)
            {
                double SurfaceArea, Height, Radius, Volume;
                string inputString;
                int number;

                {
                    //Title Text
                    Console.WriteLine("Cylinder - Surface area and volume calculator");

                    //Prompts for input request

                    Console.Write("Enter the radius: ");
                    inputString = Console.ReadLine();

                    //displays user input for radius

                    Console.WriteLine("{0}", inputString);

                    Radius = double.Parse(inputString);
                    Console.Write("Enter Height: ");
                    inputString = Console.ReadLine();
                    Height = double.Parse(inputString);

                    //displays user input for height

                    Console.WriteLine("{0}", inputString);

                    //Calculation and display of surface area

                    SurfaceArea = (((2 * ((3.149)) * Radius) + (((2 * (3.149)) * (Radius) * Height))));
                    Console.WriteLine("Surface Area = {0}", SurfaceArea);

                    //Calculation and display of volume

                    Volume = ((3.149 * Radius * Radius * Height));
                    Console.WriteLine("The Volume = {0}", Volume);

                } 

                //Prompt the user for a four digit number (e.g. 5297) and display the sum of all of the digits
                //The four digit number must be read as a single number and the individual digits must be broken out using div (/) and mod (%).

    //Adds white space between lines
                Console.WriteLine("");

                Console.WriteLine("Sum of the digits - enter four digit whole numbers");
                Console.Write("please enter four digit whole number: ");

//此时我很困惑我该怎么做才能获得一个4位数,其中数字不能等于零,是小数或是负数,不能小于1000或大于9999我不是要求答案,因为这是为了上课,但有些指导会有所帮助

            {
                //Prompt the user for three numbers and display the largest number. Use Math.Max in your solution. 
                Console.ReadLine();
            }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

有两种简单的方法可以解决问题:

  1. 您可以使用正则表达式:4位数字,[1..9]范围内的第一位数字
  2. 您可以尝试将用户字符串解析为int,如果解析成功,检查结果是否在[1000..9999]范围内 < / LI>

    P.S。正如你所问,我没有编码