C# CS0019运算符不能应用于类型为';布尔';和';int';
此程序是对分配的响应: 创建一个名为C# CS0019运算符不能应用于类型为';布尔';和';int';,c#,boolean,operands,C#,Boolean,Operands,此程序是对分配的响应: 创建一个名为Sum()的方法,该方法接受任意数量的整数参数和 显示它们的总和。编写一个Main()方法,证明当传递一个、三个、五个或十个整数的数组时sum()方法工作正常。使用sum.cs将程序另存为 摘自Microsoft®Visual C#2008《面向对象编程入门》,3e,Joyce Farrell “//step 1:”部分中的我的代码得到了CS0019错误,该错误表示它不能应用于bool和int类型的操作数 我高度怀疑这段代码还存在其他问题,但与四小时前相比,这
Sum()
的方法,该方法接受任意数量的整数参数和
显示它们的总和。编写一个Main()
方法,证明当传递一个、三个、五个或十个整数的数组时sum()
方法工作正常。使用sum.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("========================");
}
}
}
使用系统;
使用sum的公共类
{
公共静态void Main()
{
Sum();
}
公共静态无效和()
{
//步骤1:添加一、三、五
布尔q,r,s;
第一个总数,n,o,p;
字符串k,l,m;
控制台。写入(“键入数字1:”;
k=Console.ReadLine();
n=转换为32(k);
q=转换为ToBoolean(k);
Console.WriteLine();
如果(q==1)
Console.WriteLine(“接受输入”);
如果(!(q==1))
{
WriteLine(“错误:您没有键入数字1。请重试。”);
控制台。写入(“键入数字1:”;
k=Console.ReadLine();
n=转换为32(k);
q=转换为ToBoolean(k);
}
}
控制台。写入(“键入数字3:”;
l=控制台.ReadLine();
r=转换为ToBoolean(l);
o=转换为32(l);
Console.WriteLine();
如果(r=4)
{
WriteLine(“错误:您没有键入数字3。请重试。”);
控制台。写入(“键入数字3:”;
l=控制台.ReadLine();
r=转换为ToBoolean(l);
o=转换为32(l);
}
其他的
如果(r=3)
Console.WriteLine(“接受输入”);
控制台。写(“键入数字5:”;
m=控制台.ReadLine();
p=换算成32(m);
s=转换为ToBoolean(m);
Console.WriteLine();
如果(s=6)
{
WriteLine(“错误:您没有键入数字5。请重试。”);
控制台。写(“键入数字5:”;
m=控制台.ReadLine();
p=换算成32(m);
s=转换为ToBoolean(m);
}
其他的
如果(s=5)
Console.WriteLine(“接受输入”);
firstTotal=n+o+p;
WriteLine(“{0}+{1}+{2}={3}”,n,o,p,firstTotal);
//步骤2:为数组[10]输入整数
int a,arrayTotal,b,c,d,e,f,g,h,i,j,无限;
编写(“为加法输入第一个整数:”);
a=Convert.ToInt32(Console.ReadLine());
编写(“为加法输入第二个整数:”);
b=Convert.ToInt32(Console.ReadLine());
编写(“为加法输入第三个整数:”);
c=Convert.ToInt32(Console.ReadLine());
Console.Write(“为加法输入第四个整数:”);
d=Convert.ToInt32(Console.ReadLine());
编写(“为加法输入第五个整数:”);
e=Convert.ToInt32(Console.ReadLine());
编写(“为加法输入第六个整数:”);
f=Convert.ToInt32(Console.ReadLine());
编写(“为加法输入第七个整数:”);
g=Convert.ToInt32(Console.ReadLine());
编写(“为加法输入第八个整数:”);
h=Convert.ToInt32(Console.ReadLine());
编写(“为加法输入第九个整数:”);
i=Convert.ToInt32(Console.ReadLine());
编写(“为加法输入第十个整数:”);
j=Convert.ToInt32(Console.ReadLine());
阵列总数=a+b+c+d+e+f+g+h+i+j;
WriteLine({0}+{1}+{2}+{3}+{4}+{5}+{6}+{7}+{8}+{9}={10}的总数),
a、 b、c、d、e、f、g、h、i、j、arrayTotal);
//步骤3:无限数组添加
int[]数组two;
整数总计,y;
字符串相加,x;
while(Console.Write(“为加法输入一个整数,或键入ADD来计算总和:”)
{
x=Console.ReadLine();
y=转换为32(x);
如果(x==相加)
Console.WriteLine(“计算总金额”);
}
对于(y=0;y你不能用C#写(q==1)
,因为q
是bool
,1
是int
q
只能与另一个布尔表达式进行比较,例如true
、false
、另一个布尔变量等。仅当有人阅读此问题并需要将bool
与int
进行比较时:
(Convert.ToInt32(q) == 1)
当我在VS2017中开发了Visual Studio 2015解决方案时,无意中打开了该解决方案,我遇到了这个错误以及其他许多错误
其他错误包括:
);期望
}预计嗯,我很幸运有一位老师。。。他只花了一个星期的时间给我回邮件,而且他离我太远了,不能用尺子让我发疯。。。甚至扔掉它。这意味着我可以继续输入这些不起作用的糟糕代码,汉斯。弗雷德里克,谢谢你的回答。看起来我需要回去复习我写的书的规则……Noob考虑访问CoDeVeWW.StAccExchange