If the code is : int x=4; int y=2; int z=x%y; Console. WriteLine("value of z is {2}",z); | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
+ 8

If the code is : int x=4; int y=2; int z=x%y; Console. WriteLine("value of z is {2}",z);

Its value should be.. Value of z is 0,, then why this code is showing error...??

1st Nov 2016, 5:17 PM
Uplabdhi Singh
Uplabdhi Singh - avatar
5 Respuestas
+ 29
Ok, in the string "value of z is {2}" , you will first need to ask yourself, what is {2} in that statement? In your Console.WriteLine, it detects 'z' as element number {1} . Why is that so? Because it will make no sense if it has x as element number 0 or y as element number1 since x and y is not inside the input of Console.WriteLine; the system does not detect something as an element unless its inside the input. But writing "value of z is {1}" will still get you an error. This is due to the fact that the system,WriteLine,starts counting it's element number after the string. Beacuse it treats string as what it needs to write , not what it needs to store. Hence, z is actually element number {0} because the string is 'ignored'. so the correct one will be: Console.WriteLine("Value of z is {0}" , z); (Pretty complicated, I know.) Alternatively, a simplier way will be Console.WriteLine("Value of z is " + z); ^^ for that method, remember to add a space after the word 'is', the system will not automatically add a space for you.
1st Nov 2016, 5:48 PM
Wen Qin
Wen Qin - avatar
+ 1
you have to write {0}
14th Nov 2016, 2:14 PM
Nes Denon
Nes Denon - avatar
0
Thanq, for explaining the basic concept. :)
2nd Nov 2016, 5:43 AM
Uplabdhi Singh
Uplabdhi Singh - avatar
0
well explained
10th Nov 2016, 9:56 PM
Mercy cheko
0
0
19th Nov 2016, 6:13 PM
Harshal Kukade
Harshal Kukade - avatar