Witam, dopiero uczę się c#, niestety podczas bawienia się switchem nie chce działać mi kod. Tu jest kod : http://rextester.com/UPVO31864
edit:
zmien edytor na normalny i zadziala:
https://ideone.com/BqHooD
Twój kod lekko poprawiony:
Tylko, że na rextesterze musisz wejść w [ + ] Show input
I wpisać np.
1
5
10
Console.WriteLine("Podaj 1 - jeśli DODAJ, Podaj 2 - jeśli ODEJMIJ, Podaj 3 - jeśli POMNÓŻ, Podaj 4 - jeśli podziel");
int input = Int32.Parse(Console.ReadLine());
switch(input)
{
case 1:
{
Console.WriteLine("Podaj dwie liczby do dodania");
int d = Int32.Parse(Console.ReadLine());
int d2 = Int32.Parse(Console.ReadLine());
int d3 = d + d2;
Console.WriteLine("Wynik dodawania to {0}", d3);
break;
}
case 2:
{
Console.WriteLine("Podaj dwie liczby do odjecia");
int d = Int32.Parse(Console.ReadLine());
int d2 = Int32.Parse(Console.ReadLine());
int d3 = d - d2;
Console.WriteLine("Wynik dodawania to {0}", d3);
break;
}
default: Console.WriteLine("blad, wybrano zly nuemrek");
break;
}
No, ale co jak ktoś poda załóżmy "20klaka" ? Fajnie by było obsłużyć ten wyjątek, ewentualnie użyć TryParse i dać to w ifa, a moim zdaniem najlepiej to nie Console.ReadLine tylko Console.ReadKey i odczytywać pojedynczy znak. A wtedy nie potrzeba żadnej konwersji typów.
Dokładnie to coś takiego:
switch (Console.ReadKey().Key)
{
case ConsoleKey.D1:
break;
case ConsoleKey.D2:
break;
}
WeiXiao napisał(a):
Twój kod lekko poprawiony:
Tylko, że na rextesterze musisz wejść w
[ + ] Show input
I wpisać np.
1
5
10Console.WriteLine("Podaj 1 - jeśli DODAJ, Podaj 2 - jeśli ODEJMIJ, Podaj 3 - jeśli POMNÓŻ, Podaj 4 - jeśli podziel"); int input = Int32.Parse(Console.ReadLine()); switch(input) { case 1: { Console.WriteLine("Podaj dwie liczby do dodania"); int d = Int32.Parse(Console.ReadLine()); int d2 = Int32.Parse(Console.ReadLine()); int d3 = d + d2; Console.WriteLine("Wynik dodawania to {0}", d3); break; } case 2: { Console.WriteLine("Podaj dwie liczby do odjecia"); int d = Int32.Parse(Console.ReadLine()); int d2 = Int32.Parse(Console.ReadLine()); int d3 = d - d2; Console.WriteLine("Wynik dodawania to {0}", d3); break; } default: Console.WriteLine("blad, wybrano zly nuemrek"); break; }
Zmienienie nazwy zmiennej na Input ma duże znaczenie? I czy kod jest skladniowo dobry jak na swiezaka?
Nie nazywaj zmiennych typu d, d3 itd. Bo się pogubisz. Zamiast Parse
możesz użyć ```
TryParse
Sugerował bym coś takiego.
```csharp
Console.WriteLine("Podaj 1 - jeśli DODAJ, Podaj 2 - jeśli ODEJMIJ, Podaj 3 - jeśli POMNÓŻ, Podaj 4 - jeśli podziel");
int value1;
int value2;
int result;
int.TryParse(Console.ReadLine(), out int input);
switch (input)
{
case 1:
{
Console.WriteLine("Podaj dwie liczby do dodania");
int.TryParse(Console.ReadLine(), out value1);
int.TryParse(Console.ReadLine(), out value2);
result = value1 + value2;
Console.WriteLine("Wynik dodawania to {0}", result);
break;
}
case 2:
{
Console.WriteLine("Podaj dwie liczby do odjecia");
int.TryParse(Console.ReadLine(), out value1);
int.TryParse(Console.ReadLine(), out value2);
result = value1 - value2;
Console.WriteLine("Wynik dodawania to {0}", result);
break;
}
default:
Console.WriteLine("blad, wybrano zly nuemrek");
break;
}