Znajdowanie największej liczby spośród trzech

0

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.
1
  • Po co dwa razy konwertujesz obiekt do int'a?
  • Ile argumentów ma funkcja Max, a ile Ty wpisujesz?
0

Rozumiem, bardzo dziękuję, myślałem że to co mam wypisać to tylko to co zwraca (a zwraca m), a jednak aby zwróciło m, trzeba na samym końcu wypisać wszystkie inty które były argumentami funkcji?

1
Jumpeq napisał(a)

a jednak aby zwróciło m, trzeba na samym końcu wypisać wszystkie inty które były argumentami funkcji?

Przecież jeżeli wpiszesz złą ilość argumentów to program nawet się nie skompiluje więc odpowiedź jest w sumie oczywista.

1 użytkowników online, w tym zalogowanych: 0, gości: 1