CS0019运营商不能应用到类型'布尔'和'廉政'的操作数 [英] CS0019 Operator cannot be applied to operands of type 'bool' and 'int'

查看:151
本文介绍了CS0019运营商不能应用到类型'布尔'和'廉政'的操作数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这计划是响应分配:

创建接受任意数量的整数参数)命名的总和(一种方法和
显示它们的总和。编写演示总和()方法作品Main()方法
正常工作时通过一,三,五,或10整数数组。保存该程序作为
UsingSum.cs。

从Microsoft®的Visual C#®2008年,介绍了面向对象程序设计,3E,乔伊斯·法雷尔

我的code。在//第1步:部分得到CS0019错误,其中指出,它不能应用于类型为布尔和INT

的操作数

我强烈怀疑还有其他的问题,这code,但它是在什么我有一个很大的改进的四个小时前...

 使用系统;公共类UsingSum
{
    公共静态无效的主要()
    {        和();    }    公共静态无效的总和()   {

//步骤1:增加一个,三,五

  INT N;
  INT O;
  INT磷;
  INT firstTotal;
  串K,L,M;
  布尔q;
  BOOL R;
  布尔S;  Console.Write(输入号码1:);
  K =到Console.ReadLine();
  Q = Convert.ToBoolean(K);
  N = Convert.ToInt32(K);  Console.WriteLine();  如果(Q == 1)
        {
           Console.WriteLine(输入接受。);
        }     其他
        如果(!(Q == 1))
     {
     Console.WriteLine(错误:您没有键入数字1,请重试);
     Console.Write(输入号码1:);
     K =到Console.ReadLine();
     Q = Convert.ToBoolean(K);
     N = Convert.ToInt32(K);     }  Console.Write(输入号码3:);
  L =到Console.ReadLine();
  R = Convert.ToBoolean(升);
  O = Convert.ToInt32(L);  Console.WriteLine();  如果(为r = 2 || R> = 4)
     {
     Console.WriteLine(错误:您没有键入数字3,请重试);
     Console.Write(输入号码3:);
     L =到Console.ReadLine();
     R = Convert.ToBoolean(升);
     O = Convert.ToInt32(L);     }     其他
        如果相关(r = 3)
        {
           Console.WriteLine(输入接受。);
        }  Console.Write(输入号码5:);
  M =到Console.ReadLine();
  S = Convert.ToBoolean(米);
  p值= Convert.ToInt32(米);  Console.WriteLine();  如果(S< = 4 || S> = 6)
     {
     Console.WriteLine(错误:您没有键入数字5,请重试);
     Console.Write(输入号码5:);
     M =到Console.ReadLine();
     S = Convert.ToBoolean(米);
     p值= Convert.ToInt32(米);     }     其他
        如果(S = 5)
        {
           Console.WriteLine(输入接受。);
        }  firstTotal = N + O + P;  Console.WriteLine({0} + {1} + {2} = {3},N,O,P,firstTotal);

//第2步:输入为整数数组[10]

  int类型的;
  INT B:
  INT℃;
  INT D组;
  INTê;
  INT F;
  INT克;
  INT H;
  INT I;
  诠释J;
  INT无限的;
  INT arrayTotal;  Console.Write(输入第一个整​​数加法:);
  一个= Convert.ToInt32(到Console.ReadLine());
  Console.Write(加法输入第二个整数:);
  B = Convert.ToInt32(到Console.ReadLine());
  Console.Write(加法进入第三个整数:);
  C = Convert.ToInt32(到Console.ReadLine());
  Console.Write(进入第四整数加法:);
  D = Convert.ToInt32(到Console.ReadLine());
  Console.Write(加法进入第五个整数:);
  E = Convert.ToInt32(到Console.ReadLine());
  Console.Write(加法进入第六个整数:);
  F = Convert.ToInt32(到Console.ReadLine());
  Console.Write(加法进入第七个整数:);
  G = Convert.ToInt32(到Console.ReadLine());
  Console.Write(加法进入第八个整数:);
  H = Convert.ToInt32(到Console.ReadLine());
  Console.Write(加法进入第九个整数:);
  I = Convert.ToInt32(到Console.ReadLine());
  Console.Write(加法进入第十个整数:);
  J = Convert.ToInt32(到Console.ReadLine());  arrayTotal = A + B + C + D + E + F + G + H + I + J;  Console.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 [] arrayTwo;
  字符串x;
  字符串添加;
  诠释Ÿ;
  INT总;
  而(Console.Write(输入一个整数加法,或类型添加到计算的总和:))
  {
     X =到Console.ReadLine();
     Y = Convert.ToInt32(X);     如果(X = ADD)
     {
        Console.WriteLine(计算总和);
     }
  }
  对于(Y = 0; Y< arrayTwo.Length ++ Y)
  {
  总= arrayTwo [Y] + arrayTwo [Y];
  ++ arrayTwo [Y];
  Console.WriteLine(========================);
  Console.WriteLine(= / N =合计为:{0} = / N =,总数);
  Console.WriteLine(========================);
  }
 }
}


解决方案

您可以不写(Q == 1)在C#中,因为问:布尔 1 INT

只能相比,另一个布尔前pression,例如真正,另一个布尔变量等。

This program is in response to the assignment:

"Create a method named Sum()that accepts any number of integer parameters and displays their sum. Write a Main()method that demonstrates the Sum()method works correctly when passed one, three, five, or an array of 10 integers. Save the program as UsingSum.cs."

from Microsoft® Visual C#® 2008, An Introduction to Object-Oriented Programming, 3e, Joyce Farrell

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

  int n;
  int o;
  int p;
  int firstTotal;  
  string k, l, m;
  bool q;
  bool r;
  bool s;

  Console.Write("Type the number 1: ");    
  k = Console.ReadLine();
  q = Convert.ToBoolean(k);
  n = Convert.ToInt32(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();
     q = Convert.ToBoolean(k);
     n = Convert.ToInt32(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();
  s = Convert.ToBoolean(m);
  p = Convert.ToInt32(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();
     s = Convert.ToBoolean(m);
     p = Convert.ToInt32(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;
  int b;
  int c;
  int d;
  int e;
  int f;
  int g;
  int h;
  int i;
  int j;
  int unlimited;
  int arrayTotal;

  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;
  string x;
  string ADD;
  int y;
  int total;


  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("========================");
  }                                         
 }  
}  

解决方案

You can't write (q == 1) in C#, because q is a bool and 1 is an int.

q can only be compared to another boolean expression, e.g. true, false, another boolean variable, etc.

这篇关于CS0019运营商不能应用到类型'布尔'和'廉政'的操作数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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