Reputation: 3
I want the user to enter 3 separate integers and for the program to spit back which numbers are equal to each other of if they are not equal to each other. For my code here, I can get it to say they all equal to each other, and the first two are equal, but not for the first and third. I am assuming it's because my else statement isn't placed in the braces correctly.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace Compare3Numbers
{
class Program
{
static void Main(string[] args)
{
int number1;
int number2;
int number3;
Write("Enter the first number: ");
number1 = int.Parse(ReadLine());
Write("Enter the second number: ");
number2 = int.Parse(ReadLine());
Write("Enter the third number: ");
number3 = int.Parse(ReadLine());
if (number1 == number2 && number2 == number3)
{
if (number2 == number3)
{
WriteLine("All your numbers are equal!");
}
else if (number1 == number2)
{
WriteLine("Your first two numbers are equal!");
}
else if (number1 == number3)
{
WriteLine("Your first number and third number are equal!");
}
else if (number2 == number3)
{
WriteLine("Your second number is equal to your third number!");
}
}
//debug line
WriteLine("Press any key to continue...");
ReadLine();
}
}
}
Upvotes: 0
Views: 484
Reputation: 18155
A Generic approach for 'n' numbers(continue to insert numbers until encountered 'q')
static void Main(string[] args)
{
var numbers = new List<int>();
var run=true;
while(true)
{
var input = Console.ReadLine();
if(input.Equals("q"))
{
break;
}
if(Int32.TryParse(input,out var value))
{
numbers.Add(value);
}
}
var result = numbers
.Select((t,i) => new { Index = i, Value = t })
.GroupBy(g => g.Value)
.Where(g => g.Count() > 1);
foreach(var group in result)
{
var groupItems = group.ToList();
Console.WriteLine($"Your {string.Join(", ",groupItems.Take(groupItems.Count()-1).Select(x=> AddOrdinal(x.Index+1)))} and {AddOrdinal(groupItems.Last().Index+1)} are equal");
}
}
public static string AddOrdinal(int num)
{
if( num <= 0 ) return num.ToString();
switch(num % 100)
{
case 11:
case 12:
case 13:
return num + "th";
}
switch(num % 10)
{
case 1:
return num + "st";
case 2:
return num + "nd";
case 3:
return num + "rd";
default:
return num + "th";
}
}
PS: Borrowing AddOrdinal from samjudson's Answer
Example, for input,
1,2,3,4,5,6,1,2,3,1,2,3,1
Output
Your 1st, 7th, 10th and 13th are equal
Your 2nd, 8th and 11th are equal
Your 3rd, 9th and 12th are equal
Upvotes: 0
Reputation: 813
I would do it like this:
var dictionary = new Dictionary<string, bool>
{
{ "Your first two numbers are equal!", Math.Abs(number1 - number2) == 0 },
{ "Your second number is equal to your third number!", Math.Abs(number2 - number3) == 0 },
{ "Your first number and third number are equal!", Math.Abs(number3 - number1) == 0 }
};
var output = dictionary.Where(i => i.Value).ToList();
var message = output.Count == 3 ? "All your numbers are equal!" : output.FirstOrDefault(i => i.Value).Key;
Console.WriteLine(message);
Upvotes: -1
Reputation: 81493
if (number1 == number2 && number2 == number3)
// all
else if (number1 == number2)
// first second
else if (number1 == number3)
// first last
else if (number2 == number3)
// second last
else
// all broken
Additional Reading
Upvotes: 4
Reputation: 1120
The message in your code will be written only when first two numbers are equal. And there is no check that will run for the first and third and second and third number.
Try to debug your code step by step to see the path.
I would recommend you to use code analysers. The problem of "double compare the same condition" your code will be displayed by Sonar Lint.
On the second line
if (number1 == number2)
You will get:
Warning S2583 Change this condition so that it does not always evaluate to 'true'; some subsequent code is never executed.
The correct code can look like this:
if (number1 == number2)
{
if (number2 == number3)
{
WriteLine("All your numbers are equal!");
}
else
{
WriteLine("Your first two numbers are equal!");
}
}
else
{
if (number1 == number3)
{
WriteLine("Your first number and third number are equal!");
}
// there is no need for else, because this would mean, that (number1 == number2) and this is the part, where these number differs
if (number2 == number3)
{
WriteLine("Your second number and third number are equal!");
}
}
Upvotes: 0
Reputation: 19
Look at it this way
if (number1 == number2)
{
//rest of your if statements
}
Since 1 and 2 arent equal nothing inside the if statement will execute. You need to check number1 == number3 outside of that.
Upvotes: 0