CS0019 Operator cannot be applied to operands of type #39;bool#39; and #39;int#39;(CS0019 运算符不能应用于“bool和“int类型的操作数)
问题描述
这个程序是为了响应任务:
This program is in response to the assignment:
"创建一个名为 Sum()
的方法,它接受任意数量的整数参数和显示他们的总和.编写一个 Main()
方法,证明 Sum()
方法在传递一个、三个、五个或十个整数的数组时可以正常工作.将程序另存为 UsingSum.cs
."
"Create a method named Sum()
that accepts any number of integer parameters and
displays their sum. Write a Main()
method that demonstrates that the Sum()
method works correctly when passed one, three, five, or an array of ten integers. Save the program as UsingSum.cs
."
来自 Microsoft® Visual C#® 2008,面向对象编程简介,3e,Joyce Farrell
from Microsoft® Visual C#® 2008, An Introduction to Object-Oriented Programming, 3e, Joyce Farrell
//step 1:"部分中的代码出现 CS0019 错误,指出它不能应用于 bool
和 int
类型的操作数.
My code in the "//step 1:" part is getting the CS0019 error, which states that it cannot be applied to operands of type bool
and int
.
我高度怀疑这段代码还有其他问题,但与四个小时前的代码相比,这是一个很大的改进……
I highly suspect there are also other problems with this code, but it's a great improvement over what I had four hours ago…
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#写(q == 1)
,因为q
是一个bool
和 1
是一个 int
.
You can't write (q == 1)
in C#, because q
is a bool
and 1
is an int
.
q
只能与另一个布尔表达式进行比较,例如true
、false
、另一个布尔变量等
q
can only be compared to another boolean expression, e.g. true
, false
, another boolean variable, etc.
这篇关于CS0019 运算符不能应用于“bool"和“int"类型的操作数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:CS0019 运算符不能应用于“bool"和“int"类型的操作数


- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 输入按键事件处理程序 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01