如何在C#方法中打印多个值?

时间:2015-07-05 09:46:13

标签: c# methods

我想通过PrintStudentDetails(字符串优先,字符串最后,字符串生日)方法从GetStudentInformation()方法打印值。请查看以下代码:

    [self imageNotificationListeners];

- (void)imageNotificationListeners
{
    NSLog(@"The imageNotificationListeners is being called");
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(chooseImage)
                                                 name:@"ThreeFactsEnglish" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(chooseImage)
                                                 name:@"ThreeFactsChinese" object:nil];

}

如何仅使用非类别的方法打印值?我已经对其进行了研究并发现了- (void)chooseImage { NSLog(@"The chooseImage is being called"); _modelArray = [NSMutableArray arrayWithObjects:[[ImageModel alloc] initWithImageName:@"3FactsEnglishPage1.png"], [[ImageModel alloc] initWithImageName:@"3FactsEnglishPage2.png"], [[ImageModel alloc] initWithImageName:@"3FactsEnglishPage3.png"], [[ImageModel alloc] initWithImageName:@"3FactsEnglishPage4.png"], [[ImageModel alloc] initWithImageName:@"3FactsEnglishPage5"], nil]; } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Module3_HomeWork { class Program { static void GetStudentInformation() { Console.WriteLine("Enter the student's first name: "); string firstName = Console.ReadLine(); Console.WriteLine("Enter the student's last name: "); string lastName = Console.ReadLine(); Console.WriteLine("Enter the student's birthdate: "); DateTime birthdate = Convert.ToDateTime(Console.ReadLine()); Console.WriteLine("Enter the student's address line 1: "); string add1 = Console.ReadLine(); Console.WriteLine("Enter the student's address line 2: "); string add2 = Console.ReadLine(); Console.WriteLine("Enter the student's city: "); string city = Console.ReadLine(); Console.WriteLine("Enter the student's state: "); string state = Console.ReadLine(); Console.WriteLine("Enter the student's zip code: "); int zip = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Enter the student's country: "); string country = Console.ReadLine(); } static void PrintStudentDetails(string first, string last, string birthday) { Console.WriteLine("{0} {1} was born on: {2}", first, last, birthday); } static void Main(string[] args) { GetStudentInformation(); PrintStudentDetails(first, last, birthday); } public static string first { get; set; } public static string last { get; set; } public static string birthday { get; set; } } } ,我们将非常感谢您提供任何帮助或指导。

此致

3 个答案:

答案 0 :(得分:2)

如果您只想输入名字,姓氏和出生日期然后输出它们,您可以更改以下三行:

string firstName = Console.ReadLine();
string lastName = Console.ReadLine();
DateTime birthdate = Convert.ToDateTime(Console.ReadLine());

以下内容:

first = Console.ReadLine();
last = Console.ReadLine();
birthday = Convert.ToDateTime(Console.ReadLine());

那么你的程序应该按照你的预期运行。

但是既然你提到了outref,那么你真正想要的是以下内容:

class Program
{
    static void GetStudentInformation(out string first, out string last, out string birthday)
    {
        Console.WriteLine("Enter the student's first name: ");
        first = Console.ReadLine();
        Console.WriteLine("Enter the student's last name: ");
        last = Console.ReadLine();
        Console.WriteLine("Enter the student's birthdate: ");
        birthday = Convert.ToDateTime(Console.ReadLine());
    }
    static void PrintStudentDetails(string first, string last, string birthday)
    {
        Console.WriteLine("{0} {1} was born on: {2}", first, last, birthday);
    }
    static void Main(string[] args)
    {
        string first, last, birthday;
        GetStudentInformation(out first, out last, out birthday);
        PrintStudentDetails(first, last, birthday);
    }
}

答案 1 :(得分:1)

我不太清楚你的意思是什么,只有方法而不是类才能打印。但我想下面的例子可以帮助你。

void PrintDetails(ref string s1, ref string s2, ref string s3)
{ 
  s1 = "Some text goes here";
  s2 = myNumber.ToString();
  s3 = "some more text";
}

现在,在调用此函数时,只需使用以下语法:

public void SomeOtherFunction()
{
  string sMyValue1 = null;
  string sMyValue2 = null;
  string sMyValue3 = null;

  PrintDetails(sMyValue1, sMyValue2, sMyValue3);
  //Now you have all the values inside your local variable. 
}

答案 2 :(得分:0)

使用outref关键字可以帮助您从方法返回多个值,如果您真的希望它以这种方式执行,但使用属性可能是更好的解决方案。无论如何,这就是使用out关键字的样子:

static void GetStudentInformation(out string firstName, out string lastName, out DateTime birthDate,
    out string add1, out string add2, out string city, out string state, out int zip, out string country)
{
    Console.WriteLine("Enter the student's first name: ");
    firstName = Console.ReadLine();
    Console.WriteLine("Enter the student's last name: ");
    lastName = Console.ReadLine();
    Console.WriteLine("Enter the student's birthdate: ");
    birthDate = Convert.ToDateTime(Console.ReadLine());
    Console.WriteLine("Enter the student's address line 1: ");
    add1 = Console.ReadLine();
    Console.WriteLine("Enter the student's address line 2: ");
    add2 = Console.ReadLine();
    Console.WriteLine("Enter the student's city: ");
    city = Console.ReadLine();
    Console.WriteLine("Enter the student's state: ");
    state = Console.ReadLine();
    Console.WriteLine("Enter the student's zip code: ");
    zip = Convert.ToInt32(Console.ReadLine());
    Console.WriteLine("Enter the student's country: ");
    country = Console.ReadLine();
}
static void PrintStudentDetails(string first, string last, string birthday)
{
    Console.WriteLine("{0} {1} was born on: {2}", first, last, birthday);
}

static void Main(string[] args)
{
    string firstName, lastName, add1, add2, city, state, country;
    DateTime birthDate;
    int zip;

    GetStudentInformation(out firstName, out lastName, out birthDate, out add1, out add2, out city, out state, out zip, out country);
    PrintStudentDetails(firstName, lastName, birthDate.ToShortDateString());
}

您还可以使用ref关键字替换所有关键字,您将获得相同的效果。在将参数传递给GetStudentInformation方法之前,您只需要将变量初始化为某个值。