我知道这很愚蠢,但我是一个初学者,我被要求制作一个程序,该程序从方法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);

        }

    }
}

关于c# - 我在哪里可以存储信息以用于不同功能,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29608879/

10-08 21:43