2013-03-15 147 views
1

我需要幫助檢查是否輸入了負值,但每次進行更改時都會收到錯誤。我知道我需要另一個do/while循環,但是每當我把一個amount <= 0寫入,編譯器就不會讀取amount。這是我工作的應用程序的代碼:如何檢查是否輸入負數

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      double cur1 = 0.85972; 
      double cur2 = 1.16316; 
      double cur3 = 1.30246; 
      double cur4 = 0.76777; 
      double cur5 = 0.66010; 
      double cur6 = 1.51409; 
      double amount; 
      double total; 

      int input = 1; 

      Console.WriteLine("1. Euro to Sterling"); 
      Console.WriteLine("2. Sterling to Euro"); 
      Console.WriteLine("3. Euro to Dollar"); 
      Console.WriteLine("4. Dollar to Euro"); 
      Console.WriteLine("5. Dollar to Sterling"); 
      Console.WriteLine("6. Sterling to Dollar"); 
      Console.WriteLine("7. Exit"); 


      do 
      { 

       if (input == 0 || input > 8) 
        Console.WriteLine("You have entered an invalid Option please choose again " + input + " is not allowed"); 

       Console.WriteLine("Please Enter your selection 1-6 and 7 to quit:"); 
       input = int.Parse(Console.ReadLine()); 


       if (input == 1) 
       { 
        Console.WriteLine("Please enter the amount in Euro you want converted into Sterling"); 
        amount = double.Parse(Console.ReadLine()); 
        total = amount * cur1; 
        Console.WriteLine("The amount of {0} Euros in Sterling at exchange rate {1} is = {2}", amount, cur1, total); 
        Console.WriteLine("Press return to go back to the Menu"); 
        Console.ReadKey(); 
       } 

       else if (input == 2) 
       { 
        Console.WriteLine("Please enter the amount in Sterling you want converted into Euro"); 
        amount = double.Parse(Console.ReadLine()); 
        total = amount * cur2; 
        Console.WriteLine("The amount of {0} Sterling in Euros at exchange rate {1} is = {2}", amount, cur2, total); 
        Console.WriteLine("Press return to go back to the Menu"); 
        Console.ReadKey(); 
       } 

       else if (input == 3) 
       { 
        Console.WriteLine("Please enter the amount in Euro you want converted into Dollar"); 
        amount = double.Parse(Console.ReadLine()); 
        total = amount * cur3; 
        Console.WriteLine("The amount of {0} Euros in Dollars at exchange rate {1} is = {2}", amount, cur3, total); 
        Console.WriteLine("Press return to go back to the Menu"); 
        Console.ReadKey(); 
       } 

       else if (input == 4) 
       { 
        Console.WriteLine("Please enter the amount in Dollar you want converted into Euro"); 
        amount = double.Parse(Console.ReadLine()); 
        total = amount * cur4; 
        Console.WriteLine("The amount of {0} Dollars in Euro at exchange rate {1} is = {2}", amount, cur4, total); 
        Console.WriteLine("Press return to go back to the Menu"); 
        Console.ReadKey(); 
       } 

       else if (input == 5) 
       { 
        Console.WriteLine("Please enter the amount in Dollar you want converted into Sterling"); 
        amount = double.Parse(Console.ReadLine()); 
        total = amount * cur5; 
        Console.WriteLine("The amount of {0} Dollars in Sterling at exhange rate {1} is = {2}", amount, cur5, total); 
        Console.WriteLine("Press return to go back to the Menu"); 
        Console.ReadKey(); 
       } 

       else if (input == 6) 
       { 
        Console.WriteLine("Please enter the amount in Sterling you want converted into Dollar"); 

amount = double.Parse(Console.ReadLine()); 
        total = amount * cur6; 

Console.WriteLine("The amount of {0} Sterling in Dollars at exhange rate {1} is = {2}", amount, cur6, total); 
        Console.WriteLine("Press return to go back to the Menu"); 
        Console.ReadKey(); 
       } 

      } while (input != 7); 

     } 
    } 
} 
+0

歡迎來到Stackoverflow!請花時間將所有這些代碼縮減爲最基本的要素以重現您的問題。我懷疑有人會想通讀這一點。誰知道,也許這會讓你發現問題。 – MPelletier 2013-03-15 02:06:17

+1

你是什麼意思編譯器不會讀'數量'? – 2013-03-15 02:06:34

+0

_爲什麼你需要這樣做。負值的轉換仍然有效。如果用戶輸入-1美元並返回-1.03澳元,那麼問題是什麼? – paxdiablo 2013-03-15 02:13:48

回答

0

我認爲你正在試圖比較雙和int

1

你需要比較針對0.0,不是0

do 
{ 
    Console.WriteLine("Please enter the amount in Euro you want converted into Sterling"); 
    amount = double.Parse(Console.ReadLine()); 
} while (amount <= 0.0); 
total = amount * cur1; 
Console.WriteLine("The amount of {0} Euros in Sterling at exchange rate {1} is = {2}", amount, cur1, total); 
Console.WriteLine("Press return to go back to the Menu"); 
Console.ReadKey(); 

雖然你可以將這個檢查抽象爲一個方法來清理和減少你的代碼。