我在哪里可以存储信息以跨不同的功能使用
Where can I store information to use across different functions
我知道这很傻,但我是初学者,我被要求制作一个程序,从方法 GetStudentInfo()
接受学生信息,并使用另一种方法将其打印出来 PrintStudentInfo
,除了不知道在哪里可以声明变量外,我几乎完成了所有工作。我得到了名字 'XXXX' doesn't exist in the current context.
,这是代码:
class Program
{
static void Main(string[] args)
{
GetStudentInfo();
printStudentInfo();
}
//Student info ..
public static void GetStudentInfo()
{
Console.WriteLine("Enter Student's first name : ");
firstname = Console.ReadLine();
Console.WriteLine("Enter Student's last name : ");
lastname = Console.ReadLine();
Console.WriteLine("Enter Student's Address : ");
address = Console.ReadLine();
Console.WriteLine("Enter Student's country :");
country = Console.ReadLine();
Console.WriteLine("Enter Student's state : ");
state = Console.ReadLine();
Console.WriteLine("Enter Student's Telephone number :");
tele = Console.ReadLine();
}
static void printStudentInfo()
{
Console.WriteLine("Student name is" + firstname + lastname);
Console.WriteLine("Prof address is " + address);
Console.WriteLine("Student country/state is" + country + state);
Console.WriteLine("Student telephone is " + tele);
}
我建议:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
public class Student
{
public string firstname;
public string lastname;
public string address;
public string country;
public string state;
public string tele;
}
public class StudentProcessor
{
//Student info ..
public Student GetStudentInfo()
{
Student s = new Student();
Console.WriteLine("Enter Student's first name : ");
s.firstname = Console.ReadLine();
Console.WriteLine("Enter Student's last name : ");
s.lastname = Console.ReadLine();
Console.WriteLine("Enter Student's Address : ");
s.address = Console.ReadLine();
Console.WriteLine("Enter Student's country :");
s.country = Console.ReadLine();
Console.WriteLine("Enter Student's state : ");
s.state = Console.ReadLine();
Console.WriteLine("Enter Student's Telephone number :");
s.tele = Console.ReadLine();
return s;
}
public void printStudentInfo(Student s)
{
Console.WriteLine("Student name is" + s.firstname + s.lastname);
Console.WriteLine("Prof address is " + s.address);
Console.WriteLine("Student country/state is" + s.country + s.state);
Console.WriteLine("Student telephone is " + s.tele);
}
}
static void Main(string[] args)
{
StudentProcessor p = new StudentProcessor();
Student s = p.GetStudentInfo();
p.printStudentInfo(s);
}
}
}
放:
private static string firstname;
private static string lastname;
private static string address;
private static string country;
private static string state;
private static string tele;
在class(主要方法的)主体中,如果你只是想让它工作,但如果你想这样做,最好像 Ewan 说的那样去做。
例如:
class Program
{
private static string firstname;
private static string lastname;
private static string address;
private static string country;
private static string state;
private static string tele;
static void Main(string[] args)
{
....
}
....
}
我知道这很傻,但我是初学者,我被要求制作一个程序,从方法 GetStudentInfo()
接受学生信息,并使用另一种方法将其打印出来 PrintStudentInfo
,除了不知道在哪里可以声明变量外,我几乎完成了所有工作。我得到了名字 'XXXX' doesn't exist in the current context.
,这是代码:
class Program
{
static void Main(string[] args)
{
GetStudentInfo();
printStudentInfo();
}
//Student info ..
public static void GetStudentInfo()
{
Console.WriteLine("Enter Student's first name : ");
firstname = Console.ReadLine();
Console.WriteLine("Enter Student's last name : ");
lastname = Console.ReadLine();
Console.WriteLine("Enter Student's Address : ");
address = Console.ReadLine();
Console.WriteLine("Enter Student's country :");
country = Console.ReadLine();
Console.WriteLine("Enter Student's state : ");
state = Console.ReadLine();
Console.WriteLine("Enter Student's Telephone number :");
tele = Console.ReadLine();
}
static void printStudentInfo()
{
Console.WriteLine("Student name is" + firstname + lastname);
Console.WriteLine("Prof address is " + address);
Console.WriteLine("Student country/state is" + country + state);
Console.WriteLine("Student telephone is " + tele);
}
我建议:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
public class Student
{
public string firstname;
public string lastname;
public string address;
public string country;
public string state;
public string tele;
}
public class StudentProcessor
{
//Student info ..
public Student GetStudentInfo()
{
Student s = new Student();
Console.WriteLine("Enter Student's first name : ");
s.firstname = Console.ReadLine();
Console.WriteLine("Enter Student's last name : ");
s.lastname = Console.ReadLine();
Console.WriteLine("Enter Student's Address : ");
s.address = Console.ReadLine();
Console.WriteLine("Enter Student's country :");
s.country = Console.ReadLine();
Console.WriteLine("Enter Student's state : ");
s.state = Console.ReadLine();
Console.WriteLine("Enter Student's Telephone number :");
s.tele = Console.ReadLine();
return s;
}
public void printStudentInfo(Student s)
{
Console.WriteLine("Student name is" + s.firstname + s.lastname);
Console.WriteLine("Prof address is " + s.address);
Console.WriteLine("Student country/state is" + s.country + s.state);
Console.WriteLine("Student telephone is " + s.tele);
}
}
static void Main(string[] args)
{
StudentProcessor p = new StudentProcessor();
Student s = p.GetStudentInfo();
p.printStudentInfo(s);
}
}
}
放:
private static string firstname;
private static string lastname;
private static string address;
private static string country;
private static string state;
private static string tele;
在class(主要方法的)主体中,如果你只是想让它工作,但如果你想这样做,最好像 Ewan 说的那样去做。
例如:
class Program
{
private static string firstname;
private static string lastname;
private static string address;
private static string country;
private static string state;
private static string tele;
static void Main(string[] args)
{
....
}
....
}