简体   繁体   中英

C# label and goto

This is my full program. Obviously I am a beginner. The problem I face is that if the age is less than zero then it goes back a few lines, but it than asks the user again for the pin also. :( What can I do to fix this ?

 using System;

    namespace Examples
    {
        class Program
        {
            static void Main(string[] args)
            {
                string name;
                string city;
                int age;
                int pin;

                // \n is used for line-break
                Console.Write("Enter your name :  ");
                name = Console.ReadLine();

                Console.Write("\nEnter Your City :  ");
                city = Console.ReadLine();

                age:
                Console.Write("\nEnter your age :  ");
                age = Int32.Parse(Console.ReadLine());

                Console.Write("\nEnter your pin :  ");
                pin = Int32.Parse(Console.ReadLine());

                if (age < 0 || age >= 110)
                {
                    goto age;

                }


                // Printing message to console
                //formatting output
                Console.WriteLine("==============");
                Console.WriteLine("Your Complete Address:");
                Console.WriteLine("============\n");

                Console.WriteLine("Name = {0}", name);
                Console.WriteLine("City = {0}", city);
                Console.WriteLine("Age = {0}", age);
                Console.WriteLine("Pin = {0}", pin);
                Console.WriteLine("===============");

                Console.ReadLine();
            }
        }
    }

So, if I'm understanding this correctly, you want to prompt for the age if it's less than 0 or more than 110?

First of all, don't go with labels, they are ugly and you don't want to use them, instead you could use do while loop, but there are plenty of possibilities:

Also, instead of using the \\n syntax, you could also use Console.WriteLine

string name;
string city;
int age;
int pin;

// \n is used for line-break
Console.Write("Enter your name :  ");
name = Console.ReadLine();

Console.Write("\nEnter Your City :  ");
city = Console.ReadLine();
age = -1;

while (age < 0 || age >= 110)
{
    Console.Write("\nEnter your age :  ");
    age = Int32.Parse(Console.ReadLine());

    if (age < 0 || age >= 110)
    {
        Console.WriteLine("The age must be between 0 and 110.");
    }
}


Console.Write("\nEnter your pin :  ");
pin = Int32.Parse(Console.ReadLine());


// Printing message to console
//formatting output
Console.WriteLine("==============");
Console.WriteLine("Your Complete Address:");
Console.WriteLine("============\n");

Console.WriteLine("Name = {0}", name);
Console.WriteLine("City = {0}", city);
Console.WriteLine("Age = {0}", age);
Console.WriteLine("Pin = {0}", pin);
Console.WriteLine("===============");

Console.ReadLine();

u can use functions concept over here, and u should use functions

            Console.Write("\nEnter your age :  ");
            age = Int32.Parse(Console.ReadLine());
            if (age < 0 || age >= 110)
            {
               //show error msg

            }
            Console.Write("\nEnter your pin :  ");
            pin = Int32.Parse(Console.ReadLine());

           //if every data is corrent - run function
           showinfo();   
////////////
           showinfo()
           { 
            Console.WriteLine("==============");
            Console.WriteLine("Your Complete Address:");
            Console.WriteLine("============\n");

            Console.WriteLine("Name = {0}", name);
            Console.WriteLine("City = {0}", city);
            Console.WriteLine("Age = {0}", age);
            Console.WriteLine("Pin = {0}", pin);
            Console.WriteLine("===============");

            Console.ReadLine();}

as stated by @noctis ..use of goto should be avoided.. its creates issues ... also please check for - negetivenumber exception concepts...u can use the exception of ur own too

You could set the pin to -1, and check against it.

also, you might get into a religious war with that GOTO statement ... :) usually it's frowned upon, it can surely be avoided here, but some people will say it's ok. up to you really.

I'll put some code together and paste it.

void Main()
{
    string name;
    string city;
    int age;
    int pin;

    // \n is used for line-break
    Console.Write("Enter your name :  ");
    name = Console.ReadLine();

    Console.Write("\nEnter Your City :  ");
    city = Console.ReadLine();

    age = GetAge();

    //... eluded

Console.ReadLine();
}

// Define other methods and classes here
private int GetAge() {
    Console.Write("\nEnter your age :  ");
    int age = -1;
    while (age <0 || age>110) {
        age = Int32.Parse(Console.ReadLine());
    }
    return age;
}

using a method would be more appropriate in this case :)

Goto Label is a quite old relict from the very beginnings of C, you should avoid it. You may achieve the same by implementing a loop

Console.WriteLine("Enter your pin :  ");
pin = Int32.Parse(Console.ReadLine());
while (age < 0 || age >= 110)
{
   Console.WriteLine("Enter your age :  ");
   int age = Int32.Parse(Console.ReadLine());
}

Just move your condition before the pin prompt.

age:
Console.Write("\nEnter your age :  ");
age = Int32.Parse(Console.ReadLine());

if (age < 0 || age >= 110)
{
    goto age;
}

Console.Write("\nEnter your pin :  ");
pin = Int32.Parse(Console.ReadLine());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM