CS0019 l'opérateur ne peut pas être appliqué aux opérandes de type « bool » et « int »

StackOverflow https://stackoverflow.com/questions/4007493

  •  25-09-2019
  •  | 
  •  

Question

Ce programme est en réponse à la mission:

"Créer une méthode nommée Sum() qui accepte un certain nombre de paramètres entiers et affiche leur somme. Ecrivez une méthode de Main() qui démontre que la méthode fonctionne correctement Sum() lorsqu'il est passé un, trois, cinq, ou un tableau de dix entiers. Enregistrez le programme UsingSum.cs. "

à partir de Microsoft® Visual C # ® 2008, Introduction à la programmation orientée objet, 3e, Joyce Farrell

Mon code dans le « // étape 1: ». Partie devient l'erreur CS0019, qui stipule qu'il ne peut être appliqué aux opérandes de type bool et int

Je soupçonne fortement il y a aussi d'autres problèmes avec ce code, mais il est une grande amélioration par rapport à ce que j'avais il y a quatre heures ...

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("========================");
     }                                         
  }
}
Était-ce utile?

La solution

Vous ne pouvez pas écrire (q == 1) en C #, parce que q est un bool et 1 est un int.

q ne peut être comparé à une autre expression booléenne, par exemple true, false, une autre variable booléenne, etc.

Autres conseils

Juste si quelqu'un lire cette question et a besoin de comparer un bool avec un int:

(Convert.ToInt32(q) == 1)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top