2010-10-24 80 views
2

此程序是響應於所述分配的操作數:CS0019操作員不能被應用於類型「布爾」與「INT」

「創建接受任意數目的整數參數和 顯示器)命名薩姆(方法寫一個Main()方法,該方法演示Sum()方法在傳遞一個,三個,五個或十個整數數組時正確工作 。將程序另存爲 UsingSum.cs。「

從微軟的Visual C#®2008年,面向對象的編程介紹,3E,喬伊斯·法雷爾

我的代碼中的「//第1步:」部分得到CS0019錯誤,該公司聲明,不能應用於類型「布爾」和「廉政」

我高度懷疑還有其他的問題與此代碼,但它是在什麼我有四個小時前有很大的改進的操作數...

using System; 

public class UsingSum 
{ 
    public static void Main() 
    { 

     Sum(); 

    } 

    public static void Sum() 

    { 

//第一步:添加一個,三個,五個

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); 

//步驟2:輸入爲整數數組[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); 

//步驟3:無限的陣列另外

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

@Nooob考慮訪問codereview.stackexchange.com – ANeves 2013-10-22 11:28:31

回答

8

不能寫入在C#(q == 1),因爲qbool1int

q只能與另一個布爾表達式比較,例如, truefalse,另一個布爾變量等

+0

好吧,我幸運地有一位老師......他只用了一個星期的時間就給我發回了電子郵件,而且他太遙遠了,不敢用統治者來嘲笑我......甚至把它扔掉。這意味着我可以繼續輸入這些不起作用的悲慘代碼,漢斯。弗雷德裏克,謝謝你的回答。看起來我需要回顧一下編寫我的bools的規則...... – Nooob 2010-10-24 15:01:09

5

只是如果有人讀了這個問題,需要一個比較有boolint

(Convert.ToInt32(q) == 1) 
相關問題