using System;
namespace Mounova
{
class Chanatza
{
static void Main()
{
int a, b, c, d, e, sum;
Console.Write("Input your first number: ");
a = int.Parse(Console.ReadLine());
Console.Write("Input your second number: ");
b = int.Parse(Console.ReadLine());
Console.Write("Input your third number: ");
c = int.Parse(Console.ReadLine());
Console.Write("Input your fourth number: ");
d = int.Parse(Console.ReadLine());
Console.Write("Input your fifth number: ");
e = int.Parse(Console.ReadLine());
Console.WriteLine("sum of {0} + {1} + {2} + {3} + {4} + {5}",a,b,c,d,e,sum);
Console.ReadLine();
}
}
}
คือมันฟ้องว่าผิดตรง sum ตรงที่เรืองแสงอ่ะครับ อยากรู้ว่ามันผิดตรงไหนเหรอครับ อ่านError เเล้วไม่เข้าใจมันฟ้องว่า
using System;
namespace Mounova
{
class Chanatza
{
static void Main()
{
int a, b, c, d, e, sum = 0;
Console.Write("Input your first number: ");
a = int.Parse(Console.ReadLine());
Console.Write("Input your second number: ");
b = int.Parse(Console.ReadLine());
Console.Write("Input your third number: ");
c = int.Parse(Console.ReadLine());
Console.Write("Input your fourth number: ");
d = int.Parse(Console.ReadLine());
Console.Write("Input your fifth number: ");
e = int.Parse(Console.ReadLine());
Console.WriteLine("sum of {0} + {1} + {2} + {3} + {4} = {5}",a,b,c,d,e,sum);
Console.ReadLine();
}
}
}
แต่ก็ไม่ได้ พอกด 5 ครั้งผลลัพธ์มันเท่ากับ 0 สงสัยคงประกาศตัวแปร = 0
แต่พอเอา = 0;
ก็ฟ้องว่า
Use of inassigned local variable 'sum'
using System;
namespace Mounova
{
class Chanatza
{
static void Main()
{
int a, b, c, d, e, sum;
Console.Write("Input your first number: ");
a = int.Parse(Console.ReadLine());
Console.Write("Input your second number: ");
b = int.Parse(Console.ReadLine());
Console.Write("Input your third number: ");
c = int.Parse(Console.ReadLine());
Console.Write("Input your fourth number: ");
d = int.Parse(Console.ReadLine());
Console.Write("Input your fifth number: ");
e = int.Parse(Console.ReadLine());
sum = a + b + c + d + e;
Console.WriteLine("sum of {0} + {1} + {2} + {3} + {4} = {5}",a,b,c,d,e,sum);
Console.ReadLine();
}
}
}