我是c#的新手,我在这个小程序中遇到了问题 我想在方法ClientsDetails中返回输入的信息,以便在方法Print()中使用它们。 任何帮助PLZ?
public static void Main(string[] args)
{
ClientsDetails();
Print(???,???,???);
Console.ReadLine();
}
public static void ClientsDetails()
{
Console.Write("Client's first name: ");
string firstName = Console.ReadLine();
Console.Write("Client's last name: ");
string lastName = Console.ReadLine();
Console.Write("Client's birthdate: ");
string birthday = Console.ReadLine();
}
public static void Print(string first, string last, string birthday)
{
Console.WriteLine("Client : {0} {1} was born on: {2}", first, last, Birthday);
}
}
答案 0 :(得分:1)
您可以使用结构:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
struct Person{
public static FirstName;
public static LastName;
public static Birthday;
}
class Program
{
static void Main(string[] args)
{
Person person = ClientsDetails();
Print(person.FirstName, person.LastName, person.Birthday);
Console.ReadKey();
}
public static Person ClientsDetails()
{
Person returnValue = new Person();
Console.Write("Client's first name: ");
returnValue.FirstName = Console.ReadLine();
Console.Write("Client's last name: ");
returnValue.LastName = Console.ReadLine();
Console.Write("Client's birthdate: ");
returnValue.Birthday = Console.ReadLine();
return returnValue;
}
public static void Print(string first, string last, string birthday)
{
Console.WriteLine(String.Format("Client : {0} {1} was born on: {2}", first, last, birthday));
}
}
}
或数组:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string person = ClientsDetails();
Print(person[0], person[1], person[2]);
Console.ReadKey();
}
public static string[] ClientsDetails()
{
string[] returnValue = new string[3];
Console.Write("Client's first name: ");
returnValue[0] = Console.ReadLine();
Console.Write("Client's last name: ");
returnValue[1] = Console.ReadLine();
Console.Write("Client's birthdate: ");
returnValue[3] = Console.ReadLine();
return returnValue;
}
public static void Print(string first, string last, string birthday)
{
Console.WriteLine(String.Format("Client : {0} {1} was born on: {2}", first, last, birthday));
}
}
}
或引用(通过引用传递):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string firstName, lastName, birthday;
ClientsDetails(ref firstName, ref lastName, ref birthday);
Print(firstName, lastName, birthday);
Console.ReadKey();
}
public static void ClientsDetails(ref string firstName, ref string lastName, ref string birthday)
{
Console.Write("Client's first name: ");
firstName = Console.ReadLine();
Console.Write("Client's last name: ");
lastName = Console.ReadLine();
Console.Write("Client's birthdate: ");
birthday = Console.ReadLine();
}
public static void Print(string first, string last, string birthday)
{
Console.WriteLine(String.Format("Client : {0} {1} was born on: {2}", first, last, birthday));
}
}
}
答案 1 :(得分:0)
我刚刚根据您的要求更正了您的计划。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
private static String FirstName;
private static String LastName;
private static String Birthday;
static void Main(string[] args)
{
ClientsDetails();
Print(FirstName, LastName, Birthday);
Console.ReadKey();
}
public static void ClientsDetails()
{
Console.Write("Client's first name: ");
FirstName = Console.ReadLine();
Console.Write("Client's last name: ");
LastName = Console.ReadLine();
Console.Write("Client's birthdate: ");
Birthday = Console.ReadLine();
}
public static void Print(string first, string last, string birthday)
{
Console.WriteLine(String.Format("Client : {0} {1} was born on: {2}", first, last, Birthday));
}
}
}
答案 2 :(得分:-1)
有一种方法可以将所需的参数传递给您的方法,例如,您可以这样做:
String f = "first string";
String l = "last string";
String b = "birthday string";
Print(f,l,b);
BTW,在您的情况下,您似乎希望将用户的输入传递给Print
方法,因此一种简单的方法是只调用Print
内的ClientsDetails
方法{1}}这样的方法:
Print(firstName, lastName, birthday);
有关此内容的综合资源,您可以像往常一样参考docs。目前你可以忽略Async Methods
部分。