【问题标题】:Operator '<' cannot be applied to operands of type 'decimal' and 'double'运算符“<”不能应用于“十进制”和“双精度”类型的操作数
【发布时间】:2016-03-08 03:06:09
【问题描述】:

我正在尝试开发一个程序来计算用户输入的分数。我还试图对用户输入的高低设置一个限制(即 0 = 100)。但是当我使用十进制时,它一直给我这个错误,“运算符'

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

namespace Grade_Program
{
    class Program
    {
        static void Main(string[] args)
        {
            string First;
            string Last;
            First = "Cristiano";
            Last = " Ronaldo";
            Console.Write("Please enter student name <First Last>: ");
            Console.WriteLine(First + Last );

            Console.WriteLine(" ");

                                                     Console.WriteLine("*************NOTE**********************************************");
        Console.WriteLine("*** Be sure to include decimal point for scores.            ***");
        Console.WriteLine("***     !!!All score should range from 0.00 to 100.00 !!    ***");
        Console.WriteLine("***                                                         ***");
        Console.WriteLine("*** For example : 80.50                                     ***");
        Console.WriteLine("***************************************************************");

        Console.WriteLine(" ");

        decimal Exam_1;
        decimal Exam_2;
        decimal Exam_3;
        decimal Assignment_1;
        decimal Assignment_2;

        Console.Write("Please enter score for Exam 1 <Example: 100.0>: ");
        Exam_1 = Convert.ToDecimal(Console.ReadLine());

        if (Exam_1 < 0.0 | Exam_1 > 100.0)
            Console.Write("Exam score cannot be less than 0. or greater than                      100.0. Please re-enter the score for Exam 1 <Example: 95.0>:");
            Exam_1 = Convert.ToDecimal(Console.ReadLine());

        Console.Write("Please enter score for Exam 2 <Example: 0.0>: ");
        Exam_2 = Convert.ToDecimal(Console.ReadLine());

【问题讨论】:

  • 试试0M &lt;= myDecimal || 100M &gt;= myDecimal(参考:msdn.microsoft.com/en-us/library/364x0z75.aspx
  • 大多数情况下,这个错误是因为投射问题。如果您粘贴代码,我们可以准确给出正确答案。

标签: c# .net comparison


【解决方案1】:

我在您的代码中发现了至少 四个 问题。

首先,如前所述,您应该使用M 后缀告诉C# 编译器它是decimal,以便接受比较:

if (Exam_1 < 0.0M | Exam_1 > 100.0M)

但是其次,使用||而不是|,因为你要做OR操作,而不是Bitwise-OR

if (Exam_1 < 0.0M || Exam_1 > 100.0M) //change | to ||

第三,我认为您知道这一点非常重要:您不需要 decimal 考试成绩的数据类型(除非你的考试成绩可以是 99.12345678901234556789012345 格式 - 这是完全不可能的)。

decimal 通常用于精度要求非常高的数字(如银行中的money 计算),精度可达 16 位以上。如果你的考试成绩不需要,不要使用decimal,这是矫枉过正。只需将doubleintfloat 用作您的Exams,您很可能走在正确的轨道上。

第四,关于你的错误处理,这是不正确的做法:

if (Exam_1 < 0.0 | Exam_1 > 100.0)
    Console.Write("Exam score cannot be less than 0. or greater than                      100.0. Please re-enter the score for Exam 1 <Example: 95.0>:");
    Exam_1 = Convert.ToDecimal(Console.ReadLine());

由于两个原因:

  1. 您的Exam_1 位于块外(没有{} 括号)
  2. 你使用if,而你应该使用while

这是正确的做法:

double Exam_1 = -1; //I use double to simplify

Console.Write("Please enter score for Exam 1 <Example: 100.0>: ");
Exam_1 = Convert.ToDouble(Console.ReadLine());

while (Exam_1 < 0.0 || Exam_1 > 100.0) { //see the curly bracket
    Console.Write("Exam score cannot be less than 0. or greater than                      100.0. Please re-enter the score for Exam 1 <Example: 95.0>:");
    Exam_1 = Convert.ToDouble(Console.ReadLine());
} //see the end curly bracket

在 C# 语言中,缩进并不意味着作用域,这与 Python 等语言不同。

【讨论】:

  • @Abdulhamid no...您的错误处理(在您的术语中是“循环”,但实际上不是)似乎是另一个问题...不幸的是..
  • 您能否解释一下这个问题,这样我以后就不会再遇到它了?
  • 扫描时,我注意到您刚才给我的方式并没有将用户输入限制为“十进制”(即 2.0 而不是 2)
  • @Abdulhamid 是的,通常我们不会强制用户在不更改值时输入额外的零。这是因为 Convert.ToDouble 行为可以区分这一点。如果你想设置.00这样的限制,那么即使decimal也不会有任何效果。相反,您需要的是一个自定义字符串检查器——这可能会很麻烦,而且好处很少。因此,人们通常不会那样做。
【解决方案2】:

对于小数,您必须在值中添加“M”后缀以告诉计算机它是小数。否则计算机会将其视为双重。

你的小数

【讨论】:

    【解决方案3】:

    正如其他人已经指出的那样。为了使用大于或小于运算符比较 decimal 类型,您必须将其与另一个 decimal 类型进行比较。为了将文字数字声明为十进制,它需要Mm 后缀。这里是decimal 类型上的MSDN 供参考。

    if (Exam_1 < 0.0m || Exam_1 > 100.0m)
    

    Here's a .NET fiddle with the fix.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-22
      • 1970-01-01
      相关资源
      最近更新 更多