gpt4 book ai didi

c - 小游戏总数不相加

转载 作者:行者123 更新时间:2023-12-05 01:19:38 24 4
gpt4 key购买 nike

长话短说,我目前正在学习 C 编程,今天我正在尝试构建一个迷你游戏,根据玩家掷骰子的结果计算玩家总数。

我的程序功能正常,它会提示用户输入并比较他们的滚动以确保他们输入了正确的信息。

我遇到的问题是显示在程序末尾的总数似乎没有相加。无论我输入的结果如何,总值始终为 1。

有人能指引我正确的方向吗?

谢谢

#include <stdio.h>
int main(void)
{

int R1, R2, R3;
int totalScore = 0;

puts("Welcome to CRAZY dice game!");

puts("Enter Roll 1 Value: ");
scanf("%d", &R1);

while (R1 < 1 || R1 > 6) {
puts("Value is outside accepted input, try again: ");
scanf("%d", &R1);
}

puts("Enter Roll 2 Value: ");
scanf("%d", &R2);

while (R2 < 1 || R2 > 6) {
puts("Value is outside accepted input, try again: ");
scanf("%d", &R2);
}

puts("Enter Roll 3 Value: ");
scanf("%d", &R3);

while (R3 < 1 || R3 > 6) {
puts("Value is outside accepted input, try again: ");
scanf("%d", &R3);

}

if (R1 == 1 || 2) {
totalScore = totalScore + 1;
}
else if (R1 == 3 || 4) {
totalScore = totalScore + 2;
}
else if (R1 == 5 || 6) {
totalScore = totalScore + 3;
}


if (R2 < R1) {

switch (R2){

case '1':
case '2':
totalScore = totalScore + 1;

case '3':
case '4':
totalScore = totalScore + 2;

case '5':
case '6':
totalScore = totalScore + 3;
}
}


else {
totalScore = totalScore;
}


if (R3 < R2) {

switch (R3){

case '1':
case '2':
totalScore = totalScore + 2;

case '3':
case '4':
totalScore = totalScore + 4;

case '5':
case '6':
totalScore = totalScore + 6;
}
}

else if (R3 < R1) {

switch (R3){

case '1':
case '2':
totalScore = totalScore + 1;

case '3':
case '4':
totalScore = totalScore + 2;

case '5':
case '6':
totalScore = totalScore + 3;

}
}

printf("Total Score is: %d", totalScore);
}

最佳答案

问题:

  1. 这些:

    if (R1 == 1 || 2)
    else if (R1 == 3 || 4)
    else if (R1 == 5 || 6)

    没有达到您的预期。他们应该是

    if (R1 == 1 || R1 == 2)
    else if (R1 == 3 || R1 == 4)
    else if (R1 == 5 || R1 == 6)
  2. 这个:

    switch (R2){

    case '1':
    case '2':
    totalScore = totalScore + 1;

    case '3':
    case '4':
    totalScore = totalScore + 2;

    case '5':
    case '6':
    totalScore = totalScore + 3;
    }

    应该是

    switch (R2){

    /* Remove the '' as R2 is not a character, but an integer */
    /* Add breaks so that execution does not slip into subsequent cases */
    case 1:
    case 2:
    totalScore = totalScore + 1;
    break;

    case 3:
    case 4:
    totalScore = totalScore + 2;
    break;

    case 5:
    case 6:
    totalScore = totalScore + 3
    break;
    }

    其他 switch-case 也是如此。

建议:

  1. 这是很多重复的代码:

    puts("Enter Roll 1 Value: ");
    scanf("%d", &R1);

    while (R1 < 1 || R1 > 6) {
    puts("Value is outside accepted input, try again: ");
    scanf("%d", &R1);
    }

    puts("Enter Roll 2 Value: ");
    scanf("%d", &R2);

    while (R2 < 1 || R2 > 6) {
    puts("Value is outside accepted input, try again: ");
    scanf("%d", &R2);
    }

    puts("Enter Roll 3 Value: ");
    scanf("%d", &R3);

    while (R3 < 1 || R3 > 6) {
    puts("Value is outside accepted input, try again: ");
    scanf("%d", &R3);
    }

    我建议创建一个函数并返回一个值:

    int getVal()
    {
    static int counter = 1;
    int temp;

    puts("Enter Roll %d Value: ", counter);
    scanf("%d", &temp);

    while (temp < 1 || temp > 6) {
    puts("Value is outside accepted input, try again: ");
    scanf("%d", &temp);
    }

    counter++;

    return temp;
    }

    并使用以下方法从 main 调用该函数:

    R1 = getVal();
    R2 = getVal();
    R3 = getVal();

    或者在 main 中使用数组,例如:

    int R[3];

    代替

    int R1, R2, R3;

    这样你就可以使用:

    int i;
    for(i = 0; i < 3; i++)
    {
    R[i] = getVal();
    }
  2. 这个:

    else {
    totalScore = totalScore;
    }

    什么都不做。将其删除。

关于c - 小游戏总数不相加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32535877/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com