输入开关盒

时间:2019-03-15 13:45:13

标签: c#

对不起,我的英语不好,我仍然是初学者。

当我输入5表示默认值时,怎么不显示

Console.Write("Input nilai a = ");
int a = Convert.ToInt32(Console.ReadLine());

Console.Write("Input nilai b = ");
int b = Convert.ToInt32(Console.ReadLine());
Console.WriteLine();

这是源代码

Console.Write("Input Nomor Menu [1..4]: ");
int pilihan = int.Parse(Console.ReadLine());
Console.WriteLine();

Console.Write("Input nilai a = ");
int a = Convert.ToInt32(Console.ReadLine());

Console.Write("Input nilai b = ");
int b = Convert.ToInt32(Console.ReadLine());
Console.WriteLine();


switch (pilihan)
{
    case 1:
        Console.WriteLine("Hasil Penambahan {0} + {1} = {2}", a, b, Penambahan(a, b));
        break;
    case 2:
        Console.WriteLine("Hasil Pengurangan {0} - {1} = {2}", a, b, Pengurangan(a, b));
        break;
    case 3:
        Console.WriteLine("Hasil Perkalian {0} * {1} = {2}", a, b, Perkalian(a, b));
        break;
    case 4:
        Console.WriteLine("Hasil Pembagian {0} / {1} = {2}", a, b, Pembagian(a, b));
        break;           
    default:
        Console.WriteLine("Maaf, Menu Yang Anda Pilih Tidak Tersedia"); // Sorry, the Menu You Choose is Not Available
        break;
}

2 个答案:

答案 0 :(得分:3)

在打印出其他输入提示之前,您需要先测试菜单输入

Console.Write("Input Nomor Menu [1..4]: ");
int pilihan = int.Parse(Console.ReadLine());
Console.WriteLine();

if (pilihan > 4 || pilihan < 1)
{
    Console.WriteLine("Maaf, Menu Yang Anda Pilih Tidak Tersedia"); // Sorry, the Menu You Choose is Not Available
}
else
{    
    Console.Write("Input nilai a = ");
    int a = Convert.ToInt32(Console.ReadLine());

    Console.Write("Input nilai b = ");
    int b = Convert.ToInt32(Console.ReadLine());
    Console.WriteLine();


    switch (pilihan)
    {
        case 1:
            Console.WriteLine("Hasil Penambahan {0} + {1} = {2}", a, b, Penambahan(a, b));
            break;
        case 2:
            Console.WriteLine("Hasil Pengurangan {0} - {1} = {2}", a, b, Pengurangan(a, b));
            break;
        case 3:
            Console.WriteLine("Hasil Perkalian {0} * {1} = {2}", a, b, Perkalian(a, b));
            break;
        case 4:
            Console.WriteLine("Hasil Pembagian {0} / {1} = {2}", a, b, Pembagian(a, b));
            break;
    }
}

您也可以跳过默认测试,因为您已经处理过默认测试,并且在这种情况下不想采取任何措施。

答案 1 :(得分:3)

在要求用户输入数字之前,您需要检查操作pilihan是否有效。将错误消息从default移到外部else子句。

Console.Write("Input Nomor Menu [1..4]: ");
int pilihan = int.Parse(Console.ReadLine());
Console.WriteLine();

if (pilihan > 0 && pilihan < 5)
{
    Console.Write("Input nilai a = ");
    int a = Convert.ToInt32(Console.ReadLine());

    Console.Write("Input nilai b = ");
    int b = Convert.ToInt32(Console.ReadLine());
    Console.WriteLine();


    switch (pilihan)
    {
        case 1:
            Console.WriteLine("Hasil Penambahan {0} + {1} = {2}", a, b, 1);
            break;
        case 2:
            Console.WriteLine("Hasil Pengurangan {0} - {1} = {2}", a, b, 2);
            break;
        case 3:
            Console.WriteLine("Hasil Perkalian {0} * {1} = {2}", a, b, 3);
            break;
        case 4:
            Console.WriteLine("Hasil Pembagian {0} / {1} = {2}", a, b, 5);
            break;
        default:
            break;
    }
}
else
{
    Console.WriteLine("Maaf, Menu Yang Anda Pilih Tidak Tersedia");
}