userInput
已分配,所以我不知道爲什麼會出現錯誤。我已經瀏覽了類似的問題,用Google搜索了一下,沒有找到我想要的東西。 ps的新的編程在分配的變量上獲取未分配的變量錯誤
static void Main(string[] args)
{
//declare variables
int hours, minutes, seconds, remainder, userInput;
const int SECONDS_PER_MINUTE = 60;
const int SECONDS_PER_HOUR = 3600;
userInput = getUserInput(userInput); //error
hours = CalculateHours(userInput, SECONDS_PER_HOUR);
remainder = CalculateRemainder(userInput, SECONDS_PER_HOUR);
minutes = CalculateMinutes(remainder, SECONDS_PER_MINUTE);
seconds = CalculateSeconds(remainder, SECONDS_PER_MINUTE);
DisplayResults(hours, minutes, remainder, userInput, seconds);
}
//prompt the user to enter number of seconds
public static int getUserInput(int userInput)
{
Console.WriteLine("How about you enter a number of " +
" seconds and Ill see what I can do for you. ");
return userInput = int.Parse(Console.ReadLine());
}
//this method takes in userinput and converts to hours if necessary
//userInput/SECONDS_PER_HOUR will be returned to CalculateHours
//in main and stored in the hours variable. same goes for the rest
//except DisplayResults
public static int CalculateHours(int userInput, int SECONDS_PER_HOUR)
{
return userInput/SECONDS_PER_HOUR;
}
//this method takes userinput and gives a remainder to be used for minutes and seconds
public static int CalculateRemainder(int userInput, int SECONDS_PER_HOUR)
{
return userInput % SECONDS_PER_HOUR;
}
public static int CalculateMinutes(int remainder, int SECONDS_PER_MINUTE)
{
return remainder/SECONDS_PER_MINUTE;
}
public static int CalculateSeconds(int remainder, int SECONDS_PER_MINUTE)
{
return remainder % SECONDS_PER_MINUTE;
}
新的張貼在這裏。由於某種原因,我的代碼似乎被切斷了。 – MilesLong 2014-10-17 15:37:53
您將'userInput'作爲參數傳遞給getUserInput方法。在通話時,它是未分配的。它不需要是一個參數,所以將它從方法和調用中刪除。 – 2014-10-17 15:38:04
它告訴你在使用時沒有分配給哪個對象? – deathismyfriend 2014-10-17 15:38:48