其他和If语句C

时间:2020-06-07 16:54:56

标签: c if-statement

我刚刚开始用C语言进行编码,我不知道如何添加 doubles 的概念,程序会检测玩家何时在两个骰子上掷出相同的数字并进行报告作为关键成功,关键失败或关键联系。这是一个简单的骰子检查器:

//Dice Checker Demo 
#include <stdio.h> 

#define SECRET_TARGET 7

int main(void) {
    //read in and store the value of the first die 
    printf("Welcome to Dicechecker!\n"); 
    int dieOne; 
    printf("Please input the value of the first die: ");
    scanf("%d", &dieOne); 

    int dieTwo;
    printf("Please input the value of the second die: ");
    scanf("%d", &dieTwo);

    int total = dieOne + dieTwo;
    printf("The total of the dice roll is currently: %d\n", total);

    if (total > SECRET_TARGET) {
        printf("Skill Roll Successful, Congratulations\n");
    } else if (total == SECRET_TARGET) {
        printf("Close, but not close enough, try again please\n");
    } else if (dieOne == dieTwo && total > SECRET_TARGET) {
        printf("Critical Success! Large Applause\n");
    } else {
        printf("Skill Roll failed, Try Again\n");
    }    

    return 0; 
}

4 个答案:

答案 0 :(得分:0)

我不知道是否有更好的答案,但是我要做的只是在输入两个骰子的值时检查是否相等:

Range("B2").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select 
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add2 Key:=Range("C2:C" + CStr(ShCount)) _
,SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add2 Key:=Range("D2:D" + CStr(ShCount)) _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("B2:D" + CStr(ShCount))
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Range("B2").Select

并检查它们是否从那里获胜。

答案 1 :(得分:0)

您的问题是if s的顺序

当第一个if产生 true 时,将不再测试if,因为它们都在else之下。

这意味着如果总和大于SECRET_TARGET,if (die1 == die2)将永远不会被测试。

if (total > SECRET_TARGET) {
    //...
} else {
    // here total <= SECRET_TARGET
    if (die1 == die2 && total > SECRET_TARGET) {...}
    //                  ^^^^^^^^^^^^^^^^^^^^^ no, this is impossible
}

答案 2 :(得分:0)

错误在于您的逻辑:按照您的“ if-else if-else” 顺序

if (total > SECRET_TARGET) {
    printf("Skill Roll Successful, Congratulations\n");
} else if (total == SECRET_TARGET) {
    printf("Close, but not close enough, try again please\n");
} else if (dieOne == dieTwo && total > SECRET_TARGET) {
    printf("Critical Success! Large Applause\n");
} else {
    printf("Skill Roll failed, Try Again\n");
}

您在if (dieOne == dieTwo && total > SECRET_TARGET)之后执行if (total > SECRET_TARGET)的检查。请记住,当dieOne == dieTwo && total > SECRET_TARGET以及total > SECRET_TARGET为true时,将只执行其中一个分支。后者排名第一,第二名甚至都不会得到评估。

要检查您的 双骰子状况 ,只需在您的第一个分支中处理特殊情况:

if (total > SECRET_TARGET) {
    printf("Skill Roll Successful, Congratulations\n");

    /* Particular case: double dice */
    if (dieOne == dieTwo) {
        printf("Critical Success! Large Applause\n");
   }
} else if (total == SECRET_TARGET) {
    printf("Close, but not close enough, try again please\n");
}  else {
    printf("Skill Roll failed, Try Again\n");
}

答案 3 :(得分:0)

如果您对if测试进行了错误排序,则可以排除某些谓词正确的可能性(逻辑子句在代码的注释中进行了讨论,并嵌入其中):

    if (total > SECRET_TARGET) {
        /* here you execute things when total is greater than
         * SECRET_TARGET */
        printf("Skill Roll Successful, Congratulations\n");
    } else if (total == SECRET_TARGET) {
        /* here you have discarded total > SECRET_TARGET, so you know that
         * total <= SECRET_TARGET, and by if clause total has to be equal 
         * to SECRET_TARGET */
        printf("Close, but not close enough, try again please\n");
    } else if (dieOne == dieTwo && total > SECRET_TARGET) {
        /* here you continue discarding, you don't allow total to be
         * > SECRET_TARGET, also you don't allow to be == SECRET_TARGET, so
         * when you say that total > SECRET_TARGET, you are saying that 
         * total <= SECRET TARGET (by the discarding process) and that
         * total > SECRET_TARGET (by the second clause of this if test) and
         * this can never happen. Independently of what the values of the dice 
         * are */
        printf("Critical Success! Large Applause\n");
    } else {
        /* this part is executed only if all of the previous fail, so what
         * you know is that total < SECRET_TARGET, (but both dice can be equal
         * or not, as the third test failed anyway, so any value of the dice 
         * continues to be valid. */
        printf("Skill Roll failed, Try Again\n");
    }    

我认为,如果您更改测试顺序,然后先将两者的测试 相等的骰子,您将获得更好的结果。认为使用if ... else if的机会总是相互排斥的(即使从逻辑上讲,它们并非如此),这取决于您进行测试的顺序。首先转到最具体的案例,然后再对不太具体的案例进行测试。最后,else在没有匹配项时该怎么办。

就您而言,我应该这样做:

if (total > SECRET_TARGET) {
    if (dieOne == dieTwo) {
        printf("Critical Success! Large applause\n");
    } 
 // else { // you have not included this case 
 //        // (total > ST && dice different)
 // }
} else if (total == SECRET_TARGET) {
    printf("Close, but not close enough, try again please\n");
} else {
    printf("Skill Roll failed, Try Again\n");
}