0

The problem is with line 19. i thought it was the while statement but just noticed its line 19. I've tried various things and i can't find the solution to my error.

using System;

class MainClass {
  public static void Main (string[] args) {
      Random r = new Random();
      int Randomnum= r.Next(0,100);
      int x = 0;

      while(x != Randomnum){
          x = Convert.ToInt32(Console.ReadLine()); 

          if (x < Randomnum)
          Console.WriteLine("higher");

          if (x > Randomnum)
          Console.WriteLine("lower");
       
          if (x = Randomnum)
          Console.WriteLine("corrct!");
      }
  }
}
  • 8
    Last conditional has a single `=` (assignment) instead of double `==` (comparison) – Diado Oct 01 '20 at 12:03
  • `corrct` -> `correct`, sorry. – Trevor Oct 01 '20 at 12:09
  • its fine! i didn't notice that myself, thanks. i don'r understand why one 19 is being weird though. the other 2 if statements work fine but not if x = randomnum :/ – Oliver Sharp Oct 01 '20 at 12:10
  • 1
    @OliverSharp because `x = Randomnum` is an assignment. And an assignment in c# always returns the assigned value. In your case an integer, which cannot be used as condition in an `if` clause. You want `x == Randomnum` (double ==) which is the operator for comparison – derpirscher Oct 01 '20 at 12:26

2 Answers2

2

Looks like you have a syntax issue like Nauman mentioned. The Equality Operator (==) is the comparison operator, checking if their operands are equal or not and the Equals(=) compares the contents of a string. The == Operator compares the reference identity while the = method compares only contents.

Change if (x = Randomnum) to if (x == Randomnum) and it should work.

0

Its syntax issue if (x = Randomnum) use double equal for comparison if (x == Randomnum) single equal is for assignment that's why if condition doesn't receive bool value and its showing this error.

Nauman Asghar
  • 111
  • 10