Nhờ mọi người "tìm" lỗi

Có liên quan đến hàm (Funtion) các cao thủ giúp đỡ em ạ.
Em code, thì báo lỗi biến a,b ở dòng lệnh gọi hàm.
Please tell me the error a,b.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Add_Function
{
    class Program
    {
       
       public static void Main(string[] args)
        {
            int a;
            int b;
            int c;
            while (true)
            {
                Console.Write("\n a = ");
                string a1=Console.ReadLine();
                if (a1.ToUpper() == "EXIT") break;
                try
                {
                    a = Convert.ToInt32(a1);
                    Console.Write(" b = ");
                    string b1 = Console.ReadLine();
                    if (b1.ToUpper() == "EXIT") break;
                    try
                    {
                        b = Convert.ToInt32(b1);
                    }
                    catch (FormatException)
                    {
                        Console.WriteLine(" Please check again ");
                    }
                }
                catch (FormatException)
                {
                    Console.WriteLine(" Please check again ");
                }         
                
                //Console.WriteLine(" => a + b = {0}", c);
                Console.WriteLine(" Do you continue or exit ? ");
            }
            Program prog1 = new Program();

            c = Add(a, b);
// => em code, thì báo lỗi biến a,b ở dòng lệnh gọi hàm

         //Could you tell me  how to use a,b?
         // error a, b

            //Console.Read();
        }//END   Main

         public static int Add(int x, int y)
        {
            int result = x + y;
            return result;
        }//END   Add
    }//END      Program
}

Báo lỗi gì???
Khi có lỗi thì có nghĩa là có mô tả chi tiết về lỗi, KHÔNG CẦN TÌM LỖI, do bạn KHÔNG ĐỌC thôi.

3 Likes

Mình thì không rành C# lắm nhưng khi tìm kiếm trên Google thì thấy bài này https://stackoverflow.com/questions/9233000/why-compile-error-use-of-unassigned-local-variable.
Hiểu sơ sơ là biến cục bộ trong hàm của C# không có giá trị mặc định. Để khắc phục thì bạn sửa lại code :

int a = 0;
int b = 0;

P/s: Lần sau nếu bạn hỏi như vầy thì nên up cả thông báo lỗi lên cho mọi người đọc và dễ dàng sửa hơn.

5 Likes

Nếu bị exception thì a, b bằng mấy bạn :smiley:

4 Likes

Cảm ơn bác thật nhiều; máy chỉ báo lỗi viền đỏ, gạch chân màu đỏ.
Em sửa theo hướng dẫn của bác. => hết lỗi và chạy đúng rồi ạ !
Cảm ơn bác nhiều !

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            int a=0;
            int b=0;
            int c;
            while (true)
            {
                Console.Write("\n a = ");
                string a1 = Console.ReadLine();
                if (a1.ToUpper() == "EXIT") break;
                try
                {
                    a = Convert.ToInt32(a1);
                    Console.Write(" b = ");
                    string b1 = Console.ReadLine();
                    if (b1.ToUpper() == "EXIT") break;
                    try
                    {
                        b = Convert.ToInt32(b1);
                    }
                    catch (FormatException)
                    {
                        Console.WriteLine(" Please check again ");
                    }
                }
                catch (FormatException)
                {
                    Console.WriteLine(" Please check again ");
                }

                //Console.WriteLine(" => a + b = {0}", c);
                
                Program prog1 = new Program();

                c = Add(a, b);
                Console.WriteLine(" Add(a , b) =  {0} + {1} = {2}", a, b, c);
                Console.WriteLine(" Do you continue or exit ? \n");
            }
            
            

            //Console.Read();
        }//END   Main
        public static int Add(int x, int y)
        {
            int result = x + y;
            return result;
        }//END   
    }
}

Dạ, hiện tại chưa có phần này ạ. Bổ sung như sau có đúng không ạ !

try
{
    a = Convert.ToInt32(a1);
    Console.Write(" b = ");
    string b1 = Console.ReadLine();
    if (b1.ToUpper() == "EXIT") break;
    try
    {
        b = Convert.ToInt32(b1);
    }
    catch (FormatException)
    {
        Console.WriteLine(" Please check again ");
        a = 0;
    }
}
catch (FormatException)
{
    Console.WriteLine(" Please check again ");
    b = 0;
}
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?