none
C# exercise - help needed RRS feed

  • Question

  • How to display 2 inputs for example 1 and 9 as 19 and making it as a variable.

    thanks in advance.

    Friday, September 28, 2018 12:30 PM

All replies

  • Hi,

    how you want to display it(console, wpf, forms, 3d, external device with interface ...). How you want to do the input(console.readline ..., wpf.textbox ..., ...)? You need to give us some more informations then we can help you for sure.

    Greetings, Chris


    • Edited by DerChris88 Friday, September 28, 2018 12:39 PM
    Friday, September 28, 2018 12:39 PM
  • How to display 2 inputs for example 1 and 9 as 19 and making it as a variable.

    As DerChris88 stated, you need to be a lot more specific about the context
    and requirements of your task. If this is for a course assignment then you
    need to use the information you have already been taught - and not more
    advanced methods. The same is generally true if you are studying on your
    own using a textbook.

    In either case, we have no way of knowing what you have already learned or
    what you are expected to use to solve the exercise.

    Here's a simple example that illustrates some of the steps involved in
    doing such data manipulations and displays. It assumes that input has
    been received via the usual methods, such as reading from the console.

    static void Main(string[] args)
    {
        string Input1 = "1"; // simulated input
        string Input2 = "9"; // simulated input
    
        string value = Input1 + Input2;
    
        int ival = 0;
    
        if (!int.TryParse(value, out ival))
        {
            Console.WriteLine("Conversion of {0} to integer failed!", value);
        }
        else Console.WriteLine("string {0} = integer {1}", value, ival);
    
        Console.ReadLine();
    }
    

    - Wayne

    Friday, September 28, 2018 4:25 PM
  • int n = 1;

    int m = 9;

    String result = n.ToString() + m.ToString();

    :)

    You need to study how to ask questions - other way will got proper, but completely useless answers. 


    Sincerely, Highly skilled coding monkey.

    Friday, September 28, 2018 4:30 PM
  • Let's talk about the input. You can use Console.ReadLine() to read a whole line from the console. Here is an example:

    static void Main(string[] args)
    {
        // Console.ReadLine() reads a line from the console till enter is pressed
        // The line is set to the variable str
        string str = Console.ReadLine(); // If the program reaches this point, you can input eg '1[enter]'
                                         // Now you could just concat a new line to the string str
        str += Console.ReadLine(); // If the program reaches this point, you can input eg '9[enter]'
                                   // Now str is eg '19'
        Console.WriteLine(str);
        // If you want to get the number 19 you need to parse it
    
        if (Int32.TryParse(str, out int i))
        {
            Console.WriteLine("It's a valid Integer.");
            // Do something with i
        }
        else
        {
            Console.WriteLine("It's not a valid Integer!");
        }
        Console.ReadLine(); // This line is to avoid closing to early
    }

    Here is another way to get keyboard input from console:

    static void Main(string[] args)
    {
        // This is how you can read keys till you press escape
        string str = "";
        ConsoleKeyInfo key;
        while ((key = Console.ReadKey()).Key != ConsoleKey.Escape)
        {
            str += Console.ReadKey().KeyChar;
        }
        // Write the input to the console
        Console.WriteLine(str);
        // And parsing again            
        if (Int32.TryParse(str, out int i))
        {
            Console.WriteLine("It's a valid Integer.");
            // Do something with i
        }
        else
        {
            Console.WriteLine("It's not a valid Integer!");
        }
        Console.ReadLine();
    }

    Greetings, Chris


    Friday, September 28, 2018 5:48 PM
  • Since this is obviously a homework assignment, I would have asked @Skylinee to first post what code he/she has already written and give simple suggestions from there. All of the above coding suggestions together have pretty much written his/her homework assignment completely.  =0(

    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Sunday, September 30, 2018 4:53 PM
    Moderator
  • Hi BonnieB,

    you are right. I will consider this in future.

    Greetings, Chris

    Sunday, September 30, 2018 6:23 PM
  • Hi Skylinee,

    Is there any update? Do you resolve the issue, I think WayneAKing & DerChris88  provide a good idea

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, October 2, 2018 2:40 AM
    Moderator