C# :: Aufgabe #181

6 Lösungen Lösungen öffentlich

Kleine Variablenspielerei

Anfänger - C# von kjaenke - 03.07.2017 um 15:05 Uhr
Schreibe ein Programm das zwei Zahlen entgegen nimmt und jeweils in eine Variable speichert.
Tausche die Werte der Variablen ohne weitere Variablen zu definieren.
Es sind im ganzen Programm nur die zwei vorher definierten Variablen erlaubt!

Viel Spaß :)

Lösungen:

vote_ok
von hollst (13980 Punkte) - 04.07.2017 um 09:59 Uhr
Quellcode ausblenden C#-Code
using System;
using static System.Console;

namespace zahlentausch_181  {

    class Program    {

        static void Main()        {
            int[] input = new int[2];

            do  {
                bool[] bo_input_ok = new bool[] { false, false };

                while (!bo_input_ok[0])                {
                    "Bitte zahl_1 eingeben: ".Message();
                    bo_input_ok[0] = int.TryParse(ReadLine(), out input[0]);
                }
                while (!bo_input_ok[1])                {
                    "Bitte zahl_2 eingeben: ".Message();
                    bo_input_ok[1] = int.TryParse(ReadLine(), out input[1]);
                }

                input = input.tausche();

                ("zahl_1: " + input[0].ToString() + Environment.NewLine + "zahl_2: " + input[1].ToString()).MessageLine();

            }   while (!("again? exit ESC".EndMessage().Key == ConsoleKey.Escape));
        }
    }

    public static class MyExtensions    {

        public static void Message(this string s) => Write(s);

        public static void MessageLine(this string s) => WriteLine(s); 

        public static ConsoleKeyInfo EndMessage(this string s)
        { s.MessageLine(); return ReadKey(true); }

        public static int[] tausche(this int[] input)        {
            int[] result = new int[2];
            input.CopyTo(result, 0);
            result[0] += result[1];
            result[1] = result[0] - result[1];
            result[0] -= result[1];
            return result;
        }
    }
}
2x
vote_ok
von Mexx (2370 Punkte) - 04.07.2017 um 17:46 Uhr
Da es sich hier um Zahlen handelt gehe ich davon aus dass sowas erwartet wird :).

Quellcode ausblenden C#-Code
namespace A181_Variablenspielerei
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 14;
            int b = 21;
            Tauschen(ref a, ref b);
        }

        static void Tauschen(ref int a, ref int b)
        {
            b += a;
            a = b - a;
            b = b - a;
        }
    }
}
vote_ok
von DBqFetti (2480 Punkte) - 06.07.2017 um 21:01 Uhr
Quellcode ausblenden C#-Code
using System;
					
public class Program
{
	public static void Main()
	{
		int a = 5;
		int b = 10;
		
		new Action<int, int>((xa, xb) => {a = xb;b = xa;})(a, b);
	}
}
1x
vote_ok
von Truebsal (50 Punkte) - 10.07.2017 um 10:22 Uhr
Quellcode ausblenden C#-Code
            int a, b;
            Console.WriteLine("Gib die erste Zahl ein:");
            a =Convert.ToInt32( Console.ReadLine());
            Console.WriteLine("Gib die zweite Zahl ein:");
            b = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Deine eingaben sind: {0} und {1}", a, b);
            Console.WriteLine("Die Werte der Variablen werden nun ohne neue Variable vertauscht.");
            b = b + a;
            a = b - a;
            b = b - a;
            Console.WriteLine("Die neuen Werte sind {0} und {1}", a, b);
            Console.ReadLine();
vote_ok
von Exception (7090 Punkte) - 01.06.2018 um 13:50 Uhr
Quick 'n' dirty ab C# 7.0 :)

Quellcode ausblenden C#-Code
using System;

namespace VariablenTausch
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                int a = Convert.ToInt32(args[0]);
                int b = Convert.ToInt32(args[1]);

                Console.WriteLine("a = {0}\nb = {1}\n", a, b);

                (a, b) = (b, a);    // System.ValueTuple

                Console.WriteLine("a = {0}\nb = {1}", a, b);
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadKey();
        }
    }
}
vote_ok
von t.wi (660 Punkte) - 14.06.2019 um 11:30 Uhr
Quellcode ausblenden C#-Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace übung181
{
    class Program
    {
        static void Main(string[] args)
        {
            int c = Convert.ToInt32(Console.ReadLine());
            int x = Convert.ToInt32(Console.ReadLine());

            x = x + c;
            c = x - c;
            x = x - c;
            
            Console.WriteLine(c);
            Console.WriteLine(x);
            Console.ReadKey();
        }
    }
}
1811031

Du scheinst einen AdBlocker zu nutzen. Ich würde mich freuen, wenn du ihn auf dieser Seite deaktivierst und dich davon überzeugst, dass die Werbung hier nicht störend ist.