2016-10-12 41 views
0
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); 
     } 
    } 
} 

如何在我的方法调用中输入这些值?当我运行该程序时,该线在变量点中空白。我试图用getStudentInfo方法获取用户输入,然后将其存储在变量中,并将其输入到printStudentInfo方法中,以格式化并将其写入控制台。如何在调用方法时使用局部变量?

+1

你发布的代码甚至无法编译,所以我不知道你的意思是什么_“当我运行该程序时,该行出现空白_”。你如何运行一个不能编译的程序?最有可能的是,你的问题是几百甚至上千个“我不知道局部变量和类变量之间的区别”的现有问题的重复,但是不可能确定地知道,因为你的代码例子不会做你说的那样。 –

回答

0

这段代码不应该编译和运行。您的范围内没有名字,姓氏或生日变量。你用什么编辑器来编写它?

如果你想保留这些变量,然后在方法之外声明它们,并以相同的方式分配它们,但没有'string'修饰符。像这样...

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); 
    } 
} 
-1

进行此更改,您应该能够得到您想要的。

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(); 

    } 



} 
} 

创建静态属性来保存的价值和使用它,你在主呼吁任何方法()method.Note静态属性程序class.Read下从这里C# Properties

0
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); 
    } 
} 
创造了约特性
+1

你应该真的在为这样的事情创建一个新类型,而不是使用'Tuple'。 – Servy

相关问题