2016-04-10 19 views
0
double UserInput; 
string y, z; 
double OunceToGram, PoundToOunce, PoundToKilogram, PintToLitre, InchToCenti, MilesToInch; 


OunceToGram = UserInput * 28.0; //(error is here, it cannot find UserInput) 
PoundToOunce = UserInput * 16.0; 
PoundToKilogram = UserInput * 0.454; 
PintToLitre = UserInput * 0.568; 
InchToCenti = UserInput * 2.5; 
MilesToInch = UserInput * 63360.0; 

int i = 0; 

while (i < UserInput) 
{ 
    Console.WriteLine(""); 
    Console.WriteLine("Please enter a unit to convert, type a num <1 to cancel"); 
    UserInput = Convert.ToDouble(Console.ReadLine()); 
+0

你能請顯示UserInput分配到的代碼? –

+1

你確定這是錯誤信息嗎?相反,從上面的代碼,我認爲你應該'使用未分配的局部變量....'或類似 – Steve

+0

初始化userinput之前試圖使用它 – nhouser9

回答

2

你可以解決你的問題沒有立即的用戶輸入轉換成你的變量UserInput,如果用戶鍵入檢查常規信件停止輸入

double UserInput = 0.0; // <- You need to initialize before using it ... 
..... 

string stopInput = "N"; 
while (stopInput != "Q")) 
{ 
    Console.WriteLine(""); 
    Console.WriteLine("Please enter a unit to convert, type 'Q' to cancel"); 
    stopInput = Console.ReadLine(); 
    if(stopInput == "Q") 
     break; 
    UserInput = Convert.ToDouble(stopInput); 
    .... 
} 
+0

史蒂夫這個工程,我用<1來取代而不是一個字母,但問題仍然是,如果我把UserInput初始化爲0.0,那麼它將把它帶入等式而不是用戶輸入的內容。 –