locked
How do i code this? in C#? RRS feed

  • Question

  • In this kind of program for C#


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

    namespace Exer1_Personal_Information
    {
        class Program
        {
            static void Main(string[] args)
            {

     

    how do i code if i want

    age = only accept 0 - 150

    gender = must accept only M / F

    middle initial = must only accept one letter

    status = must accept only singe / married / divorced / separated / widow

     

    Sunday, November 13, 2011 2:26 PM

Answers

  • public enum Gender
    {
        Male,
        Female,
    }
    
    public enum Status
    {
        Single,
        Married,
        Divorced,
        Seperated,
        Widow,
    }
    
    public class PersonalInformation
    {
        public bool SetAge(int age)
        {
            if (age < 0 || age > 150)
                return false;//Not assigned
    
            Age = age;
    
            return true;//assigned
        }
    
        public int Age
        {
            get;
            private set;
        }
    
        public Gender Gender
        {
            get;
            set;
        }
    
        public char MiddleInitial
        {
            get;
            set;
        }
    
        public Status Status
        {
            get;
            set;
        }
    }
    


    Any fool can know. The point is to understand.(Albert Einstein)
    • Marked as answer by Bob Shen Friday, November 25, 2011 3:26 AM
    Wednesday, November 16, 2011 7:58 PM
  • Hi,

    use the Console class (ReadLine-method etc), use conditional statements, switch statements and the Properties and Methods of System.String.

    Regards,

      Thorsten

    • Marked as answer by Bob Shen Friday, November 25, 2011 3:25 AM
    Sunday, November 13, 2011 2:32 PM

  • Your question could be interpreted in at least two ways.  The first way is to accept command line arguments for those values, such as:
     
        Exer1_Personal_Information age=25 gender=M middle_initial=Z status=widow
     
    The second way could be to accept params as requested, such as:
     
        Please enter age (0-150): 33
        Age is ok...
     
        Please enter gender (M/F): X
        Sorry, enter gender again...
        Please enter gender (M/F):
     
    etc...

    --
    Mike
    • Proposed as answer by JMCF125 Sunday, November 13, 2011 3:11 PM
    • Marked as answer by Bob Shen Friday, November 25, 2011 3:25 AM
    Sunday, November 13, 2011 2:50 PM
  • Hi Asian,

    Welcome to the MSDN forum.

    Here is a sample may help you:


                while (true)
                {
                    Console.WriteLine("Please input your age:");
                    int a = int.Parse(Console.ReadLine());
                    if (a>0&&a<150)
                    {
                        Console.WriteLine("good");
                        break;
                    }
                    else
                        Console.WriteLine("only accept 0 - 150");
                }
                while (true)
                {
                    Console.WriteLine("Please input your gender:M/F");
                    string st = Console.ReadLine();
                    if (st=="M"||st=="F")
                    {
                        Console.WriteLine("good");
                        break;
                    }
                    else
                        Console.WriteLine("only accept M or F");
                }
                while (true)
                {
                    Console.WriteLine("Please input your middle initial");
                    string st = Console.ReadLine();
                    if (st.Length==1)
                    {
                        Console.WriteLine("good");
                        break;
                    }
                    else
                        Console.WriteLine("only accept one letter");
                }
                isgo:Console.WriteLine("please choose your status");
                Console.WriteLine("1. singe  2. married  3. divorced  4. separated  5. widow");
                string s = Console.ReadLine();
                string sta;
                switch (s)
                {
                    case "1": sta = "single"; break;
                    case "2": sta = "married"; break;
                    case "3": sta = "divorced"; break;
                    case "4": sta = "separated"; break;
                    case "5": sta = "widow"; break;
                    default: Console.WriteLine("choose 1~5 please");
                        goto isgo;
                }
                Console.WriteLine("end");
                Console.Read();    

    Have a nice day.


    Bob Shen [MSFT]
    MSDN Community Support | Feedback to us
    • Marked as answer by Bob Shen Friday, November 25, 2011 3:25 AM
    Wednesday, November 16, 2011 3:55 AM
  • You can use System.ComponentModel.DataAnnotations to include validation of data. For example, if a field is required, you would preface the property with

    [Required(ErrorMessage = "Status is required")]

    If the user failed to include information on that field and you had validation of your form, the error message would appear where you indicated the ValidationMessage should appear.

    There are also annotations for StringLength, Range, RegularExpression, and a few others.

    Hope that helps!


    Christine A. Piffat
    • Marked as answer by Bob Shen Friday, November 25, 2011 3:26 AM
    Wednesday, November 16, 2011 8:52 PM

All replies

  • Hi,

    use the Console class (ReadLine-method etc), use conditional statements, switch statements and the Properties and Methods of System.String.

    Regards,

      Thorsten

    • Marked as answer by Bob Shen Friday, November 25, 2011 3:25 AM
    Sunday, November 13, 2011 2:32 PM

  • Your question could be interpreted in at least two ways.  The first way is to accept command line arguments for those values, such as:
     
        Exer1_Personal_Information age=25 gender=M middle_initial=Z status=widow
     
    The second way could be to accept params as requested, such as:
     
        Please enter age (0-150): 33
        Age is ok...
     
        Please enter gender (M/F): X
        Sorry, enter gender again...
        Please enter gender (M/F):
     
    etc...

    --
    Mike
    • Proposed as answer by JMCF125 Sunday, November 13, 2011 3:11 PM
    • Marked as answer by Bob Shen Friday, November 25, 2011 3:25 AM
    Sunday, November 13, 2011 2:50 PM
  • Hi Asian,

    Welcome to the MSDN forum.

    Here is a sample may help you:


                while (true)
                {
                    Console.WriteLine("Please input your age:");
                    int a = int.Parse(Console.ReadLine());
                    if (a>0&&a<150)
                    {
                        Console.WriteLine("good");
                        break;
                    }
                    else
                        Console.WriteLine("only accept 0 - 150");
                }
                while (true)
                {
                    Console.WriteLine("Please input your gender:M/F");
                    string st = Console.ReadLine();
                    if (st=="M"||st=="F")
                    {
                        Console.WriteLine("good");
                        break;
                    }
                    else
                        Console.WriteLine("only accept M or F");
                }
                while (true)
                {
                    Console.WriteLine("Please input your middle initial");
                    string st = Console.ReadLine();
                    if (st.Length==1)
                    {
                        Console.WriteLine("good");
                        break;
                    }
                    else
                        Console.WriteLine("only accept one letter");
                }
                isgo:Console.WriteLine("please choose your status");
                Console.WriteLine("1. singe  2. married  3. divorced  4. separated  5. widow");
                string s = Console.ReadLine();
                string sta;
                switch (s)
                {
                    case "1": sta = "single"; break;
                    case "2": sta = "married"; break;
                    case "3": sta = "divorced"; break;
                    case "4": sta = "separated"; break;
                    case "5": sta = "widow"; break;
                    default: Console.WriteLine("choose 1~5 please");
                        goto isgo;
                }
                Console.WriteLine("end");
                Console.Read();    

    Have a nice day.


    Bob Shen [MSFT]
    MSDN Community Support | Feedback to us
    • Marked as answer by Bob Shen Friday, November 25, 2011 3:25 AM
    Wednesday, November 16, 2011 3:55 AM
  • To accept one character of input just use ReadKey.  It's much easier than trying to use ReadLine and validating that there is only one character.
    Wednesday, November 16, 2011 5:28 PM
  • public enum Gender
    {
        Male,
        Female,
    }
    
    public enum Status
    {
        Single,
        Married,
        Divorced,
        Seperated,
        Widow,
    }
    
    public class PersonalInformation
    {
        public bool SetAge(int age)
        {
            if (age < 0 || age > 150)
                return false;//Not assigned
    
            Age = age;
    
            return true;//assigned
        }
    
        public int Age
        {
            get;
            private set;
        }
    
        public Gender Gender
        {
            get;
            set;
        }
    
        public char MiddleInitial
        {
            get;
            set;
        }
    
        public Status Status
        {
            get;
            set;
        }
    }
    


    Any fool can know. The point is to understand.(Albert Einstein)
    • Marked as answer by Bob Shen Friday, November 25, 2011 3:26 AM
    Wednesday, November 16, 2011 7:58 PM
  • You can use System.ComponentModel.DataAnnotations to include validation of data. For example, if a field is required, you would preface the property with

    [Required(ErrorMessage = "Status is required")]

    If the user failed to include information on that field and you had validation of your form, the error message would appear where you indicated the ValidationMessage should appear.

    There are also annotations for StringLength, Range, RegularExpression, and a few others.

    Hope that helps!


    Christine A. Piffat
    • Marked as answer by Bob Shen Friday, November 25, 2011 3:26 AM
    Wednesday, November 16, 2011 8:52 PM