[英]How do I use local variables when calling a method?
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);
}
}
}
How do I input these values in my method call? 如何在方法调用中输入这些值? When I run the program, the line comes up blank in the variable spots.
当我运行程序时,该行在可变位置空白。 I am trying to get the user input with the
getStudentInfo
method, and then store it in the variables, and input it into the printStudentInfo
method to format it and write it to the console. 我正在尝试使用
getStudentInfo
方法获取用户输入,然后将其存储在变量中,并将其输入到printStudentInfo
方法中以对其进行格式设置并将其写入控制台。
This code shouldn't compile and run at all. 这段代码根本不应该编译和运行。 You don't have a firstName, lastName, or birthday variable in scope.
您在范围内没有firstName,lastName或Birthday变量。 What editor are you using to write this in?
您使用什么编辑器编写此内容?
If you want to keep the variables then declare them outside of the methods and assign them the same way but without the 'string' modifier. 如果要保留变量,则在方法之外声明它们,并以相同的方式分配它们,但不带'string'修饰符。 Like this...
像这样...
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);
}
}
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);
}
}
Make this changes you should be able to get what you want. 进行此更改,您应该能够获得所需的内容。
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();
}
}
}
Create static properties to hold the value and use it any method you are calling in the Main() method.Note that static properties are created under program class.Read about properties from here C# Properties 静态属性程序class.Read下创建距离这里大约属性创建静态属性来保存的价值和使用它,你在main()method.Note呼吁任何方法C#属性
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.