我很努力地理解函數如何在main之外工作。我只需要計算用戶輸入的主要信息的總數,但是我應該調用一個函數來總和這個信息。這裏是我到目前爲止的代碼,我相信它是不是很接近正確,在正確的方向輕推將是一個巨大的幫助在c中使用函數#
namespace ConsoleApplication17
{
class Program
{
static void Main(string[] args)
{
string customerName, customerState;
double numberItems, itemPrice;
Console.WriteLine("Please enter the customer name");
customerName = Console.ReadLine();
Console.WriteLine("Please enter the state in which you reside:");
customerState = Console.ReadLine();
Console.WriteLine("How many items were purchased?");
numberItems = int.Parse(Console.ReadLine());
Console.WriteLine("What was the price of said items?");
itemPrice = Convert.ToDouble(Console.ReadLine());
}
public static double ComputeTotal (double total)
{
double totalAmount;
Console.Write(total);
}
}
public static double ComputeTax (double totalAmount, string state, string fl, string nj, string ny)
{
if (state == fl)
return totalAmount*.06;
else if (state == nj)
return totalAmount*.07;
else if (state == ny)
return totalAmount*.04;
}
總之,我需要使用函數ComputeTotal來乘以numberItems和itemPrice
爲什麼不直接調用'ComputeTotal()'? – Houseman 2015-02-24 02:34:36
我首先將方法的數量和價格傳遞給方法,進行計算並返回結果。 – 2015-02-24 02:34:50
@GrantWinney所以這裏是我想說的話,不要太羅嗦。這是爲了上課,我的老師很快就過去了,我不知道如何將這些傳遞給方法。我一直在看例子,但沒有像樣的解釋,僅靠我自己就很難。 – Wardy24 2015-02-24 02:36:40