+ 3
Where is my fault in this code? I want to write my age without using string
static void Main(string[] args) { string yourName; int yourAge = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("What is your name and age?"); //Type your name, press enter and type your age yourName = Console.ReadLine(); yourAge = Console.ReadLine (); Console.WriteLine("Hello {0}; You are {1} years old", yourName, yourAge); }
5 ответов
+ 3
if thats the case, there's still a way, change ya code to be like
string yourname;
int yourage;
yourname = Console.ReadLine();
yourage = int.Parse(Console.ReadLine());
Console.WriteLine("Name: {0}, Your age is: {1}", yourname, yourage);
note that int.Parse is the same as Convert.ToInt32.
the problem with your code above is your declared age as an int and almost at the end of the code you enteted a string
yourage = Console.ReadLine();
by doing this you were assigning it a string value yet you had declared it as an int
+ 1
the fault with your code is that you declared the age as int and the asked for a first input of age which you converted to an int.
the problem comes at the end of the code where you asked for another age which you didn't change to integer.
to correct your code just declare age as a string like
string yourname;
string yourage;
yourname = Console.ReadLine();
yourage = Console.ReadLine();
Console.WriteLine("Hello {0}, Your age is: {1}", yourname, yourage);
0
string="Hello"
string="You are "
int =a//your age
string="years old"
I don not know very well C# but this i will do in c++. XD
0
@Tuchy is there any way to write it with int?
i just want to learn conversion. I have seen an example on SoloLearn and it works and i wanted to insert it to here but i couldn't achieve
0
@Tuchy so Console.ReadLine() is a string.
Thank you so much :)