开发者

way to store and use users input

开发者 https://www.devze.com 2023-01-04 17:12 出处:网络
I need to work with the input given by the user,how can i get the integer and char in开发者_运维百科put from user and how to store it and how to use it for manipulation in c#Here is a simple console p

I need to work with the input given by the user,how can i get the integer and char in开发者_运维百科put from user and how to store it and how to use it for manipulation in c#


Here is a simple console program that asks a user for his or her name, stores the input in a string variable, and then asks a question where the only valid response is an integer, and continues to prompt the user if an invalid input is provided. It then redisplays the user's answers on the screen.

using System;

namespace Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("What is your name?  ");
            string name = Console.ReadLine();

            bool validInput = false;
            int inputDays = 0;
            int actualDays = 7;

            while (!validInput)
            {
                Console.Write("How many days are in a week?  ");
                string response = Console.ReadLine();
                validInput = int.TryParse(response, out inputDays);
            }

            Console.WriteLine("{0}, you said there are {1} days in a week. This is {2}.", name, inputDays, inputDays == actualDays);
            Console.ReadKey();
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消