运算符“<”不能应用于 'decimal' 和 'double' 类型的操作数

Operator '<' cannot be applied to operands of type 'decimal' and 'double'

我正在尝试开发一个程序来计算用户输入的分数。我还试图对用户输入的高低设置一个限制(即 0 <= 或 >= 100)。但是当我使用小数时,它一直给我这个错误,"Operator '<' cannot be applied to operands of type 'decimal' and 'double'"

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());

对于小数,您必须向值添加 "M" 后缀以告诉计算机它是小数。否则电脑会认为它是双倍的。

你的小数 < 98.56M;

我在您的代码中注意到至少 四个 个问题。

首先,如前所述,您应该使用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,这是矫枉过正。只需为您的 Exams 使用 doubleintfloat 并且您很可能在正确的轨道上。

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

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.

等语言不同

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

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

Here's a .NET fiddle with the fix.