【问题标题】:Error: CS1503 Argument 1 and Error:CS0019 '=='错误:CS1503 参数 1 和错误:CS0019 '=='
【发布时间】:2016-09-17 05:46:26
【问题描述】:

我们目前正在学习一个类中的函数、参数、参数,所以我想保持相同的格式,因为“十进制价格”和“十进制金额”将被输入到函数定义中

我尝试了不同的方法将“小数”转换为“字符串”,以防止用户输入字符而不是小数,但我不知道为什么它不起作用

由于“金额”是decimal,因此需要将其转换为string 才能运行(amount == string.Empty),但作为第一个问题,我无法弄清楚。

        decimal price; 
        decimal amount; 



        Console.Write("What is the price?"); 
        price = decimal.Parse(Console.ReadLine()); 


        double pricenumber;

        while (!double.TryParse(price, out pricenumber)) //error here 
        {
            Console.WriteLine("You've not entered a price.\r\nPlease enter a price");
            price = decimal.Parse(Console.ReadLine());

        }

        Console.Write("How many were you planning on purchasing?"); 

        amount = decimal.Parse(Console.ReadLine()); 

        while (amount == string.Empty) //error here
        {
            Console.WriteLine("You cannot leave this blank.\r\nPlease enter how many are needed:");
            amount = decimal.Parse(Console.ReadLine());
        }

【问题讨论】:

标签: c# string arguments decimal


【解决方案1】:

您的代码中存在一些逻辑缺陷,您必须加以修复。请看cmets:

Console.Write("What is the price?"); 
price = decimal.Parse(Console.ReadLine()); // you're already parsing the user-input into
// a decimal. This is somewhat problematic, because if the user enters "foo" instead 
// of "123" the attempt to parse the input will fail

double pricenumber;
while (!double.TryParse(price, out pricenumber)) // the variable "price" already contains
// a parsed decimal. That's what you did some lines above. "TryParse" expects a string to
// be parsed whereas you're committing the parsed decimal
{
    Console.WriteLine("You've not entered a price.\r\nPlease enter a price");
    price = decimal.Parse(Console.ReadLine());
}

所以你应该做的是将用户输入保持为字符串,直到你尝试解析它:

Console.Write("What is the price?"); 
string input = Console.ReadLine(); // keep as string

double pricenumber;
while (!decimal.TryParse(input, out pricenumber))
{
    Console.WriteLine("You've not entered a price.\r\nPlease enter a price");
    input = Console.ReadLine();
}

您的其他尝试也是如此。再次,请查看 cmets:

Console.Write("How many were you planning on purchasing?"); 
amount = decimal.Parse(Console.ReadLine()); // you're already parsing the string into a
// decimal

while (amount == string.Empty) // as you can't compare a decimal with a string, this 
// creates an error
{
    Console.WriteLine("You cannot leave this blank.\r\nPlease enter how many are needed:");
    amount = decimal.Parse(Console.ReadLine());
}

你可以用上面的方法解决它:

Console.Write("How many were you planning on purchasing?"); 
input = Console.ReadLine(); // again, keep as string

while (!decimal.TryParse(input, out amount))
{
    Console.WriteLine("You cannot leave this blank.\r\nPlease enter how many are needed:");
    input = Console.ReadLine();
}

如果还有进一步优化的空间,您可以并且应该将该逻辑放入单独的方法中,因为代码几乎相同并且会导致重复。

private static decimal GetParsedInput(string question, string noticeOnFailed)
{
    Console.Write(question); 
    input = Console.ReadLine();

    decimal result;
    while (!decimal.TryParse(input, out result))
    {
        Console.WriteLine(questionOnFailed);
        input = Console.ReadLine();
    }

    return result;
}

用法:

decimal price = GetParsedInput(
    "What is the price?", 
    "You've not entered a price.\r\nPlease enter a price:");
decimal amount = GetParsedInput(
    "How many were you planning on purchasing?", 
    "You cannot leave this blank.\r\nPlease enter how many are needed:");

【讨论】:

    【解决方案2】:

    试试这个:

    string price;
            string amount;
    
    
    
            Console.Write("What is the price?");
            price = Console.ReadLine();
    
    
            double pricenumber;
    
            while (!double.TryParse(price, out pricenumber)) 
            {
                Console.WriteLine("You've not entered a price.\r\nPlease enter a price");
                price = Console.ReadLine();
    
            }
    
            Console.Write("How many were you planning on purchasing?");
    
            double amountnumber;
            amount = Console.ReadLine();
    
            while (!double.TryParse(amount, out amountnumber)) 
            {
                Console.WriteLine("You cannot leave this blank.\r\nPlease enter how many are needed:");
                amount = Console.ReadLine();
            }
    

    【讨论】:

      【解决方案3】:

      我想通了

      我已经这样做了

      { public static decimal questions(string question)
          {
              Console.Write(question);
              string answer = Console.ReadLine();
             
              decimal result;
              while (!decimal.TryParse(answer, out result))
              {
                  Console.WriteLine(question);
                  answer = Console.ReadLine();
              }
              return result;
      }
      

      用法:

       string productcost = "How much does one " + productname + " cost? ";
       questions(productcost);
      

      完美运行

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-28
        相关资源
        最近更新 更多