Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 存储在方法中的用户输入_C# - Fatal编程技术网

C# 存储在方法中的用户输入

C# 存储在方法中的用户输入,c#,C#,我在方法上有困难。因此,在一个方法中,我要求用户输入名字,我想将该字符串存储在另一个方法中,然后让我的主方法调用存储该字符串的方法。但是我在存储字符串时遇到了问题。我正在使用以下代码: static void GetStudentInformation() { Console.WriteLine("Enter the student's first name: "); string firstName = Console.ReadLine(); } static void Pr

我在方法上有困难。因此,在一个方法中,我要求用户输入名字,我想将该字符串存储在另一个方法中,然后让我的主方法调用存储该字符串的方法。但是我在存储字符串时遇到了问题。我正在使用以下代码:

static void GetStudentInformation()
{
    Console.WriteLine("Enter the student's first name: ");
    string firstName = Console.ReadLine();
}

static void PrintStudentDetails(string first)
{
    Console.WriteLine("{0}", first);
}

public static void Main (string[] args){
    GetStudentInformation();
}
因此,一旦他们输入名称,它就应该存储在details方法中,然后在main方法中调用。谢谢你提前帮助我

static string GetStudentInformation()
{
     Console.WriteLine("Enter the student's first name: ");
     return Console.ReadLine();
}

static void PrintStudentDetails(string first)
{
     Console.WriteLine("{0}", first);
}

public static void Main (string[] args)
{
     string firstName = GetStudentInformation();
     PrintStudentDetails(firstName);
}
如果您需要从用户检索多个输入。您可以返回字符串列表(快速解决方案):

如果您需要从用户检索多个输入。您可以返回字符串列表(快速解决方案):

Student.java

public class Student {

    private String studentName;

    public Student(String studentName) {
        this.name = studentName;
    }

    public static String getStudentName() {
        return studentName;
    }
    public static void setStudentName(String newName) {
        studentName = newName;
    }
然后你可以做:

Student aStudent = new Student(Console.ReadLine);
我知道这并不完全适用于您的特定情况,但它只是用来显示您想要做的事情背后的基本逻辑。

Student.java

public class Student {

    private String studentName;

    public Student(String studentName) {
        this.name = studentName;
    }

    public static String getStudentName() {
        return studentName;
    }
    public static void setStudentName(String newName) {
        studentName = newName;
    }
然后你可以做:

Student aStudent = new Student(Console.ReadLine);

我知道这并不完全适用于您的特定情况,但它只是用来显示您想要执行的操作背后的基本逻辑。

return Console.ReadLine();需要是sdtFirstName=Console.ReadLine(),因为我试图从用户那里获取其他字符串变量并存储它,然后调用PrintStudent方法来检索学生输入的所有数据。有什么办法可以做到这一点吗?thanks@user2447032:如果您需要从用户检索多个输入。您可以返回字符串列表或创建一个类来存储所有输入。我将在返回控制台上更新应答器。ReadLine();需要是sdtFirstName=Console.ReadLine(),因为我试图从用户那里获取其他字符串变量并存储它,然后调用PrintStudent方法来检索学生输入的所有数据。有什么办法可以做到这一点吗?thanks@user2447032:如果您需要从用户检索多个输入。您可以返回字符串列表或创建一个类来存储所有输入。我将更新答案。GetStudent还有多个字符串和出生日期,需要用户输入。因此它不能是静态字符串。此外,GetStudent有多个字符串和一个出生日期,需要用户输入。所以它不能是静态字符串。