Anda di halaman 1dari 3

Estruturas de decisão

private void btnIF_Click(object sender, EventArgs e)


{
int vNum;
vNum = Convert.ToInt32(txtNum.Text);

if (vNum > 0) txtRes.Text = "Valor Positivo";


if (vNum < 0) txtRes.Text = "Valor Negativo";
if (vNum == 0) txtRes.Text = "Valor Nulo";
}

private void btnIF_ELSE_Click(object sender, EventArgs e)


{
int vNota;
vNota = Convert.ToInt32(txtNum.Text);

if (vNota < 10)


txtRes.Text = "Reprovado";
else
txtRes.Text = "Aprovado";
}

private void btnIF_ELSE_IF_Click(object sender, EventArgs e)


{ // < 0, 0-7 Reprovou, 8-9 Exame, 10-16 Aprovado, 17-20 Oral, >0
int vNota;
vNota = Convert.ToInt32(txtNum.Text);

if (vNota < 0)
txtRes.Text = "Inválido";
else if (vNota <= 7)
txtRes.Text = "Reprovou";
else if (vNota <= 9)
txtRes.Text = "Exame";
else if (vNota <= 16)
txtRes.Text = "Aprovado";
else if (vNota <= 20)
txtRes.Text = "Oral";
else
txtRes.Text = "Inválido";
}
1
private void btnBlocos_Click(object sender, EventArgs e)
{
int vNota;
vNota = Convert.ToInt32(txtNum.Text);
txtRes.ForeColor = Color.White;

if (vNota < 10)


{
txtRes.Text = "Reprovado";
txtRes.BackColor = Color.Red;
}
else
{
txtRes.Text = "Aprovado";
txtRes.BackColor = Color.Green;
}
}

private void btnOpRelacionais_Click(object sender, EventArgs e)


{
int vNota;
vNota = Convert.ToInt32(txtNum.Text);

if (vNota >= 10 && vNota <= 20)


txtRes.Text = "Aprovado";
else if (vNota >= 0 && vNota < 10)
txtRes.Text = "Reprovado";
else
txtRes.Text = "Inválida";
}

private void btnSwitch_Click(object sender, EventArgs e)


{
int vNota;
vNota = Convert.ToInt32(txtNum.Text);

switch (vNota)
{
case 1:
txtRes.Text = "Mau";
break;
case 2:
txtRes.Text = "Insuficiente";
break;
case 3:
txtRes.Text = "Suficiente";
break;
case 4:
txtRes.Text = "Bom";
break;
case 5:
txtRes.Text = "Muito bom";
break;
default:
txtRes.Text = "Nota inválida";
break;
}
}

2
//Limpa e desactiva os botões
private void btnLimpar_Click(object sender, EventArgs e)
{
txtNum.Clear();
txtRes.Clear();
txtRes.BackColor = Color.White;
txtNum.Focus();
btnIF.Enabled = false;
btnIF_ELSE.Enabled = false;
btnIF_ELSE_IF.Enabled = false;
btnBlocos.Enabled = false;
btnOpRelacionais.Enabled = false;
btnSwitch.Enabled = false;
}
//Esta parte no evento KeyPress só deixa aceitar números e tecla backspace.
private void txtNum_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsNumber(e.KeyChar) && !(e.KeyChar == (char)Keys.Back))
{
e.Handled = true;
}
}

private void txtRes_TextChanged(object sender, EventArgs e)


{
txtRes.BackColor = Color.Blue;
txtRes.ForeColor = Color.White;
}

private void btnSair_Click(object sender, EventArgs e)


{
Application.Exit();
}
//Activa os botões ao inserir um número
private void txtNum_TextChanged(object sender, EventArgs e)
{
btnIF.Enabled = true;
btnIF_ELSE.Enabled = true;
btnIF_ELSE_IF.Enabled = true;
btnBlocos.Enabled = true;
btnOpRelacionais.Enabled = true;
btnSwitch.Enabled = true;
}
}

Anda mungkin juga menyukai