lmarkov

Bonus Scores

Dec 6th, 2012
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.00 KB | None | 0 0
  1. /*
  2.  * Write a program that applies bonus scores to given scores in the range [1..9]. The program reads a digit as an input. If the digit is between 1 and 3, the program multiplies it by 10; if it is between 4 and 6, multiplies it by 100; if it is between 7 and 9, multiplies it by 1000. If it is zero or if the value is not a digit, the program must report an error.
  3.         Use a switch statement and at the end print the calculated new value in the console.
  4. */
  5.  
  6. using System;
  7.  
  8. class BonusScores
  9. {
  10.     static void Main()
  11.     {
  12.         byte digit;
  13.         int result = 0;
  14.  
  15.         digit = InputData();
  16.  
  17.         switch (digit)
  18.         {
  19.             case 1:
  20.             case 2:
  21.             case 3:
  22.                 {
  23.                     result = digit * 10;
  24.                     break;
  25.                 }
  26.             case 4:
  27.             case 5:
  28.             case 6:
  29.                 {
  30.                     result = digit * 100;
  31.                     break;
  32.                 }
  33.             case 7:
  34.             case 8:
  35.             case 9:
  36.                 {
  37.                     result = digit * 1000;
  38.                     break;
  39.                 }
  40.             default:
  41.                 {
  42.                     Console.WriteLine("Error! Please enter a value between 1 and 9" + Environment.NewLine);
  43.                     break;
  44.                 }
  45.         }
  46.         if (result != 0)
  47.         {
  48.             OutputData(result);
  49.         }
  50.         Main();
  51.     }
  52.  
  53.     static byte InputData()
  54.     {
  55.         byte digit;
  56.         string invalidInput = "Please enter a value between 1 and 9" + Environment.NewLine;
  57.         Console.WriteLine("Enter a digit : ");
  58.         while (!(byte.TryParse(Console.ReadLine(), out digit) && digit >=0 && digit <= 9))
  59.         {
  60.             Console.WriteLine(invalidInput);
  61.             Console.WriteLine("Enter a digit: ");
  62.         }
  63.         return digit;
  64.     }
  65.  
  66.     static void OutputData(int result)
  67.     {
  68.         Console.WriteLine("Result: {0}" + Environment.NewLine, result);
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment