C#标签和转到

时间:2014-10-28 08:49:33

标签: c#

这是我的完整计划。显然我是初学者。我面临的问题是,如果年龄小于零,那么它会回到几行,但它也会再次询问用户针脚。 :(我该怎么做才能解决这个问题?

 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();
            }
        }
    }

5 个答案:

答案 0 :(得分:3)

所以,如果我正确理解这一点,你想提示年龄是否小于0或大于110?

首先,不要使用标签,它们是丑陋的,你不想使用它们,而是你可以使用do while循环,但有很多可能性:

此外,您还可以使用\n

,而不是使用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();

答案 1 :(得分:1)

你可以在这里使用函数概念,你应该使用函数

            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();}

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

答案 2 :(得分:0)

您可以将引脚设置为-1,然后检查它。

另外,你可能会因为GOTO声明而陷入宗教战争...... :) 通常它不受欢迎,这里肯定可以避免,但有些人会说它没问题。由你决定。

我将一些代码放在一起粘贴。

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;
}
在这种情况下,使用方法会更合适:)

答案 3 :(得分:0)

Goto Label从C的开始是一个相当古老的遗留物,你应该避免它。您可以通过实现循环来实现相同的目标

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());
}

答案 4 :(得分:-1)

在引脚提示之前移动您的状况。

mytable={}
table.insert(mytable, {a, b, c, d, e, f})
...