I'm new to C# and having problems passing input data from the static Main method to another method command-line public static. Below is the code in visual studio 2019. I have the input data and the code should return the calcPay value but doesn't. It only enters the value declared in the initialization of the variable.
namespace CalcGrossPay
{
class Salary
{
static void Main()
{
double myHours = 0;
double myRate = 0;
double grossPay;
grossPay = CalcPay(myHours, myRate);
Console.WriteLine("Enter total hours worked: ");
myHours = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Enter rate per hour: ");
myRate = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Number of hours worked is {0} at {1} per hour", myHours, myRate);
Console.WriteLine("My gross pay is\n {0:F2}", grossPay.ToString("c"));
}
public static double CalcPay(double hours, double rate)
{
double pay;
// If hours>40
// Get gross pay by calculating overtime
if (hours > 40)
{
pay = 40 * rate + (1.5 * rate * (hours - 40));
}
else
{
pay = hours * rate;
}
return pay;
}
}
}```
The CalcPay
method is only call once and that is when the variables are set to 0 Change the methode like this
namespace CalcGrossPay
{
class Salary
{
static void Main()
{
double myHours = 0;
double myRate = 0;
double grossPay;
Console.WriteLine("Enter total hours worked: ");
myHours = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Enter rate per hour: ");
myRate = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Number of hours worked is {0} at {1} per hour", myHours, myRate);
grossPay = CalcPay(myHours, myRate);
Console.WriteLine("My gross pay is\n {0:F2}", grossPay.ToString("c"));
}
public static double CalcPay(double hours, double rate)
{
double pay;
// If hours>40
// Get gross pay by calculating overtime
if (hours > 40)
{
pay = 40 * rate + (1.5 * rate * (hours - 40));
}
else
{
pay = hours * rate;
}
return pay;
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.