简体   繁体   English

C#标签和转到

[英]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? 所以,如果我正确理解这一点,你想提示年龄是否小于0或大于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: 首先,不要使用标签,它们很丑,你不想使用它们,而是你可以使用do while循环,但有很多可能性:

Also, instead of using the \\n syntax, you could also use Console.WriteLine 此外,您还可以使用Console.WriteLine ,而不是使用\\n语法

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 正如@noctis所述..应该避免使用goto ..它会产生问题......还请检查 - negetivenumber异常概念......你也可以使用你自己的例外

You could set the pin to -1, and check against it. 您可以将引脚设置为-1,然后检查它。

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. 另外,你可能会因为GOTO声明而陷入宗教战争...... :)通常它不赞成,在这里肯定可以避免,但有些人会说它没问题。 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. Goto Label从C的一开始就是一个相当古老的遗留物,你应该避免它。 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());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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