Em mới tự học C# được 2 ngày, thử làm chương trình tính toán. Cụ thể, khi em có thêm return vào thì khi nhập 1 số và 1 phép toán, chương trình sẽ không bị khựng lại; nhưng khi em bỏ return ra, chương trình sẽ lỗi ngay, chỉ khi em stop debug thì chương trình mới tắt. Bác nào rành về này chỉ giáo giúp em. Tks
p/s: code ở dưới:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace practice
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
private void labelKetQua_Click(object sender, EventArgs e)
{
}
private void buttonTinhToan_Click(object sender, EventArgs e)
{
string strSoA = textBoxSoA.Text;
string strSoB = textBoxSoB.Text;
// Kiểm tra có nhập số chưa
if (strSoA == "" && strSoB == "")
{
MessageBox.Show("Bạn chưa nhập cả hai số");
return; // tại sao khi bỏ các lệnh return thì chương trình lỗi, các lệnh return này có tác dụng gì bên C#?
}
else if (strSoA == "")
{
MessageBox.Show("Bạn chưa nhập số A");
return;
}
else if (strSoB == "")
{
MessageBox.Show("Bạn chưa nhập số B");
return;
}
// kiểm tra phép toán và tính toán
string phepToan = comboBoxPhepToan.Text;
switch (phepToan)
{
case "+":
textBoxKetQua.Text = Convert.ToString(Convert.ToInt16(strSoA) + Convert.ToInt16(strSoB));
break;
case "-":
textBoxKetQua.Text = Convert.ToString(Convert.ToInt16(strSoA) - Convert.ToInt16(strSoB));
break;
case "*":
textBoxKetQua.Text = Convert.ToString(Convert.ToInt16(strSoA) * Convert.ToInt16(strSoB));
break;
case "/":
textBoxKetQua.Text = Convert.ToString(Convert.ToDouble(strSoA) / Convert.ToDouble(strSoB));
break;
case "chia lấy dư":
textBoxKetQua.Text = Convert.ToString(Convert.ToInt16(strSoA) % Convert.ToInt16(strSoB));
break;
default:
MessageBox.Show("Bạn chưa chọn phép toán");
break;
}
}
}
}