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



我正在尝试提出一个程序来计算从用户输入中给出的成绩。我还试图对用户输入的高低设置限制(即 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());

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

首先,如前所述,您应该使用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 等语言不同。

对于十进制,您必须在值中添加"M"后缀以告诉计算机它是小数。否则计算机会将其视为双精度。

您的十进制<98.56M;

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

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

这是修复程序的 .NET 摆弄。

相关内容

最新更新