I was learning to code and I was almost sure that my code is correct. Unfortunately for unknown for me reason I did it wrong. My job was to:
Write a program that reads three numbers from the console and then outputs the middle value of the three numbers. For example:
>54
>4456
>2
54
My code outputs only these 2 orders:
1
2
3 // output = 2
&
2
3
1 // output = 2
but when I'm writing this in this order:
3
2
1
There's no any output.
Here's my code:
And this is the code which seems to work well:
Could you please tell me what's wrong with my code and what I have to change?
Write a program that reads three numbers from the console and then outputs the middle value of the three numbers. For example:
>54
>4456
>2
54
My code outputs only these 2 orders:
1
2
3 // output = 2
&
2
3
1 // output = 2
but when I'm writing this in this order:
3
2
1
There's no any output.
Here's my code:
C#:
using System;
namespace ConsoleInput
{
public class TheMiddle
{
public static void Main(string[] args)
{
string aString = Console.ReadLine();
string bString = Console.ReadLine();
string cString = Console.ReadLine();
int a = int.Parse(aString);
int b = int.Parse(bString);
int c = int.Parse(cString);
int result;
if (a > b)
{
if (c > a)
result = a;
else if (b > c)
result = b;
else
result = c;
}
else
{
if (b < c)
result = b;
else if (a > c)
result = a;
else
result = c;
Console.WriteLine(result);
}
}
}
}
And this is the code which seems to work well:
C#:
using System;
namespace ConsoleInput
{
public class TheMiddle
{
public static void Main(string[] args)
{
string aString = Console.ReadLine();
string bString = Console.ReadLine();
string cString = Console.ReadLine();
int a = int.Parse(aString);
int b = int.Parse(bString);
int c = int.Parse(cString);
int result;
if (a < b)
{
if (c < a)
result = a;
else if (c > b)
result = b;
else
result = c;
}
else
{
if (c < b)
result = b;
else if (c > a)
result = a;
else
result = c;
}
Console.WriteLine(result);
}
}
}
Could you please tell me what's wrong with my code and what I have to change?