2017-09-26 14 views
-1

問題出現在第34行: 不能隱式地將類型'double'轉換爲'int'。存在明確的轉換(您是否缺少演員?)不知道如何轉換這個..或者什麼是雙...新程序員

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

namespace ConsoleApp1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
    { 
     string FirstName; 
     string LastName; 
     int Hours = 0; 
     int OvertimeHours = 0; 
     int HourlyPay; 
     int GrossPay; 

     //float AveragePay; 

     Console.WriteLine("Please enter your first name!"); 
     FirstName = Console.ReadLine(); 
     Console.WriteLine("Please enter your last name!"); 
     LastName = Console.ReadLine(); 
     Console.WriteLine("How many hours have you worked this week?"); 
     Hours = Int32.Parse(Console.ReadLine()); 
     Console.WriteLine("How many overtime hours did you work this week?"); 
     OvertimeHours = Int32.Parse(Console.ReadLine()); 
     Console.WriteLine("What is your hourly pay ?"); 
     HourlyPay = Int32.Parse(Console.ReadLine()); 

     if (Hours > 40) 
      GrossPay = (OvertimeHours * HourlyPay *1.5) + (Hours * HourlyPay); 
     else 
      GrossPay = (Hours * HourlyPay); 

      Console.ReadLine(); 
    } 
} 
} 

我不知道現在該做什麼,以及什麼是雙。我仍然是一個非常新的程序員,只有幾個星期的經驗,任何和所有的幫助將不勝感激,謝謝。

+3

歡迎來到堆棧溢出。恐怕這不是教程網站。您可以使用Google找到許多教程,並且有許多書籍可以在[Amazon](http://www.amazon.com)上找到。或者你可以問老師給你分配的幫助;他們正在獲得報酬,爲您提供完成所需的內容。 –

+0

本例中的'double'來自'1.5'。當你乘以小數時,它假定你使用的是雙精度。嘗試將「GrossPay」加倍。 –

+2

您通過Google找到了我認爲,爲什麼不以同樣的方式搜索'double'? – MickyD

回答

0

底線是,當你做這樣的

if (Hours > 40) 
    GrossPay = (OvertimeHours * HourlyPay *1.5) + (Hours * HourlyPay); 
else 
    GrossPay = (Hours * HourlyPay); 

計算它的結果是幾乎從未int。您需要像這樣轉換

GrossPay = Convert.ToInt32((OvertimeHours * HourlyPay *1.5) + (Hours * HourlyPay)); 

Bau然後記住它隨附的圓角。可能是int對於這種情況是錯誤的數據類型

+1

如果你認爲你會失去精度,然後設置GrossPay爲雙。 –

+0

是的,當你處理金錢時,不要將'double'轉換爲'int'。 4.99美元變成4美元。事實上,在處理貨幣時,您應該使用「decimal」類型。 –

+0

@RufusL你是對的。答案中提到'int'在這種情況下不是正確的類型。但OP最需要理解的是,這樣的數學運算很少返回「int」,因此OP無法設置變量 –

1

帶小數點的數字被認爲是雙倍數。您將GrossPay聲明爲int。當你* 1.5時,數字變成十進制數字。因此,你面對這個錯誤。嘗試更改int GrossPaydouble GrossPay。另一個提示是,你可以使用double而不是int作爲Hours/HourlyPay,因爲小時可以是'1.5小時',HourlyPay可以是'$ 15.50/hr'。希望這可以幫助你。

做徹底的谷歌,因爲所有這些答案肯定可以在谷歌和教程:)

+0

非常感謝,它比我想象的要簡單得多! – Boogy

+0

沒問題,在編程中最好! :) – 2017-09-26 03:06:09

+0

@GhostCat嗨哈哈,因爲這個問題太廣泛,無法回答。無論如何,非常感謝你的信息! :) – 2017-10-05 09:38:14

相關問題