How to fix duplicated questions (using Readline() and return)

Issue

This Content is from Stack Overflow. Question asked by gon

I’m new to C#.
I’m creating a small program that input number by a user returns to the number and display it.
It works but a user is asked the same questions twice.
I just need one.
I’d appreciate if anyone help me with that.

Here is output of the program.
enter image description here

Here is my programming.
Class1

using System;
using System.Numerics;
using System.Reflection.Emit;
using static System.Console;
using System.IO;

namespace aaaa
{
    public class Program
    {
        public static void Main(string[] args)
        {
            TryReturn tryreturn = new TryReturn();
            tryreturn.template();
        }
     
    }
}

Class2

using System;
using System.Numerics;
using System.Reflection.Emit;
using static System.Console;
using System.IO;

namespace aaaa
{
    public class TryReturn
    {
        public int entryNum;

        public void template()
        {
            returnyNumber();
            DisplayNumber();
        }
        public void DisplayNumber()
        {
            entryNum = returnyNumber();
            WriteLine("number is " + entryNum);

            Read();
        }
        public int returnyNumber()
        {
            int entryNum;
            Write("Choose 1 or 2 >> ");
            do
            {
                int.TryParse(ReadLine(), out entryNum);
                if (entryNum == 1 || entryNum == 2)
                {
                    break;
                }
                else
                {
                    Write("Invalid entry! Enter either '1' or '2' >> ");
                }
            }
            while (true);
            return entryNum;
        }
    }
}



Solution

Check the Answers

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?