本文介绍了运算符'<'不能应用于“十进制”和“双精度”类型的操作数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试提出一个程序,该程序可以根据用户输入的分数来计算成绩。我还试图设置用户输入的上限或下限(即0 = 100)。但是当我使用十进制时,它总是给我这个错误,运算符'<'不能应用于类型'decimal'和'double'的操作数

I'm trying to come up with a program that calculates grades given from the users input. I am also trying to set a limit on how high or low the user input can be (i.e 0 <= or >= 100). But when I use decimal it keeps giving me this error, "Operator '<' cannot be applied to operands of type 'decimal' and 'double'"

 c 操作

But secondly, use || instead of |, because you want to do OR operation, not Bitwise-OR

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

然后第三次,我想让您知道这一点很重要:您考试标记的 不需要 十进制数据类型(除非您的考试标记的格式可以为99.12345678901234556789012345-这是

And thirdly, I think quite important for you to know this: you wouldn't need decimal data type for exam mark (unless your exam mark can be of format 99.12345678901234556789012345 - which is quite impossible).

十进制通常用于要求非常高精度的数字(例如 money 进行计算),精度超过16位。如果您的考试成绩不需要,不要使用十进制,那就是 overkill 。只需将 double 或 int 或 float 用作考试,您很可能就走对了。

decimal is normally used for numbers requiring very high precision (such as money calculation in the bank) up to more than 16-digit accuracy. If your exam mark does not need that, don't use decimal, it is overkill. Just use double or int or float for your Exams and you are most probably in the right track.

第四,关于您的错误处理,这是错误的操作方式:

Fourthly, about your error handling, this is incorrect way of doing it:

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. 您的考试_1 在代码块之外(没有 {} 括号)

  2. 您使用 if ,而应使用 while

  1. Your Exam_1 is outside of the block (there isn't {} bracket)
  2. You use if while you should use 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这样的语言。

In C# language, indentation does not mean scoping, unlike in language like Python.

这篇关于运算符'&lt;'不能应用于“十进制”和“双精度”类型的操作数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 21:57