C# Conditional Statements Review Excercise

I’m studying C# and am currently working on the lessonConditional Statements. I tried one of the excercises they suggest at the end of the lesson. I chose the one saying: “Have a user guess a random number between 1-10 and lets them know if they got it correct, are too low, or are too high”.

Though my code runs without errors, it doesn’t work like expected. When you fill in a random number, say 2, while the set number is 7 (and thus the only correct answer), the system gives back that 2 is correct.

See my code below.

  int number = 7;
  
  Console.WriteLine("Raad een getal tussen 1 en 10");
  Convert.ToInt32(Console.ReadLine());
  
  switch (number) {
  case 1:
    Console.WriteLine("Het goede getal is hoger.");           
      break;
    
  case 2:
    Console.WriteLine("Het goede getal is hoger.");
      break;
    
  case 3:
    Console.WriteLine("Het goede getal is hoger.");
      break;
      
  case 4:
    Console.WriteLine("Het goede getal is hoger.");
      break;
      
  case 5:
    Console.WriteLine("Het goede getal is hoger.");
      break;
      
  case 6:
    Console.WriteLine("Het goede getal is hoger.");
      break;
      
  case 7:
    Console.WriteLine("Correct!");
      break;

  default:
    Console.WriteLine("Het goede getal is lager.");
      break;

Hi and welcome to the Forum!

Take a closer look at your third line of code. You ask for an input of the user and convert it to an Int. What happens to the user input afterwards?

2 Likes

I’m not sure… I was told that any input is automatically treated as a string, so if you expect a number, you should convert it. But maybe I didn’t quite understand it? Still, if I delete Convert.ToInt32, the answer 2 is still correct, even though it shouldn’t be…

You are correct, that you have to convert it to an int, since you want to compare two numbers.

Your number variable stores 7.

In your switch block you check if different cases are true for your variable number.

Did number change in any way or is it still 7?
What do you think?

1 Like

I think I get it :blush: At least, now the outcome is as expected when I run the program!

switch (Convert.ToInt32(Console.ReadLine())) {
case 1:
Console.WriteLine(“Het goede getal is hoger.”);
break;

2 Likes

Nice! Now your switch block checks the user input instead of the number variable.

Have you tried changing your number for something else than 7?
Does your code still work?

1 Like

Thanks for your help! :grin:

Of course the code still ‘works’… But it still states that 7 is correct.

I think I can fix that :thinking:

2 Likes

You’re very welcome!

Sure, feel free to post again, if you get stuck.

Just some hints to point you in the right direction:

  1. At the moment you store only one number and never use it again.

  2. You want to compare two numbers, so you should save the user input to another variable.

  3. Your switch block has redundant code. You define many different cases for each possible number, which often give the same response! (e. g. “Number is higher” or “Number is lower”. But how can you know what number was randomly generated? You can’t change your code while its being run. Let the machine do the thinking!

  4. This can be solved if you think of all the cases there are and what should happen in each case. There are actually only 3 cases which matter for comparing the numbers. Can you figure out which 3 cases there are?
    NOTE: If you want to solve it this way, you shouldn’t use switch. You’d rather prefer if-else conditionals.

2 Likes

Thanks again for your help! It works with different ‘correct numbers’ now!

  	int correctNumber = 6;
  
  Console.WriteLine("Let's play guess the number! Choose a number between 1 and 10");
  
  int guessedNumber = Convert.ToInt32(Console.ReadLine());
     
  if (guessedNumber < correctNumber) {
    Console.WriteLine("The correct number is higher than the number you chose.");
  }
  
  else if (guessedNumber == correctNumber) {
    Console.WriteLine("Correct!");
  }
      
  else {
    Console.WriteLine("The correct number is lower than the number you chose.");
  }
3 Likes