2016-03-08 1102 views
8

我正試圖想出一個程序來計算用戶輸入的等級。我也試圖限制用戶輸入的高低(即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()); 
+1

請張貼的那個的投擲錯誤的準確代碼行。 –

+1

嘗試'0M <= myDecimal || 100M> = myDecimal'(參照https://msdn.microsoft.com/en-us/library/364x0z75.aspx) – souldzin

+0

多數此錯誤出現,因爲鑄造問題的時間。如果你粘貼代碼,我們可以準確地給出正確的答案。 – Sasa1234

回答

13

至少有四個問題我注意到你的代碼。

首先,如提到的,你應該使用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,它是矯枉過正。只需使用doubleintfloat作爲您的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()); 

由於兩方面的原因:

  1. Exam_1是塊之外(有ISN」 t {}支架)
  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語言。

+0

循環會工作嗎? – Abdulhamid

+0

@Abdulhamid沒有......你的錯誤處理(在你的術語「環」,但實際上它不是)似乎是另一個問題...不幸的是... – Ian

+0

願你揭示的,這個問題,使我不在未來遇到它? – Abdulhamid

1

對於小數,你必須加上「M」爲後綴該值告訴計算機它是一個小數。否則電腦會認爲它是雙倍的。

yourDecimal < 98.56M;

1

至於其他的都已經指出。爲了比較使用大於或小於運營decimal類型,您必須把它比作另一decimal類型。爲了將文字數字聲明爲小數,它需要Mm後綴。以下是decimal類型上的MSDN以供參考。

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

Here's a .NET fiddle with the fix.

相關問題