-
February 19th, 2015, 04:21 PM
#1
Instead of "goto"-method?
Hi!
I'm fairly new to programming, and I've realized that people reaaalllyy don't like "goto"-methods. I was wondering how one might write code that allows a user to decide how many entries to put in? For example, in the code below, the user inputs a name, and is then asked wether he/she wants to input another name. How could I do this without having to use go-to method?
Thanks in advance
Code:
using System;
namespace GoToTest
{
public class GoToTest
{
public static void Main()
{
InputName:
string name;
Console.Write("Input name: ");
name = Console.ReadLine();
string decision;
Console.WriteLine(""); //Empty line for increased readability
Console.WriteLine("Would you like to input another name? Yes - No");
decision = Console.ReadLine();
if (decision == "Yes")
{
goto InputName;
}
else
{
Console.WriteLine("Name is " + name);
}
}
}
}
-
February 19th, 2015, 04:36 PM
#2
Re: Instead of "goto"-method?
Victor Nijegorodov
-
February 19th, 2015, 10:02 PM
#3
Re: Instead of "goto"-method?
Code:
using System;
namespace GoToTest
{
public class GoToTest
{
public static void Main()
{
string name;
Console.Write("Input name: ");
name = Console.ReadLine();
string decision;
bool isUserExiting = false;
while(isUserExiting == false)
{
Console.WriteLine(""); //Empty line for increased readability
Console.WriteLine("Would you like to input another name? Yes - No or Exit");
decision = Console.ReadLine();
if (decision == "Yes")
{
Console.Write("Input name: ");
name = Console.ReadLine();
}
else
{
Console.WriteLine("Name is " + name);
isUserExiting = true;
Console.ReadLine();
}
if(decision == "Exit")
{
isUserExiting = true;
}
}
}
}
}
ya guess i should of proof read it i got side tracked
Last edited by willmotil; February 19th, 2015 at 11:48 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|