Agunos
Agunos

Reputation: 19

Input in switch case

How to not this got showed up when i'm type 5 which mean default

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

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

This is the source code

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;
}

Upvotes: 0

Views: 111

Answers (2)

Colin Young
Colin Young

Reputation: 3058

You need to test the menu input before printing out the other input prompts:

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;
    }
}

You can also skip the default test since you've handled it earlier and you don't want to take any action in that case.

Upvotes: 4

fhcimolin
fhcimolin

Reputation: 614

You need to check if the operation pilihan is valid before you ask the user for the number inputs. Move the error message from the default to the outer else clause.

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");
}

Upvotes: 3

Related Questions