+ 1
Can anyone spot me where I were wrong?
13 Respuestas
+ 4
@Arjun
https://code.sololearn.com/cJCzMQ7EW25W/#cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoloLearn
{
class Program
{
static void Main(string[] args)
{
string YourName = "";
string a = "Arjun";
YourName = Console.ReadLine();
if (YourName == a) {
Console.WriteLine("Hello, {0}!", YourName);
}
else {
Console.WriteLine("Please try again, {0}.", YourName);
}
}
}
}
+ 3
:::ERROR:::
..\Playground\(15,24): error CS0103: The name 'a' does not exist in the current context
^As advised in your compilation error, you never declared the variable 'a'. Simply declare the variable and you'll be able to utilize it or use a string literal for comparison instead.
Also,...
CHANGE:
if (YourName = a)
TO:
if (YourName == a)
^Currently, you have it ASSIGNING the value to the variable 'YourName'. However, I assume you were wanting to do a comparison instead. When doing a COMPARISON with the value, you'll want to use double-equals sign '=='.
+ 3
Line 13: string YourName = "";
Line 14: string a = "Arjun";
Line 15: YourName = Console.ReadLine();
^
Line 13 is where you declared your string variable 'YourName'.
Line 14 is where we declared your string variable 'a'.
Line 15 is where we assigned the user's input to our variable 'YourName'.
+ 2
@Arjun
As for why, the variables do NOT exist in memory until you declare them, which is why we declare them at the top BEFORE we use them below. Think of it like boxes. We can't put stuff into a box that doesn't even exist. However, if get a box and then put a piece of tape on it with a label of what the box is used for, that is like declaring a variable; we create a space in memory, label it with a name, and determine the type of data it'll hold.
+ 1
thanks Fata1 Err0r ... can you explain the lines 13 14 15? why?
+ 1
@arjun
your code
https://code.sololearn.com/cYhs1XVOMvEq/?ref=app
+ 1
until var a is defined as string . compiler won't check for variable a
it produce error , as no declaration
+ 1
thanks all
0
define the string a
0
how
0
string a = "a";
0
thanks Rajeeb can you elaborate the define the string a
0
oh