使用用户输入的数字总和4位数字[英] Sum of number using user entered 4 digit numbers

本文是小编为大家收集整理的关于使用用户输入的数字总和4位数字的处理方法,想解了使用用户输入的数字总和4位数字的问题怎么解决?使用用户输入的数字总和4位数字问题的解决办法?使用用户输入的数字总和4位数字问题的解决方案?那么可以参考本文帮助大家快速定位并解决问题,译文如有不准确的地方,大家可以切到English参考源文内容。

问题描述

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

//在这一点9999我不是在要求答案,因为这是针对课堂的,但是某些指导会有所帮助

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

推荐答案

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

  1. 您可以使用正则表达式:4位数字,[1..9]范围中的第一个数字
  2. 您可以尝试parse 用户字符串进入int,如果解析成功,则在[1000..9999] range range

P.S.如您所要求的,我没有编码

本文地址:https://www.itbaoku.cn/post/2352619.html

问题描述

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

// At this point i'm confused as to what i should do to get a 4digit number where the number cannot equal zero, be a decimal or be a negative number, cannot be less than 1000 or greater than 9999 i'm not asking for answers because this is for class but some guidance would be helpful

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

推荐答案

There're two simple ways to solve the problem:

  1. You can use regular expressions: 4 digits, first one in [1..9] range
  2. You can try parse user string into int, and if parsing succeeds, check if result within [1000..9999] range

P.S. As you've asked, I've refrained from coding

查看更多