长话短说,我目前正在学习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);
}发布于 2015-09-12 06:29:40
问题:
switch-case也是如此。建议:
main调用函数:
R1 = getVal();R2 = getVal();R3 = getVal();
或者在main中使用数组,如:
int R3;
而不是
int R1,R2,R3;
以便您可以使用:
i;for(i = 0;i< 3;i++) { Ri = getVal();}发布于 2015-09-12 06:27:36
这不像你想的那样:
if (R1 == 1 || 2)它需要写成:
if (R1 == 1 || R1 == 2)其他案件也是如此。
switch中还有两个问题--您似乎缺少了break语句,并且您编写了case标签,就好像它们是字符一样。
case '1':
case '2':
totalScore = totalScore + 2;应:
case 1:
case 2:
totalScore = totalScore + 2;
break;(除非你真的打算“通过”下一个案例标签?)
https://stackoverflow.com/questions/32535877
复制相似问题