Question Beginners Problem

sp11883

New member
Joined
Jul 21, 2016
Messages
2
Programming Experience
Beginner
Hi, Ive just started on a Software Development Fundamentals course. Just wondering if anyone can help with the following:

Create a program that continues to ask the user to enter a number until the user enters 0.



Any help would be appreciated
Thanks
 
We're happy to help with specific problems but we're not here to do your homework for you. As JB suggests, make use of the material that you've been given, make an attempt and then, if what you try doesn't work, post here and tell us what you were trying to do, how you were trying to do it (include relevant code) and what happened when you tried (include relevant error messages). If you haven't tried then you don't even know that you can't do it.
 
This is my attempt so far:
        static void Main(string[] args)
        {

            Console.WriteLine("Enter a number");
            string userValue = Console.ReadLine();
            if (userValue != "0")
            {
                Console.WriteLine("You entered: " + userValue);
                Console.WriteLine("Enter a Number");
                string userValue2;
                userValue2 = Console.ReadLine();


            

            }
            else
            {
                Console.WriteLine("You Entered a 0, Game Over");
                Console.ReadLine();
            }

How would i get it to keep repeating the question when a "0" isn't entered?
 
Last edited by a moderator:
For future reference, please post code snippets like this:

[xcode=c#]your code here[/xcode]

or, if you need to add extra formatting, e.g. bold or colour, like this:

[code]your code here[/code]

As for the issue, the first thing to note is that you need to do the same thing multiple times so what code construct do you think you would use for that? If you said "a loop" then you get a gold star. If not then you need to spend more time reading the material you've been given. As for what type of loop, there's no list to enumerate so a 'foreach' loop is out and you don't know how many times you need to loop so a 'for' loop is out too.

That leaves a 'do' loop or a 'while' loop. I'll leave it to you to decide which one but the main difference between them is that a 'do' loop tests na exit condition after executing the body, which means that at least one iteration is always going to be executed, and a 'while' loop tests an exit condition before executing the body.
 
Back
Top Bottom