A really common mistake in while loops is infinite loops, so it’s important to remember to increment i below console.WriteLine so that it doesn’t continue displaying 1 forever.
i += 1; is another way of writing i++;
Problem Solving With While Loops
Here I’ve coded a While Loop for the number of attempts it takes to roll a six on a dice given that there is a one in six chance.
I defaulted the numberOfAttempts to 0 as no attempts had been made yet.
The int attempt stores the single attempt and as long as the number does not equal six, != 6 the code will loop the code setting the attempt to a new random value.
To generate random I referenced the random class, similarly to in a previous post.
I’ve set the parameters of attempt = numberGen.Next(1, 7); because the dice roll limits the numbers 1 to 6. Again 1 being inclusive and 7 being exclusive. This could also be written as (1, 6 + 1);
I also experimented with a do while loop, which required you to be sure the code would perform at least one iteration.
This is a way to iterate a way to do something a number of times.
defining int variable to i and setting it equal to 1, in this case 1 is our starting number.
As long as i is <=10 we want to execute the line below.
i++ is to add a value of 1 to the value of i on each loop.
The (i) is the parameter telling the console to write the value of i, upon completion it loops until it reaches the number 10.
Displaying Even Numbers
To experiment with if statements, I’ve attempted getting the console to display only even numbers to the user between 0-100 when using a for loop. It appears to have been a success!
Here I took the user answer and subtract the correct answer from it in the parenthesis to apply it to the closeness of the user’s answer. For example, if the user was asked what is 2*8 and the user said 15, the application would perform the subtraction 15 – 16 = -1.
The Math.Abs statement converts any negative number to a positive.
So when seeing 15-16= -1 and the Math.Abs statement converts this to 1, the application can read that the user was 1 away from the correct answer.
Here if else statements can be used to inform the user how close they were to the correct answer, by saying if diff == 1 (being the closest), else if diff <= 10 (being fairly close) and else (being not close at all).
Expanding from the previous post of referencing a random class to create a random number generator I also added the goto Start, so that the user can continuously answer random math questions.
Next I added a switch statement to vary the responses for, I could continue using if else statements but the switch statement is easier to read.
Here I made a new int variable responseIndex which offers 3 responses to the user.
The switch statement uses no semi colon, and in the parenthesis it references the responseIndex.
I then created cases with individual responses and then added a break to end the switch statement and move onto the next one. Then similarly to the else statement in if else, I added a default case, so if none of the other cases are met the computer will resort to the default.
Writing a multiplier question for the user with two different responses.
Here I have written a similar application to the cat question one in my earlier post. Except now I am asking for the application to ensure the user’s answer is correct as well as using if else statements.
Here the int variables have been defined with their own values with implicit variable declaration so that the user has two numbers to multiply.
The if statement asks the computer to check that the user’s answer matches the multiplication of int variables num01 and num02.
Again, if the user’s answer is equal to num01*num02 then they receive a response of correct, if the answer is anything else, they receive the response of incorrect, try again.
Adding Random Numbers (Referencing Random Class)
I learned of the C# language class called random, which has all kinds of methods for random numbers etc. In order to use it I had to reference the class by creating an instance of the random class.
Random numberGenerator = new Random(); references the class.
numberGenerator.Next returns a non-negative number.
However, by just adding (); on the end of each function there are no limitations for the numbers, which could make the questions difficult for the user. So I’ve set limitations for the random numbers.
For choosing between numbers 1-10 I have writting (1,11) as the main value is inclusive, in this case number 1, will be included. However, the max value is excluded, in this case 11 which means the random numbers will only be offered up to 10 as the number 11 is excluded.
Here I am experimenting with if statements, I can see the importance of this for authentication, for games etc.
Here I learned that if statements check if a statement is met and responds to it. You do not use semi colons at the end of if statements.
I familiarised myself with: == equal to, != not equal to etc.
Here I am asking the user to answer the question of how many cats they own. Depending on the number of cats they own they’ll receive a different response.
I discovered Conver.ToInt32 in this area, as the computer needs to know what it is reading.
Another feature I wanted to add was for the user to be able to continue using the calculator without having to close and re-open the application.
At the end of my code I added goto Start; and added the variable Start: at the top of my area of code.
To also to communicate to the user that they could continue if they hit enter, after the answer I added another string, including a new line \n for presentation. When the user hits enter (even though ReadKey will accept any key) the user can enter another division.