运算符'<'不能应用于“十进制”和“双精度”类型的操作数 [英] Operator '<' cannot be applied to operands of type 'decimal' and 'double'

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

问题描述

我正在尝试提出一个程序,该程序可以根据用户输入的分数来计算成绩。我还试图设置用户输入的上限或下限(即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'"

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


推荐答案

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

There are at least four issues I noticed in your code.

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

Firstly, as mentioned, you should use M suffix to tell the C# compiler that it is a decimal for accepted comparison:

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

第二,使用 || 代替 | ,因为您要执行 OR 操作,而不是按位或 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;'不能应用于“十进制”和“双精度”类型的操作数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆