01/10/2018, 09:17

Cho em xin hỏi về C# vấn đề ép kiểu

using System;
namespace ConsoleApplication1
{
class MyFisrtEasyProgramInCshape
{
    public static void Main(string[] args)
    {
        Console.WriteLine("Chuong trinh tinh toan");
        Console.WriteLine("****************************************************************");
        int a, b;
        double s;
        Console.WriteLine("a=");
        a = int.Parse(Console.ReadLine());
        Console.WriteLine("b=");
        b = int.Parse(Console.ReadLine());
        // Tong
        s = a + b;
        Console.WriteLine("Tong={0}",s);
        // Hieu
        s = a - b;
        Console.WriteLine("Hieu={0}", s);
        // Tich
        s = a * b;
        Console.WriteLine("Tich={0}", s);
        // Thuong
        s = (double)a / b;
        Console.WriteLine("Thuong={0}",s);
        Console.ReadKey();
       }
    }
}

Giả như người dùng nhập vào là 5a hay 6b thì mình phải xử lí làm sao đây ạ?

Trần Hoàn viết 11:24 ngày 01/10/2018

bạn dùng try-catch để bắt FormatException và thông báo cho người dùng nhé

Bài liên quan
0