CS0019 运算符不能应用于“bool”和“int”类型的操作数

问题描述 投票:0回答:4

此计划是为了响应作业:

“创建一个名为

Sum()
的方法,该方法接受任意数量的整数参数,并且 显示它们的总和。编写一个
Main()
方法,演示当传递一个、三个、五个或十个整数的数组时,
Sum()
方法可以正确工作。将程序保存为
UsingSum.cs
。”

来自 Microsoft® Visual C#® 2008,面向对象编程简介,3e,Joyce Farrell

“//step 1:”部分中的代码出现 CS0019 错误,该错误指出它不能应用于类型为

bool
int
的操作数。

我高度怀疑这段代码还存在其他问题,但与我四个小时前的情况相比,这是一个很大的改进......

using System;

public class UsingSum
{
    public static void Main()
    {
        Sum();
    }

    public static void Sum() 
    { 
       // Step 1: Addition of one, three, five
       bool q, r, s;
       int firstTotal, n, o, p;
       string k, l, m;

       Console.Write("Type the number 1: ");    
       k = Console.ReadLine();

       n = Convert.ToInt32(k);
       q = Convert.ToBoolean(k);

       Console.WriteLine();

       if (q == 1)
           Console.WriteLine("Input accepted.");
       else if (!(q == 1))
       {
          Console.WriteLine("Error: You didn't type the number 1. Please try again.");
          Console.Write("Type the number 1: ");    
          k = Console.ReadLine();

          n = Convert.ToInt32(k);
          q = Convert.ToBoolean(k);
       }
    }

    Console.Write("Type the number 3: ");    
    l = Console.ReadLine();
    r = Convert.ToBoolean(l);
    o = Convert.ToInt32(l);

    Console.WriteLine();

    if (r <= 2 || r >= 4)
    {
       Console.WriteLine("Error: You didn't type the number 3. Please try again.");
       Console.Write("Type the number 3: ");    
       l = Console.ReadLine();
       r = Convert.ToBoolean(l);
       o = Convert.ToInt32(l);
    }
    else
        if (r = 3)
           Console.WriteLine("Input accepted.");

    Console.Write("Type the number 5: ");    
    m = Console.ReadLine();

    p = Convert.ToInt32(m);
    s = Convert.ToBoolean(m);

    Console.WriteLine();

    if (s <= 4 || s >= 6)
    {
       Console.WriteLine("Error: You didn't type the number 5. Please try again.");
       Console.Write("Type the number 5: ");    
       m = Console.ReadLine();

       p = Convert.ToInt32(m);
       s = Convert.ToBoolean(m);
    }
    else
       if (s = 5)
          Console.WriteLine("Input accepted.");

     firstTotal = n + o + p;

     Console.WriteLine("{0} + {1} + {2} = {3}", n, o, p, firstTotal); 

     // Step 2: Entering integers for array[10]

     int a, arrayTotal, b, c, d, e, f, g, h, i, j, unlimited;

     Console.Write("Enter first integer for addition: ");
     a = Convert.ToInt32(Console.ReadLine());
     Console.Write("Enter second integer for addition: ");
     b = Convert.ToInt32(Console.ReadLine());
     Console.Write("Enter third integer for addition: ");
     c = Convert.ToInt32(Console.ReadLine());
     Console.Write("Enter forth integer for addition: ");
     d = Convert.ToInt32(Console.ReadLine());
     Console.Write("Enter fifth integer for addition: ");
     e = Convert.ToInt32(Console.ReadLine());
     Console.Write("Enter sixth integer for addition: ");
     f = Convert.ToInt32(Console.ReadLine());
     Console.Write("Enter seventh integer for addition: ");
     g = Convert.ToInt32(Console.ReadLine());
     Console.Write("Enter eighth integer for addition: ");
     h = Convert.ToInt32(Console.ReadLine());
     Console.Write("Enter ninth integer for addition: ");
     i = Convert.ToInt32(Console.ReadLine());
     Console.Write("Enter tenth integer for addition: ");
     j = Convert.ToInt32(Console.ReadLine());

     arrayTotal = a + b + c + d + e + f + g + h + i +j; 

     Console.WriteLine("The total of {0} + {1} + {2} + {3} + {4} + {5} + {6} + {7} + {8} + {9} = {10}",
     a, b, c, d, e, f, g, h, i, j, arrayTotal);

     // Step 3: Unlimited array addition
     int[] arrayTwo;
     int total, y;
     string ADD, x;

     while(Console.Write("Enter an integer for addition, or type ADD to calculate the sum: "))
     {
        x = Console.ReadLine();
        y = Convert.ToInt32(x);

        if (x == ADD)
           Console.WriteLine("Calculating the total sum");
     }   

     for (y = 0; y < arrayTwo.Length; ++y)
     {
        total = arrayTwo[y] + arrayTwo[y];              
        ++arrayTwo[y];
        Console.WriteLine("========================");
        Console.WriteLine("=/n= The total is: {0} =/n=", total);
        Console.WriteLine("========================");
     }                                         
  }
}
c# boolean operands
4个回答
19
投票

在 C# 中不能写

(q == 1)
,因为
q
bool
1
int

q
只能与另一个布尔表达式进行比较,例如
true
false
、另一个布尔变量等


12
投票

如果有人读了这个问题并且需要将

bool
int
进行比较:

(Convert.ToInt32(q) == 1)

0
投票

当我无意中使用 Visual Studio 2015 打开一个在 VS2017 中开发的解决方案时,我遇到了这个错误和许多其他错误。

其他错误包括:

;预计

}预计


0
投票

尝试使用

A.Equals(B)
代替
A == B

© www.soinside.com 2019 - 2024. All rights reserved.