gpt4 book ai didi

C# 标签和转到

转载 作者:行者123 更新时间:2023-11-30 16:10:01 25 4
gpt4 key购买 nike

这是我的完整程序。显然我是初学者。我面临的问题是,如果年龄小于零,那么它会返回几行,但它也会再次向用户询问 pin。 :( 我能做些什么来解决这个问题?

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

最佳答案

那么,如果我没理解错的话,你想提示年龄小于 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();

关于C# 标签和转到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26604063/

25 4 0