长话短说,我目前正在学习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);
}
答案 0 :(得分:4)
这些:
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)
此:
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
s相同。
这是很多重复的代码:
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();
}
此:
else {
totalScore = totalScore;
}
什么都不做。删除它。
答案 1 :(得分:3)
这不符合你的想法:
public function setLightSaber(LightSaberInterface $saber)
需要写成:
if (R1 == 1 || 2)
同样适用于其他案件。
if (R1 == 1 || R1 == 2)
中还有两个问题 - 您似乎缺少switch
语句,并且您已将break
标签写成字符,例如
case
应该是:
case '1':
case '2':
totalScore = totalScore + 2;
(除非你实际上打算&#34;通过&#34;到下一个案例标签?)。