Cześć,
Mam napisać program który za pomocą instrukcji warunkowej znajduje i zwraca największą liczbę z trzech, mam użyć funkcji:
int Max(int a, int b, int c)
Napisałem do tego dwa typy programów, jednak żaden się nie kompiluje, w którymś miejscu popełniam błąd.
1 program (trochę na piechotę)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Zad6
{
class Program
{
static int Max(int a, int b, int c)
{
if ((a > b) && (a>c))
{
return a;
}
if((b>a)&&(b>c))
{
return b;
}
if((c>a)&&(c>b))
{
return c;
}
}
static void Main(string[] args)
{
int a, b, c;
Console.WriteLine("Podaj pierwszą liczbę: ");
a = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Podaj drugą liczbę: ");
b = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Podaj trzecią liczbę: ");
c = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Największa liczba to: {0}" + Max(Convert.ToInt32(a), Convert.ToInt32(b), Convert.ToInt32(c)));
Console.ReadKey();
}
}
}
2 program, zmyślniejszy:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Zad6
{
class Program
{
static int Max(int a, int b, int c)
{
int m;
m = a;
if (b > m) m = b;
if (c > m) m = c;
return m;
}
static void Main(string[] args)
{
int a, b, c, m;
Console.WriteLine("Podaj pierwszą liczbę: ");
a = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Podaj drugą liczbę: ");
b = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Podaj trzecią liczbę: ");
c = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Największa liczba to: {0}", Max(Convert.ToInt32((m))));
Console.ReadKey();
}
}
}
W samej funkcji
static int Max(int a, int b, int c)
nie mogę dodać int m
, musi zostać tylko napis z tymi trzema intami.