Advertisement
Guest User

Lab4

a guest
Nov 9th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.66 KB | None | 0 0
  1. using System;
  2.  
  3. namespace ASasitharan__Lab4
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.  
  10.             //*********************************************************************************************VARIABLES***************************************************************************************************************
  11.             const int MIN_GRADE = 0;
  12.             const int MAX_GRADE = 300;
  13.  
  14.             string tmpUserInput;
  15.             int scoreInput;
  16.             double [] scores = new double[6];
  17.             int count = 1;
  18.             bool isValid=false;
  19.             double sum = 0;
  20.             double highScore;
  21.             double lowestScore;
  22.             bool restart = true;
  23.             string yesNo;
  24.             bool validYesNo = false;
  25.             double average = 0.0;
  26.  //*********************************************************************************************VARIABLES***************************************************************************************************************
  27.  //loop while restart is true
  28.             while (restart)
  29.             {
  30.                 //reseet average variable
  31.                 average = 0.0;
  32.                 //reset sum variable
  33.                 sum = 0.0;
  34.                 //resets count to 1
  35.                 count = 1;
  36.  
  37.                 //for loop that loops through entire array
  38.                 for (int i = 0; i < scores.Length; i++)
  39.                 {
  40.  
  41.                     //reset isValid to false
  42.                     isValid = false;
  43.  
  44.                     //loop while isValid is false
  45.                     while (!isValid)
  46.                     {
  47.                         //prompt the user for score input and use count to list the game number
  48.                         Console.Write("Please enter the score for game " + count + ": ");
  49.  
  50.                         //tmp string user input for bowling scores
  51.                         tmpUserInput = Console.ReadLine();
  52.  
  53.                         //run if user input is numeric and parses user input
  54.                         if (Int32.TryParse(tmpUserInput, out scoreInput))
  55.                         {
  56.                             //runs if user input is within valid range
  57.                             if (scoreInput >= MIN_GRADE && scoreInput <= MAX_GRADE)
  58.                             {
  59.                                 //assigns score input to array
  60.                                 scores[i] = scoreInput;
  61.  
  62.                                 //sum variable adds up all of the data through each iteration
  63.                                 sum += scores[i];
  64.                                
  65.                                 //increment count up by 1
  66.                                 count++;
  67.  
  68.                                 //set isValid to true
  69.                                 isValid = true;
  70.                             }
  71.  
  72.                             //runs if user input is outside of valid range
  73.                             else
  74.                             {
  75.  
  76.                                 //display error message
  77.                                 Console.WriteLine("\nScores must be between 0 and 300. Please try again.\n");
  78.                             }
  79.                         }
  80.  
  81.                         //runs if user input is not numeric and an integer
  82.                         else
  83.                         {
  84.                             //display error message
  85.                             Console.WriteLine("\nScores must be numeric, whole numbers only, no decimals. Please try again.\n");
  86.                         }
  87.                     }
  88.  
  89.                 } //end of for loop
  90.                
  91.  
  92.  
  93.                 //clears the console
  94.                 Console.Clear();
  95.  
  96.                 //border
  97.                 Console.WriteLine("======================================================================================================================\n");
  98.  
  99.                 //list all of the scores
  100.                 Console.WriteLine("Game 1: " + scores[0] + "     Game 2: " + scores[1] + "     Game 3: " + scores[2] + "     Game 4: " + scores[3] + "     Game 5: " + scores[4] + "     Game 6: " + scores[5]);
  101.  
  102.                 //border
  103.                 Console.WriteLine("\n======================================================================================================================\n");
  104.  
  105.  
  106.                 //sets high score to be equal to first array value
  107.                 highScore = scores[0];
  108.  
  109.                 //loops through the array
  110.                 for (int j = 0; j < scores.Length; j++)
  111.                 {
  112.                     //runs if the current array value is greater than the current high score
  113.                     if (scores[j] > highScore)
  114.                     {
  115.  
  116.                         //changes the highest score to current array value
  117.                         highScore = scores[j];
  118.                     }
  119.                 }//end of for loop
  120.  
  121.  
  122.                 //sets lowest score to be equal to first array value
  123.                 lowestScore = scores[0];
  124.  
  125.                 //loops through the array
  126.                 for (int k = 0; k < scores.Length; k++)
  127.                 {
  128.                     //runs if the current array value is greater than the current lowest score
  129.                     if (scores[k] < lowestScore)
  130.                     {
  131.                         //changes the lowest score to current array value
  132.                         lowestScore = scores[k];
  133.                     }
  134.                 }
  135.  
  136.                 average = sum / scores.Length;
  137.                
  138.                 //displays average score for the bowler
  139.                 Console.WriteLine("Average Score for Bowler: " + Math.Round(average));
  140.              
  141.  
  142.                 //displays highest score
  143.                 Console.WriteLine("\nHigh Score for Bowler: " + highScore);
  144.  
  145.                 //displays lowest score
  146.                 Console.WriteLine("\nLow Score for Bowler: " + lowestScore);
  147.  
  148.                 //reset validYesNo value to false
  149.                 validYesNo = false;
  150.                
  151.                 //loop while validYesNo is false
  152.                 while (!validYesNo)
  153.                 {
  154.  
  155.                     //prompt user to enter y or n
  156.                     Console.WriteLine("\n\nWould you like to process another set of bowler scores? ");
  157.                     Console.Write("\nPlease enter Y to continue or N to exit: ");
  158.                    
  159.                     //user input for yes or no
  160.                     yesNo = Console.ReadLine();
  161.  
  162.                     //runs if user enter y
  163.                     if (yesNo == "Y" || yesNo == "y")
  164.                     {
  165.                        
  166.  
  167.                         //clears the console
  168.                         Console.Clear();
  169.                        
  170.                         //sets validYesNo to true to break out of loop
  171.                         validYesNo = true;
  172.                        
  173.                     }
  174.                    
  175.                     //runs if user enters n
  176.                     else if (yesNo == "N" || yesNo == "n")
  177.                     {
  178.                         //set restart  to false to break out of main loop
  179.                         restart = false;
  180.                        
  181.                         //sets validYesNo to true to break out of current loop
  182.                         validYesNo = true;
  183.                     }
  184.                    
  185.                     //runs if user enteres anything besides y or n
  186.                     else
  187.                     {
  188.                         //display error message
  189.                         Console.WriteLine("\n\nENTRY ERROR");
  190.                        
  191.                     }
  192.                 }
  193.  
  194.  
  195.  
  196.  
  197.             }
  198.  
  199.  
  200.         }
  201.     }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement