运算符'<'不能应用于'decimal'和'double'类型的操作数
我正试图想出一个程序来计算用户输入的等级。我也试图限制用户输入的高低(即0 < =或> = 100)。但是,当我使用十进制它不断给我这个错误,“操作员‘<’不能应用于类型‘十进制’和‘双师型’的操作数”运算符'<'不能应用于'decimal'和'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
后缀告诉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.123456789的 - 这是完全不可能的)。
decimal
通常用于要求非常高精度的数字(例如银行中的money
计算),精度高达16位以上。如果您的考试标记不需要,不要使用decimal
,它是矫枉过正。只需使用double
或int
或float
作为您的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());
由于两方面的原因:
- 你
Exam_1
是块之外(有ISN” t{}
支架) - 您使用
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
@Abdulhamid没有......你的错误处理(在你的术语“环”,但实际上它不是)似乎是另一个问题...不幸的是... – Ian
愿你揭示的,这个问题,使我不在未来遇到它? – Abdulhamid
对于小数,你必须加上“M”为后缀该值告诉计算机它是一个小数。否则电脑会认为它是双倍的。
yourDecimal < 98.56M;
至于其他的都已经指出。为了比较使用大于或小于运营decimal
类型,您必须把它比作另一decimal
类型。为了将文字数字声明为小数,它需要M
或m
后缀。以下是decimal
类型上的MSDN以供参考。
if (Exam_1 < 0.0m || Exam_1 > 100.0m)
请张贴的那个的投掷错误的准确代码行。 –
尝试'0M = myDecimal'(参照https://msdn.microsoft.com/en-us/library/364x0z75.aspx) – souldzin
多数此错误出现,因为铸造问题的时间。如果你粘贴代码,我们可以准确地给出正确的答案。 – Sasa1234