Új hozzászólás Aktív témák

  • ArchElf

    addikt

    válasz Lacces #2423 üzenetére

    Három megoldás, ami nem korlátoz... Ja és Parse helyett TryParse-t érdemes használni...
    static void Tömb1()
    {
    int[] tomb = new int[] { 0, 0, 0 };
    do
    {
    int temp;
    if (!int.TryParse(Console.ReadLine(), out temp))
    temp = 0;
    tomb[2] = tomb[1];
    tomb[1] = tomb[0];
    tomb[0] = temp;
    } while (tomb[0] + tomb[1] + tomb[2] != 8);
    Console.WriteLine(" Az utolsó 3 elem összege 8 ");
    }

    static void Tömb2()
    {
    List<int> tomb = new List<int>(new int[] { 0, 0, 0 });
    int sum;
    do
    {
    int temp;
    if (!int.TryParse(Console.ReadLine(), out temp))
    temp = 0;
    tomb.Add(temp);
    tomb.RemoveAt(0);
    sum = 0;
    foreach (int val in tomb)
    sum += val;
    } while (sum != 8);
    Console.WriteLine(" Az utolsó 3 elem összege 8 ");
    }

    static void TömbLinq()
    {
    List<int> tomb = new List<int>(new int[] { 0, 0, 0 });
    do
    {
    int temp;
    if (!int.TryParse(Console.ReadLine(), out temp))
    temp = 0;
    tomb.Add(temp);
    tomb.RemoveAt(0);
    } while (tomb.Sum() != 8);
    Console.WriteLine(" Az utolsó 3 elem összege 8 ");
    }

    AE

Új hozzászólás Aktív témák