コード例 #1
0
            public static string GetTeens(int num)
            {
                string teens;

                switch (CodeSnippets.GetPlaceValue(num, 1))
                {
                default: teens = ""; break;

                case 1: teens = "eleven"; break;

                case 2: teens = "twelve"; break;

                case 3: teens = "thirteen"; break;

                case 4: teens = "fourteen"; break;

                case 5: teens = "fifteen"; break;

                case 6: teens = "sixteen"; break;

                case 7: teens = "seventeen";  break;

                case 8: teens = "eighteen"; break;

                case 9: teens = "nineteen"; break;
                }
                return(teens);
            }
コード例 #2
0
            public static string GetOnes(int num)
            {
                string ones;

                switch (CodeSnippets.GetPlaceValue(num, 1))
                {
                default: ones = "ERROR!!"; break;

                case 0: ones = ""; break;

                case 1: ones = "one"; break;

                case 2: ones = "two"; break;

                case 3: ones = "three"; break;

                case 4: ones = "four"; break;

                case 5: ones = "five"; break;

                case 6: ones = "six"; break;

                case 7: ones = "seven"; break;

                case 8: ones = "eight"; break;

                case 9: ones = "nine"; break;
                }
                return(ones);
            }
コード例 #3
0
            public static string GetHundreds(int num)
            {
                string hundreds;

                switch (CodeSnippets.GetPlaceValue(num, 100))
                {
                default: hundreds = ""; break;

                case 1: hundreds = "one hundred"; break;

                case 2: hundreds = "two hundred"; break;

                case 3: hundreds = "three hundred"; break;

                case 4: hundreds = "four hundred"; break;

                case 5: hundreds = "five hundred"; break;

                case 6: hundreds = "six hundred"; break;

                case 7: hundreds = "seven hundred"; break;

                case 8: hundreds = "eight hundred"; break;

                case 9: hundreds = "nine hundred"; break;
                }
                if (CodeSnippets.GetPlaceValue(num, 10) != 0 || CodeSnippets.GetPlaceValue(num, 1) != 0)
                {
                    hundreds += " and ";
                }
                return(hundreds);
            }
コード例 #4
0
            public static void RunTen()
            {
                Console.WriteLine("\n05.10. Write a program that applies bonus points to given scores in the range [1…9] by the following rules:");
                Console.WriteLine("- If the score is between 1 and 3, the program multiplies it by 10.");
                Console.WriteLine("- If the score is between 4 and 6, the program multiplies it by 100.");
                Console.WriteLine("- If the score is between 7 and 9, the program multiplies it by 1000.");
                Console.WriteLine("- If the score is 0 or more than 9, the program prints an error message.\n");

                int score    = CodeSnippets.GetInt();
                int orgScore = score;

                if (score < 1 || score > 9)
                {
                    Console.WriteLine("ERROR!!");
                }
                else if (score == 1 || score == 2 || score == 3)
                {
                    score *= 10;
                }
                else if (score == 4 || score == 5 || score == 6)
                {
                    score *= 100;
                }
                else if (score == 7 || score == 8 || score == 9)
                {
                    score *= 1000;
                }

                Console.WriteLine("Score of {0} plus bonus points is {1}", orgScore, score);

                Console.WriteLine("\nCOMPLETE\n");
            }
コード例 #5
0
            public static void RunEleven()
            {
                Console.WriteLine("\n05.11. * Write a program that converts a number in the range [0…999] to words, corresponding to the English pronunciation. Examples:");
                Console.WriteLine("- 0 --> \"Zero\"");
                Console.WriteLine("- 12 --> \"Twelve\"");
                Console.WriteLine("- 98 --> \"Ninety eight\"");
                Console.WriteLine("- 273 --> \"Two hundred seventy three\"");
                Console.WriteLine("- 400 --> \"Four hundred\"");
                Console.WriteLine("- 501 --> \"Five hundred and one\"");
                Console.WriteLine("- 711 --> \"Seven hundred and eleven\"\n");

                //could work this out better so that if hundreds place, ones are chosen followed by " hundred", rather than making each their own case. Would then be easier to add further numbers ie thousands.

                while (true)
                {
                    Console.WriteLine("Enter int bewteen 0 and 999");
                    int n = CodeSnippets.GetInt();
                    while (n < 0 || n > 999)
                    {
                        Console.WriteLine("Invalid int chosen");
                        n = CodeSnippets.GetInt();
                    }
                    string result = "";

                    StringBuilder sb = new StringBuilder();
                    switch (n)
                    {
                    default:
                        if (CodeSnippets.GetPlaceValue(n, 100) != 0)
                        {
                            sb.Append(GetHundreds(n));
                        }
                        if (CodeSnippets.GetPlaceValue(n, 10) != 0)
                        {
                            if (CodeSnippets.GetPlaceValue(n, 10) == 1)
                            {
                                sb.Append(GetTeens(n));
                            }
                            else
                            {
                                sb.Append(GetTens(n));
                            }
                        }
                        if (CodeSnippets.GetPlaceValue(n, 10) != 1)
                        {
                            sb.Append(GetOnes(n));
                        }
                        result = sb.ToString(); break;

                    case 0: result = "zero"; break;
                    }
                    Console.WriteLine(result.First().ToString().ToUpper() + result.Substring(1));

                    Console.WriteLine("\nCOMPLETE\n");
                }
            }
コード例 #6
0
            public static string GetTens(int num)
            {
                string tens;

                switch (CodeSnippets.GetPlaceValue(num, 10))
                {
                default: tens = ""; break;

                case 0: tens = " and "; break;

                case 1:
                    if (CodeSnippets.GetPlaceValue(num, 1) == 0)
                    {
                        tens = "ten";
                    }
                    else
                    {
                        tens = GetTeens(CodeSnippets.GetPlaceValue(num, 1));
                    }
                    tens = "ten"; break;

                case 2: tens = "twenty"; break;

                case 3: tens = "thirty"; break;

                case 4: tens = "forty"; break;

                case 5: tens = "fifty"; break;

                case 6: tens = "sixty"; break;

                case 7: tens = "seventy"; break;

                case 8: tens = "eighty"; break;

                case 9: tens = "ninty"; break;
                }
                if (CodeSnippets.GetPlaceValue(num, 10) != 1 || CodeSnippets.GetPlaceValue(num, 1) != 0)
                {
                    tens += " ";
                }
                return(tens);
            }
コード例 #7
0
            public static void RunEight()
            {
                Console.WriteLine("\n05.8. Write a program that, depending on the user’s choice, inputs int, double or string variable. If the");
                Console.WriteLine("variable is int or double, the program increases it by 1. If the variable is a string, the program appends \" * \" at the end.");
                Console.WriteLine("Print the result at the console. Use switch statement.\n");

                Console.WriteLine("Input an int (indicates what type the input is: 0 = int; 1 = double; 2 = string.");
                int    type   = CodeSnippets.GetInt();
                string result = "";

                switch (type)
                {
                default:
                    Console.WriteLine("{0} is not a valid option", type);
                    break;

                case 0:
                    type++;
                    result = type.ToString();
                    break;

                case 1:
                    type++;
                    result = type.ToString();
                    break;

                case 2:
                    result = type.ToString() + "*";
                    break;
                }

                Console.WriteLine(result);

                Console.WriteLine("\n...Not sure if this is what the question is asking for but...");

                Console.WriteLine("\nCOMPLETE\n");
            }