2016-10-12 39 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

相關問題