簡體   English   中英

無法弄清楚如何使用if語句返回除選項之外的其他任何內容

[英]can't figure out how to use if statements to return anything other then one option

該社區的第一篇文章。 開始審核我的大學中的某些C類,並且對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;
}

好的,讓我們先看一下if語句:

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

該分號立即結束您的else塊(@Joe:很好地發現...)。 因此,以下代碼塊將毫無疑問地執行:

{
    point1 = 0;
}

所有的點x值都將設置為0。這顯然不是您想要的。

else (Answer1 != 32);

實際上,這行相當於

else
{
    (Answer1 != 32);
}

因此,您無需計算answer1 != 32可以計算出answer1 != 32 ...我想您實際上是想寫這樣的:

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

但是,還有一點:第二個if是對第一個的精確補充,因此,如果第一個失敗,則第二個始終為真。 因此,您可以將其省略:

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

那些if else-if else如果您評估(或多或少)獨立條件,則使用類似:

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語句通常是更好的選擇。

旁注:在您的特定情況下,您可以更輕松地進行計算:C保證比較的結果為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

您總是得到1分。。。順便說一句:恰恰是一個例子,我更喜歡上面提到的switch語句...

一些東西:

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

請參閱此頁面以更完整地討論浮點比較。

下一個:

if ( score = 0 )

在此及以下if語句中,您使用=賦值運算符而不是==相等運算符; 您實際上是 0 score 由於賦值表達式的結果是賦值后左側的值,因此該表達式的計算結果為0 ,它為false,因此不采用分支。

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...變量執行此操作,因為它們的類型不同。

除其他事項外,您的if-else陳述是錯誤的。 首先,else之后的代碼塊不需要條件。 否則,已經假定前面的if語句不滿足任何條件。 實際上,由於您已經將答案變量設置為0,所以應該完全刪除else語句,因為它是多余的。 例如,答案1和2得分陳述應更像這樣:

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

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

的確,您在此處正確使用了==來檢查相等性,但在其他地方將=用作賦值,這不適用於if語句。

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

每次執行上面的語句,分數都設置為0! 這就是為什么您無法獲得任何其他結果的原因。 之后的每個后續if語句也會將分數設置為它試圖評估的值。

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

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

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

我建議您回顧一下一些基礎知識,並通讀其他簡單的示例程序。 這里有很多地方需要改進。 您正在嘗試一些代碼功能,因此將其分成較小的程序,這樣,當某些內容無法按您想要的方式工作時,您至少對如何查看外觀有了更好的了解,因為實際上程序中的代碼存在很多相互交織的問題,這些問題可能使您很難找出是哪一部分導致了您的麻煩。

我同意其他人的看法,看看其他示例,然后重寫此程序,這里的問題比我提到的要多得多。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM