C# 调用方法时如何使用局部变量?
如何在方法调用中输入这些值?当我运行该程序时,该行在可变点处变为空白。我正在尝试使用C# 调用方法时如何使用局部变量?,c#,C#,如何在方法调用中输入这些值?当我运行该程序时,该行在可变点处变为空白。我正在尝试使用getStudentInfo方法获取用户输入,然后将其存储在变量中,然后将其输入printsudentinfo方法以格式化并将其写入控制台。此代码不应编译和运行。范围中没有firstName、lastName或birthday变量。你用什么编辑器来写这个 如果希望保留变量,则在方法之外声明它们,并以相同的方式分配它们,但不使用“string”修饰符。像这样 using System; using System.C
getStudentInfo
方法获取用户输入,然后将其存储在变量中,然后将其输入printsudentinfo
方法以格式化并将其写入控制台。此代码不应编译和运行。范围中没有firstName、lastName或birthday变量。你用什么编辑器来写这个
如果希望保留变量,则在方法之外声明它们,并以相同的方式分配它们,但不使用“string”修饰符。像这样
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MethodsExceptions2
{
class Program
{
static void Main(string[] args)
{
GetStudentInformation();
PrintStudentDetails(firstName, lastName,birthDay);
Console.ReadKey();
}
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 birthday");
string birthDay = Console.ReadLine();
}
static void PrintStudentDetails(string first, string last, string birthday)
{
Console.WriteLine("{0} {1} was born on: {2}", first, last, birthday);
}
}
}
做出这些改变,你应该能够得到你想要的
class Program
{
static string firstName;
static string lastName;
static string birthday;
static void Main(string[] args)
{
GetStudentInformation();
PrintStudentDetails(firstName, lastName, birthday);
Console.ReadKey();
}
static void GetStudentInformation()
{
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 birthday");
birthday = Console.ReadLine();
}
static void PrintStudentDetails(string first, string last, string birthday)
{
Console.WriteLine("{0} {1} was born on: {2}", first, last, birthday);
}
}
创建静态属性以保存该值,并将其用于Main()方法中调用的任何方法。请注意,静态属性是在程序类下创建的。请阅读此处的属性
{
静态void Main(字符串[]参数)
{
var userInputs=GetStudentInformation();
PrintStudentDetails(用户输入);
Console.ReadKey();
}
静态元组GetStudentInformation()
{
Console.WriteLine(“输入学生的名字:”);
string firstName=Console.ReadLine();
Console.WriteLine(“输入学生的姓氏”);
字符串lastName=Console.ReadLine();
Console.WriteLine(“输入学生的生日”);
字符串生日=Console.ReadLine();
返回新元组(firstName、lastName、birthDay);
}
静态void PrintStudentDetails(元组用户输入)
{
WriteLine(“{0}{1}出生于:{2}”、userInputs.Item1、userInputs.Item2、userInputs.Item3);
}
}
您发布的代码甚至无法编译,因此我不知道您所说的“当我运行程序时,该行出现空白”是什么意思。如何运行不编译的程序?最有可能的是,你的问题是“我不知道局部变量和类变量之间的区别”形式的成百上千个现有问题的翻版,但不可能确定,因为您的代码示例没有做到您所说的。您应该真正为类似的内容创建一个新类型,而不是使用
Tuple
。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MethodsExceptions2
{
class Program
{
public static string firstName { get; set; }
public static string lastName { get; set; }
public static string birthDay { get; set; }
static void Main(string[] args)
{
GetStudentInformation();
PrintStudentDetails(firstName, lastName, birthDay);
Console.ReadKey();
}
private static void PrintStudentDetails(string firstName, object lastName, object birthDay)
{
Console.WriteLine("{0} {1} was born on: {2}", firstName, lastName, birthDay);
}
private static void GetStudentInformation()
{
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 birthday");
birthDay = Console.ReadLine();
}
}
}
class Program
{
static void Main(string[] args)
{
var userInputs = GetStudentInformation();
PrintStudentDetails(userInputs);
Console.ReadKey();
}
static Tuple<string, string, string> 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 birthday");
string birthDay = Console.ReadLine();
return new Tuple<string, string, string>(firstName, lastName, birthDay);
}
static void PrintStudentDetails(Tuple<string, string, string> userInputs)
{
Console.WriteLine("{0} {1} was born on: {2}", userInputs.Item1, userInputs.Item2, userInputs.Item3);
}
}