简体   繁体   中英

c# beginner problem

I am trying to learn C# and I have a problem with the following code:

using System;

class IfSelect
{
    public static void Main()
    {
        string myInput;
        int myInt;
        Console.Write("Please enter a number: ");
        myInput = Console.ReadLine();
        myInt = Int32.Parse(myInput);

        if (myInt = 10)
        {
            Console.WriteLine("Your number is 10.", myInt);
        }
    }
}
if(myInt = 10)

Assigns the value of 10 to myInt instead of checking for equality. It should become:

if(myInt == 10)

which is the correct syntax for testing equality.

This:

if (myInt = 10)

needs to be this:

if (myInt == 10)

or it could be this too:

if(myInt.Equals(10))

I know it's probably just a typo but, I thought I would include the link anyway:

http://msdn.microsoft.com/en-us/library/53k8ybth.aspx

here is a link to the Equals function:

http://msdn.microsoft.com/en-us/library/ms173147(VS.80).aspx

Actually

this: myInt = Int32.Parse(myInput); should probably be something like this also

int myInt;

if(Int32.TryParse(myInput, out myInt))
{
  rest of code.
}
else
{
  Console.WriteLine("You didn't provide a number");
}

Just in case the provided input isn't a number.

http://msdn.microsoft.com/en-us/library/system.int32.tryparse.aspx

Instead of if (myInt = 10) you need to use if (myInt == 10) .

The first is the assignment operator, the second is a comparison operator.

Your if statement is assigning 10 to myInt rather than testing for equality.

Do this:

if(myInt == 10)

Change

if (myInt = 10)

to

if (myInt == 10)

Offhand, you're using the assignment operator, not the equality operator. if (myInt = 10) should probably be if (myInt == 10) .

Also, in

Console.WriteLine("Your number is 10.", myInt);

the myInt parameter is pointless. Should either use

Console.WriteLine("Your number is 10.");

or

Console.WriteLine("Your number is {0}.", myInt);
  if (myInt = 10)
  {
   Console.WriteLine("Your number is 10.", myInt);
  } 

Should change = to == to check for equality

You can try this, where you use StreamReader to capture input via the console.

Secondly you need the == (which means "Equals") instead of (=) that is an assignment operator a common mistake when you first start programming.

This however wont work as my code will have an error, but this just gives an example of where your mistake is.

class IfSelect
{
    public static void Main()
    {
            string myInput;
            int myInt;

            StreamReader reader = new StreamReader();

            Console.Write("Please enter a number: ");
            myInput = reader.ReadLine();
            myInt = Int32.Parse(myInput);

            if (myInt == 10)
            {
                Console.WriteLine("Your number is 10.", myInt);
            }
        }

    }

I think there's three problems:

  1. It doesn't compile. (myInt = 10) doesn't assign to myInt - it just wont compile.
  2. If it did,

    Console.WriteLine("Your number is 10.", myInt);

    Will always display 10.

  3. You won't be able to see the result.

Try the following:

class IfSelect
{
public static void Main()
{
    string myInput;
    int myInt;
    Console.Write("Please enter a number: ");
    myInput = Console.ReadLine();
    myInt = Int32.Parse(myInput);

    if (myInt == 10)
    {
        Console.WriteLine(string.Format("Your number is {0}.  Press any key.", myInt));
        Console.ReadLine();
    }
}
}

In addition to using == instead of = it's also a good practice to put the value before the variable like

if(10 == myInt)

So the compiler will catch when you only put 1 equal sign by accident.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM