PromptAndInput,无法将字符串转换为双

时间:2015-04-26 00:21:54

标签: c# module implicit-conversion

我在编译程序时遇到问题。错误指向了PromptAndInput("任何文本");不能隐式地将字符串转换为double

    name = PromptAndInput( "Enter the name of the customer: ");
    state = PromptAndInput( "In which state (NY / NJ / FL) ? ");

    if (state != "NJ" & state != "nj" & state != "NY" & state != "ny" & state != "FL" & state != "fl") {
        Console.WriteLine("Error");
        while (state != "NJ" & state != "nj" & state != "NY" & state != "ny" & state != "FL" & state != "fl") {
            state = PromptAndInput("In which state (NY / NJ / FL) ? ");
        }
    }

    itemQuantity = PromptAndInput("How many items were purchased?: ");

    if (itemQuantity < 0) {
        Console.WriteLine("Error");
        while (itemQuantity < 0) {
            itemQuantity = PromptAndInput("How many items were purchased?: ");
        }
    }

    itemPrice = PromptAndInput("What was the unit price of the items?: ");

    if (itemPrice < 0) {
        Console.WriteLine("Error");
        while (itemPrice < 0) {
            itemPrice = PromptAndInput("What was the unit price of the items?: ");
        }
    }

    computeTotal(itemQuantity, itemPrice, total, name);
    computeTax(state, total, itemQuantity, itemPrice);

我确定很容易发现,但由于我正在处理模块,因此我不确定如何使用PromptAndInput

我能在这做什么?

第一次修复后

itemQuantity = double.Parse(PromptAndInput(&#34;购买了多少件物品?:&#34;));

        if (itemQuantity < 0)
        {
            Console.WriteLine("Error");
            while (itemQuantity < 0)
            {
                itemQuantity = double.Parse(PromptAndInput("How many items were purchased?: "));
            }
        }

        itemPrice = int.Parse(PromptAndInput("What was the unit price of the items?: "));

        if (itemPrice < 0)
        {
            Console.WriteLine("Error");
            while (itemPrice < 0)
            {
                itemPrice = int.Parse(PromptAndInput("What was the unit price of the items?: "));



                computeTotal(itemQuantity, itemPrice, total, name);
                computeTax(state, total, itemQuantity, itemPrice);

            }
        }
    }

//function for read and write
public static string PromptAndInput(string prompt)
{
    string userInput = null;
    Console.Write(prompt);
    userInput = Console.ReadLine();
    return userInput;
}

//function for total sales
public static object computeTotal(double itemQuantity, int itemPrice, double total, string name)
{

    total = itemQuantity * itemPrice;
    Console.WriteLine();
    Console.WriteLine("--------------------------------------------------");
    Console.WriteLine("The total sales for " + name + " are: " + total);

    return total;
}

代码假设在

之后继续
itemPrice = int.Parse(PromptAndInput("What was the unit price of the items?: "));

不确定导致结束的原因

1 个答案:

答案 0 :(得分:0)

PromptAndInput的回报为String

public static string PromptAndInput(string prompt)

因此,在您调用此代码的代码中,您需要解析返回的文本:

itemQuantity = Int32.Parse(PromptAndInput("How many items were purchased?: "));

itemPrice = Double.Parse(PromptAndInput("What was the unit price of the items?: "));