Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- **Lesson 6. Conditional Instruction IF - Continuation**
- **Lesson Objective:**
- The goal of the lesson is to reinforce information about the IF conditional instruction and understand the functionality of the Random class.
- **Agenda:**
- 1. Questions and warm-up task
- 2. Review tasks
- 3. Random class
- 4. Summary task
- **Questions from the previous lesson:**
- a) What is a conditional instruction and what is it used for?
- b) What instruction did you learn in the last lesson?
- c) What is the keyword `else` used for?
- **Warm-up Task:**
- Write a console application that takes from the user the symbol or atomic number of an element and prints the element's name in the console. Include examples for 7 selected elements. The program should also display an appropriate message if the element is not found.
- ```csharp
- Console.WriteLine("Enter the symbol or atomic number of the element:");
- string elementData = Console.ReadLine();
- if (elementData == "H" || elementData == "1")
- {
- Console.WriteLine("The element is -> Hydrogen");
- }
- else if (elementData == "Co" || elementData == "27")
- {
- Console.WriteLine("The element is -> Cobalt");
- }
- else if (elementData == "K" || elementData == "19")
- {
- Console.WriteLine("The element is -> Potassium");
- }
- else if (elementData == "Ag" || elementData == "47")
- {
- Console.WriteLine("The element is -> Silver");
- }
- else if (elementData == "Au" || elementData == "79")
- {
- Console.WriteLine("The element is -> Gold");
- }
- else if (elementData == "Mg" || elementData == "12")
- {
- Console.WriteLine("The element is -> Magnesium");
- }
- else if (elementData == "He" || elementData == "2")
- {
- Console.WriteLine("The element is -> Helium");
- }
- else
- {
- Console.WriteLine("No such element found");
- }
- Console.ReadLine();
- ```
- Mention to participants that if only one instruction needs to be executed for a fulfilled condition, braces can be omitted, using a one-line if statement.
- ```csharp
- if (elementData == "H" || elementData == "1")
- Console.WriteLine("The element is -> Hydrogen");
- ```
- **Task 1. Online Store (nested conditional instructions)**
- Write a console application that simulates a simple online store with the option to choose 2-3 items or games. The program should take into account the final price of the order and whether the user has a discount code that gives a 15% discount.
- ```csharp
- static void Main(string[] args)
- {
- float keyboardPrice = 100f;
- float mousePrice = 80f;
- string promoCode = "Gigant15%";
- Console.WriteLine("Welcome! \nChoose the number of the item you are interested in: \n" +
- $"1 - Backlit Keyboard - {keyboardPrice:C}\n" +
- $"2 - Gaming Mouse - {mousePrice:C}");
- int choice = int.Parse(Console.ReadLine());
- if (choice == 1)
- {
- Console.WriteLine("Excellent choice! Enter the promo code: ");
- string userPromoCode = Console.ReadLine();
- if (userPromoCode == promoCode)
- {
- Console.WriteLine($"Amount to pay {keyboardPrice * 0.85f}");
- }
- else
- {
- Console.WriteLine($"Amount to pay {keyboardPrice}");
- }
- }
- else if (choice == 2)
- {
- // similar to 1
- }
- else
- {
- Console.WriteLine("Unfortunately, it seems we don't have what you're looking for.");
- }
- Console.ReadKey();
- }
- ```
- **Task 2. Game – Quiz Giants**
- Create a simple console application that simulates a quiz with multiple-choice questions and four answers for each. Create the initial part of the code together with students. Add 2-3 sample questions and implement the basic part of the code, then give time to add 2-3 own questions. Show the possibilities of the `toUpper()` and `toLower()` methods to change the characters entered by the user to uppercase or lowercase.
- ```csharp
- static void Main(string[] args)
- {
- Console.WriteLine("Welcome to the Giants Quiz");
- int correctAnswers = 0;
- bool continuePlaying = true;
- char answer;
- string question1 = "First question: Which of the following is not a programming language?";
- string ans1A = "Java";
- string ans1B = "C#";
- string ans1C = "HTML";
- string ans1D = "Python";
- string question2 = "Second question: Which of the following is not an operating system?";
- string ans2A = "Windows";
- string ans2B = "Linux";
- string ans2C = "Android";
- string ans2D = "Microsoft";
- Console.WriteLine(question1);
- Console.WriteLine($" A: {ans1A}\n B: {ans1B}\n C: {ans1C}\n D: {ans1D}");
- Console.WriteLine("Choose your answer: ");
- answer = char.Parse(Console.ReadLine());
- if (char.ToUpper(answer) == 'C')
- {
- correctAnswers++;
- Console.WriteLine("Well done, let's continue! Press any key");
- Console.ReadKey();
- Console.Clear(); // clear console screen
- }
- else
- {
- Console.WriteLine("Sorry, the correct answer is C. Press any key");
- continuePlaying = false;
- Console.ReadKey();
- Console.Clear();
- }
- if (continuePlaying)
- {
- Console.WriteLine(question2);
- Console.WriteLine($" A: {ans2A}\n B: {ans2B}\n C: {ans2C}\n D: {ans2D}");
- Console.WriteLine("Choose your answer: ");
- answer = char.Parse(Console.ReadLine());
- if (char.ToUpper(answer) == 'D')
- {
- correctAnswers++;
- Console.WriteLine("Well done, let's continue! Press any key");
- Console.ReadKey();
- Console.Clear();
- }
- else
- {
- Console.WriteLine("Sorry, the correct answer is D. Press any key");
- continuePlaying = false;
- Console.ReadKey();
- Console.Clear();
- }
- }
- Console.WriteLine($"You gave {correctAnswers} correct answers");
- Console.ReadLine();
- }
- ```
- **Random Class**
- In programming, we can use additional functionalities that are hidden in special objects of various classes. So far, we have learned the classes Console, String, and Math. Another useful class is Random, which is a generator of randomness. It allows for generating pseudo-random numbers within a specified range.
- Why "Pseudo"? These values are never truly random because they are generated based on a specific seed given when initializing the Random object, i.e., `new Random(12345)`, where 12345 is the seed. If we do not provide the seed value in the constructor, it will default to the current system time. If we always generated numbers based on the same seed, we would get the same sequence of numbers every time.
- To generate a value, we first need to create a Random class object, and then create a numeric variable to which we will assign the result of the `maszynaLosujaca.Next()` method. This method can take the range as parameters, e.g., for 1 - 100, we call `generator.Next(1, 101);` Note that numbers are generated up to but not including the given range, so we will never get the value given as the second parameter. If we want to draw values up to 100, we must specify 101. In other words, the minimum value is included in the draw range, the maximum value is not.
- ```csharp
- Random maszynaLosujaca = new Random();
- int drawnValue = maszynaLosujaca.Next(1, 101);
- ```
- **Task 3. Guessing Game**
- Create a console application that draws a number from the range of 1 to 20, and the user has to guess it in three attempts. After each answer, a hint should be displayed whether the entered number is less than or greater than the sought value. The program should inform how many attempts it took to guess the number.
- ```csharp
- // Create random number generator and draw a value
- Random randomMachine = new Random();
- int numberToGuess = randomMachine.Next(1, 21);
- int currentAttempt = 0;
- int enteredNumber;
- // First attempt
- Console.WriteLine("Enter a number (1-20): ");
- enteredNumber = int.Parse(Console.ReadLine());
- currentAttempt++;
- if (enteredNumber > numberToGuess)
- {
- Console.WriteLine("Not quite, my number is smaller than the entered one");
- }
- else if (enteredNumber < numberToGuess)
- {
- Console.WriteLine("Not quite, my number is greater than the entered one");
- }
- else
- {
- Console.WriteLine($"Well done, you guessed the drawn number in {currentAttempt} attempt(s)!");
- Console.ReadLine();
- return;
- }
- // Second attempt
- Console.WriteLine("Enter a number (1-20): ");
- enteredNumber = int.Parse(Console.ReadLine());
- currentAttempt++;
- if (enteredNumber > numberToGuess)
- {
- Console.WriteLine("Not quite, my number is smaller than the entered one");
- }
- else if (enteredNumber < numberToGuess)
- {
- Console.WriteLine("Not quite, my number is greater than the entered one");
- }
- else
- {
- Console.WriteLine($"Well done, you guessed the drawn number in {currentAttempt} attempt(s)!");
- Console.ReadLine();
- return;
- }
- // Third attempt
- Console.WriteLine("Enter a number (1-20): ");
- enteredNumber = int.Parse(Console.ReadLine());
- currentAttempt++;
- if (enteredNumber > numberToGuess)
- {
- Console.WriteLine("Not quite, my number is smaller than the entered one");
- Console.WriteLine($"Unfortunately, you didn't guess the number. The correct number is: {numberToGuess}");
- }
- else if (enteredNumber < numberToGuess)
- {
- Console.WriteLine("Not quite, my number is greater than the entered one");
- Console.WriteLine($"Unfortunately, you didn't guess the number. The correct number is: {numberToGuess}");
- }
- else
- {
- Console.WriteLine($"Well done, you guessed the drawn number in {currentAttempt} attempt(s)!");
- Console.ReadLine();
- return;
- }
- Console.ReadLine();
- ```
- **Final Questions:**
- - What is nested conditional instruction?
- - What do we use the `Console.Clear()` function for?
- - What does the `char.ToUpper()` function do?
- - How does random number generation work?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement