I want to write a code in C# that asks a user to input 2 numbers to compare which is higher but cant seem to get an output from this.
{ class Program {
public static int Max { get; private set; }
static void Main(string[] args)
{
Console.WriteLine("Please enter 2 (two) integer numbers on a separate line: ");
int num1 = int.Parse(Console.ReadLine());
int num2 = int.Parse(Console.ReadLine());
switch (Max)
{
case 1:
if (num1 < num2)
{
Console.WriteLine(num2 "is Maximum");
}
break;
case 2:
if (num1 > num2)
{
Console.WriteLine(num1 "is Maximum");
}
break;
}
}
}
}
CodePudding user response:
You are using the "Max" variable on the condition of the switch, but you never actually declare "Max", and the switch is only expectating the numbers 1 or 2, because you put case 1: and case 2:
So the correct way to show output is this:
if(num1 > num2)
{
Max = 1;
}
else
{
Max = 2;
}
switch(Max)
{
case 1:
Console.WriteLine(num1 "is Maximum");
break;
case 2:
Console.WriteLine(num2 "is Maximum");
break;
}
This is just in case you need to use a switch case, because you can do it only with a simple if
CodePudding user response:
case cannot be used to compare values of two numbers you might just want to use if-else statement or if you just want a application of case statement use this :
switch (input)
{
case 1:
Console.WriteLine("a");
break;
case 2:
Console.WriteLine("b");
break;
case 3:
Console.WriteLine("c");
break;
case 4:
Console.WriteLine("d");
break;
case 5:
Console.WriteLine("e");
break;
case 6:
Console.WriteLine("f");
break;
case 7:
Console.WriteLine("g");
break;
default:
Console.WriteLine("Invalid input");
break;
}