2017-06-22 35 views
-1

關於此社區的第一篇文章。在我的大學開始審覈一些C課程,並在If語句中遇到麻煩。在凝視和改變​​我的代碼的幾個小時的變化後,我仍然沒有弄清楚爲什麼我不能返回除了我設置的「得分」標準之外的值。如果有人願意告訴我我的語法錯誤發生在哪裏,甚至可能提示我應該重寫哪一部分,我將非常感激。此外,如果我的邏輯可以使用一些刷牙,我會喜歡指針。再次感謝。無法弄清楚如何使用if語句來返回除了一個選項以外的任何東西

#include <stdio.h> 


/* Main function */ 

int main() 
{ 
    int Answer1; 
    float Answer2; 
    float Answer3; 
    int Answer4; 
    int Answer5; 
    float Answer6; 

    int point1; 
    point1 = 0; 
    int point2; 
    point2 = 0; 
    int point3; 
    point3 = 0; 
    int point4; 
    point4 = 0; 
    int point5; 
    point5 = 0; 
    int point6; 
    point6 = 0; 

    char name; 
    int sum; 
    int score; 
    int multiplier1; 
    int bonus_score; 
    int counter; 
    counter = 1; 
    int x; 
    x = 1; 
    int y; 
    y = 2; 
    int z; 
    z = 3; 


    /* 
    **************** 
    **************** 
    this is the end of my declaration system, now begins the actual functions. 
    *************** 
    **************** 
    */ 


    printf (" Welcome to your career amplitude test! This simple program will tell you how far you'll go in life. \n"); 
    printf (" Remember to write your answer to at least two decimal places. \n \n "); 


    printf ("1. What is 5 + 27? \n"); 
    scanf ("%i", &Answer1); 

    printf("2. what is 2.7 - .85? \n"); 
    scanf ("%f", &Answer2); 

    printf ("3. what is 2.3 - .1 * 4? \n"); 
    scanf ("%f", &Answer3); 

    printf ("4. what is 123 * 123?\n"); 
    scanf ("%i", &Answer4); 

    printf ("5. what is 945/5?\n"); 
    scanf ("%i", &Answer5); 

    printf (" Bonus Question!!!!! \n"); 
    printf (" what is the square root of 105487.19? You have 10 seconds to enter a number (not really though.) \n"); 
    scanf ("%f", &Answer6); 


    /* 
    ****************** 
    ****************** 
    after those are printed/scanned it will come up with a potential scoring 
    system using if statements and if else 
    ***************** 
    ***************** 
    */ 


    if (Answer1 == 32) 
    { 
     point1 = 1; 
    } 
    else (Answer1 != 32); 
    { 
     point1 = 0; 
    } 
    if (Answer2 == 1.85) 
    { 
     point2 = 1; 
    }  
    else (Answer2 != 1.85); 
    { 
     point2 = 0; 
    } 
    if (Answer3 == 1.9) 
    { 
     point3 = 1; 
    }  
    else (Answer3 != 1.9); 
    { 
     point3 = 0; 
    } 
    if (Answer4 == 15129) 
    { 
     point4 = 1; 
    } 
    else (Answer4 != 15129); 
    { 
     point4 = 0; 
    } 
    if (Answer5 == 189) 
    { 
     point5 = 0; 
    } 
    else (Answer5 != 189); 
    { 
     point5 = 0; 
    } 

    if (Answer6 != 324.787) 
    { 
     point6 = 0; 
    } 
    if (Answer6 = 324.787) 
    { 
     point6 = 1; 
    } 

    /* 
    ****************** 
    ****************** 
    Now to actauly grade the assignment compared to the scoring system just established. 
    ****************** 
    ****************** 
    */ 

    while (counter < 100) 
    { 
     counter = counter+x+y+z; 
     printf("Processing at a light speed rate equal to %i \n \n \n", counter); 
    } 

    /* the above is a joke and just wanted to implement a loop for pratice */ 

    printf(" This is your raw score without the Bonus. \n");  

    sum = (point1 + point2 + point3 + point4 + point5); 
    score = sum; 

    if (score = 0) 
    {  
     score = 0; 
     printf (" Score: 0 \n"); 
     printf (" You missed every question! \n"); 
    } 

    else if (score = 1) 
    { 
     score = 1; 
     printf (" Score: 1 out of 5 \n"); 
     printf (" You only got one question right! The world needs ditch diggers too. \n"); 
    } 

    else if (score = 2) 
    { 
     score = 2; 
     printf (" Score: 2 out of 5 \n"); 
     printf (" You missed 3 questions, pratice your soft skills \n"); 
    } 

    else if (score = 3) 
    { 
     score = 3; 
     printf (" Score: 3 out of 5 \n"); 
     printf (" I forsee a future in the hedge fund industry \n"); 
    } 

    else if (score = 4) 
    { 
     score = 4; 
     printf (" Score: 4 out of 5 \n "); 
     printf (" you could probably cook books for Enron \n"); 
    } 


    else if (score = 5) 
    { 
     score = 5; 
     printf (" Score: 5 out of 5 \n"); 
     printf (" Go out there and break code for the CIA \n"); 
    } 

    printf ("With the bonus considered, your score is now \n"); 
    if (point6 = 1) 
    { 
     multiplier1 = 2; 
    } 
    else if (point6 = 0) 
    { 
     multiplier1 = 1; 
    } 

    if (multiplier1 = 2) 
    { 
     bonus_score = score * 2; 

     printf (" %i", bonus_score); 
    } 
    else if (multiplier1 = 1) 
    { 
     bonus_score = score; 
     printf (" You got the Bonus wrong. Nothing new to see here. \n"); 
    } 


    return 0; 
} 
+0

@IharobAlAsimi他指的是在大學教授的講座,課程等課程,而不是OOP概念。 –

+2

你應該全部重寫。一團糟。 –

+0

@JacobH我很快意識到這一點,並刪除了評論。 –

回答

0

您的if-else語句錯誤等等。首先,else之後的代碼塊不需要條件。否則已經假定前面的if語句沒有滿足任何條件。事實上,由於您已經將答案變量設置爲0,因此您應該完全刪除else語句,因爲它是多餘的。例如,回答1和2的比分語句應該看起來更像是這樣的:

if (Answer1 == 32) 
{ 
    point1 = 1; 
} 

if (Answer2 == 1.85) 
{ 
    point2 = 1; 
}  

這也是事實,你正確使用==這裏檢查的平等,而是使用=作爲分配的其他地方,這是不正確的如果聲明。

if (score = 0) <--needs to have ==, or else it actually sets the score to 0! 
    {  
    score = 0; 
    printf (" Score: 0 \n"); 
    printf (" You missed every question! \n"); 
    } 

每次執行上述語句時,score都設置爲0!這就是爲什麼你不能得到任何其他結果。之後的每個後續if語句都會將得分設置爲要評估的值。

如果將來您確實想使用else語句,那麼您也有錯誤的分號。

else (Answer1 != 32); <--This semicolon should not be here. 
{ 
    point1 = 0; 
} 

該分號結束else語句,而不是運行下面的代碼。而且可能發生的情況是,下面的代碼塊不再與條件綁定並且每次都執行。

我建議回頭查看一些基礎知識並閱讀其他簡單的示例程序。這裏有很多需要改進的地方。你正在試驗代碼的某些功能,所以把它分成更小的程序,這樣,當某些事情沒有按照你想要的方式工作時,你至少有更好的想法去看看,因爲它就是一個很多與程序中的代碼交織在一起的問題可能會讓您很難找出哪個部分會給您帶來麻煩。

我同意別人的觀點,看看其他一些例子,並重寫了這個程序,還有比我提到的更多的問題。

+0

嘿 - 我真的很感激你花時間解釋我是怎麼回事。這是我學習語言的第六天,所以我知道這可能是垃圾。希望我的時間更好。再次感謝。 –

1

OK,讓我們先來看看你的if聲明:

if (Answer1 == 32) 
{ 
    point1 = 1; 
} 
else (Answer1 != 32); 
//    ^!!! 

這分號立即結束你的其他塊(@Joe:很好看準...)。所以下面的塊將被執行undonditionally

{ 
    point1 = 0; 
} 

一,即全部您的要求x值將被設置爲0.顯然,這不是您想要的。

else (Answer1 != 32); 

其實,這條線就相當於

else 
{ 
    (Answer1 != 32); 
} 

所以你計算answer1 != 32而沒有評估的結果......我想你實際上是打算寫這樣的:

if (Answer1 == 32) 
{ ... } 
else if(Answer1 != 32) 
// ^^    ^(if and no colon) 
{ ... } 

然而,還有一點:第二個如果是第一個的精確補充,所以如果第一個失敗,第二個是總是是真的。所以,你可以就這麼走了出來:

if (Answer1 == 32) 
{ ... } 
else 
// ^^ no if at all any more... 
{ ... } 

那些如果其他人,如果別人是如果評估使用(或多或少)獨立的條件下,這樣的:

if(action1() == ERROR) 
    logIt(); 
else if(action2() == ERROR) 
    logIt(); 
else if(action3() == ERROR) 
    logIt(); 
else 
    return OK; 
return ERROR; 

,或者如果你檢查不同值:

if(x == 1) {} else if(x == 2) {} ... 

在後一種情況下,switch語句通常是更好的選擇。

邊注:在您的特定情況下,你可能會計算更容易:比較是1或0的Ç擔保造成的,所以你可以簡單地做:

pointX = answerX == resultX; 

最後,你的分數評價:你需要比較

if(score == x) 
//  ^^ 

單一平等字符分配:

if(score = 0) // will assign 0 to score and miss the condition 
else if(score = 1) // will assign 1 to score and pass the condition 

I. e。你總是得到1分......順便說一句:究竟這將是在那裏我寧願switch語句如上述例子...

+0

我真的很感激你花時間幫助我。我仍在學習基礎知識,所以我知道我的第一個程序可能是垃圾。希望隨着時間變得更好。再次感謝 –

1

有幾件事情:

if (Answer1 == 32) 
{ 
    point1 = 1; 
} 
else (Answer1 != 32); <<< problem 
{ 
    point1 = 0; 
} 

該代碼被解析爲

if (Answer1 == 32) 
{ 
    point1 = 1; 
} 
else 
    Answer1 != 32; // expression is evaluated, result is discarded. 

{ 
    point1 = 0; 
} 

正因爲如此,point1 = 0;正在執行無條件(所述if語句體之外)。我猜你一定是在使用gcc,因爲編譯器沒有因爲在那裏裸露塊而大吼你。

一個else不採取控制表達;你只是寫

else 
{ 
    point1 = 0; 
} 

而且,由於你已經初始化point1爲0,則不需要else分支在所有;你只需要

if (Answer1 == 32) 
{ 
    point1 = 1; 
} 

你重複同樣的錯誤在if語句塊,所以這是你需要處理的第一件事。

其次,不建議使用帶有浮點類型的==。大多數浮點值不能以給定的位數存儲在正好,所以存儲的是一個近似值。通過scanf存儲到Answer2中的近似值可能與通過直接分配存儲的近似值不同。進一步解決問題的是像1.85這樣的浮點常量的類型爲double,它使用與float不同的表示,因此==甚至是更少的可能在這種情況下工作。

除非你真的空間(這你不是),使用double代替float - 您獲得更大的範圍和精度,併爲你做什麼,它不會是任何慢。

適當的浮點比較很匆忙,這裏有一個方法:

#include <math.h> 
#include <float.h> 

int EqualEnough(double a, double b, double max_diff) 
{ 
    double diff = fabs(a - b); 
    double a_abs = fabs(a); 
    double b_abs = fabs(b); 
    double larger = a_abs > b_abs ? a_abs : b_abs; 

    return diff <= larger * max_diff; 
} 

它並不完美,它不包括所有的情況,但對你的目的它應該工作不夠好。你會main之前定義它,你把它作爲

if (EqualEnough(Answer2, 1.85, DBL_EPSILON)) 
{ 
    point2 = 1; 
} 

this page浮點比較的更完整的討論。

下一頁:

if (score = 0) 

在這個和下面的if語句,用來代替==平等運營商=賦值運算符;你實際上分配值0到score。由於賦值表達式的結果是賦值後左邊的值,因此該表達式的計算結果爲0,該值爲假,因此不採用分支。

else if (score = 1) 

在這種情況下,score被設置爲1,則表達式的值是1,使分支被採用。因此,對於這些語句,請確保您使用==

if (score == 0) 
{ 
    ... 
} 
else if (score == 1) 
{ 
    ... 
} 

最後,這僅僅是一個風格的註釋比什麼都重要:

當你發現自己創造了一堆同類型的變量用相同的名稱,後跟一個基數(point1point2point3等),這是一個強烈的暗示你真的想要一個陣列

int point[6] = {0}; // initializes all elements to 0 

if (Answer1 == 32) 
    point[0] = 1; 

if (EqualEnough(Answer2, 1.85, DBL_EPSILON)) 
    point[1] = 1; 

等。請記住,C中的數組索引從0開始,而不是1,因此您的point數組中的元素將爲point[0],point[1],point[2],...,point[5]。這也可以很容易地總結一下你的觀點:

for (size_t i = 0; i < 6; i++) 
    sum += points[i]; 

,因爲他們有不同的類型,你不會爲你的Answer...變量做到這一點。

+0

嘿 - 我真的很感激你花時間解釋我所做的這些錯誤。這是我第六天學習C,所以我期望我的第一個程序是垃圾。再次感謝 –

+0

@ZachG:不客氣。 C不是一門容易學習的語言,即使對於有經驗的程序員也是如此。有很多地雷等着你踩着。只要不斷堵塞,它最終會開始有意義。如果您認真學習C語言,您可能需要爲語言標準的[在線草案](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf)添加書籤。 。這本身並不是一個好的教學資源,但它是一個非常寶貴的參考資料。 –

相關問題