コード例 #1
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");
            }
コード例 #2
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");
                }
            }
コード例 #3
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");
            }