Methods Lab - 09. Greater of Two Values
1.Greater of Two Values
Create a method GetMax() that returns the greater of two values (the values can be of type int, char or string)
Examples
Input |
Output |
int 2 16 |
16 |
char a z |
z |
string aaa bbb |
bbb |
Това ми дава 77/100 и не знам, защо при положение, че кода работи, ако някой е наясно да ми каже, ще съм супер благодарен:
using System;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
string command = Console.ReadLine();
if (command == "int")
{
int first = int.Parse(Console.ReadLine());
int second = int.Parse(Console.ReadLine());
HigherThenTwo(first, second);
}
else if (command == "char")
{
char first = char.Parse(Console.ReadLine());
char second = char.Parse(Console.ReadLine());
HigherThenTwo(first, second);
}
else if (command == "string")
{
string first = Console.ReadLine();
string second = Console.ReadLine();
HigherThenTwo(first, second);
}
}
static int HigherThenTwo(int first, int second)
{
int result = 0;
if (first > second)
{
result = first;
}
else
{
result = second;
}
Console.WriteLine(result);
return result;
}
static char HigherThenTwo(char first, char second)
{
int result;
if (first > second)
{
result = first;
}
else
{
result = second;
}
Console.WriteLine((char)result);
return (char)result;
}
static string HigherThenTwo(string first, string second)
{
string result = string.Empty;
int firstSum = 0;
int secondSum = 0;
foreach (char c in first)
{
firstSum += c;
}
foreach (char c in second)
{
secondSum += c;
}
if (firstSum > secondSum)
{
result = first;
}
else
{
result = second;
}
Console.WriteLine(result);
return result;
}
}
}
И по интересния факт е, че с готовия мотод за comparison работи и ми дава 100/100:
using System;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
string command = Console.ReadLine();
if (command == "int")
{
int first = int.Parse(Console.ReadLine());
int second = int.Parse(Console.ReadLine());
HigherThenTwo(first, second);
}
else if (command == "char")
{
char first = char.Parse(Console.ReadLine());
char second = char.Parse(Console.ReadLine());
HigherThenTwo(first, second);
}
else if (command == "string")
{
string first = Console.ReadLine();
string second = Console.ReadLine();
HigherThenTwo(first, second);
}
}
static int HigherThenTwo(int first, int second)
{
int result = 0;
if (first > second)
{
result = first;
}
else
{
result = second;
}
Console.WriteLine(result);
return result;
}
static char HigherThenTwo(char first, char second)
{
int result;
if (first > second)
{
result = first;
}
else
{
result = second;
}
Console.WriteLine((char)result);
return (char)result;
}
static string HigherThenTwo(string first, string second)
{
string result = string.Empty;
int comparison = first.CompareTo(second);
if (comparison > 0)
{
result = first;
}
else
{
result = second;
}
Console.WriteLine(result);
return result;
}
}
}
Някакви идеи?
Мерси,