C 无法获得一行的正确输出
我制作了一个程序,生成两个骰子的骰子卷,将它们相加,然后显示执行此操作所需的尝试次数。我的问题是,程序把它们都正确地加起来了。它似乎无法打印出成功达到开始时输入的总数所需的尝试次数。我想要的预期输出示例如下C 无法获得一行的正确输出,c,printf,output,C,Printf,Output,我制作了一个程序,生成两个骰子的骰子卷,将它们相加,然后显示执行此操作所需的尝试次数。我的问题是,程序把它们都正确地加起来了。它似乎无法打印出成功达到开始时输入的总数所需的尝试次数。我想要的预期输出示例如下 Dice Thrower ============ Total sought : 7 Result of throw 1 : 4 + 4 Result of throw 2 : 1 + 4 Result of throw 3 : 2 + 6 Result of t
Dice Thrower
============
Total sought : 7
Result of throw 1 : 4 + 4
Result of throw 2 : 1 + 4
Result of throw 3 : 2 + 6
Result of throw 4 : 1 + 1
Result of throw 5 : 4 + 3
You got your total in 5 throws!
这是我的密码
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int validator(); //Function to validate
void clear(void); //Clear function
int main() {
int i=0,counter = 0, input, SUM, random1, random2; //Variables
printf("Dice Thrower\n"); //Print statements
printf("============\n");
printf("Total sought : ");
input = validator(); //Call Function Validator
srand(time(NULL));
for (i = 2; SUM!= input; i++) //For Loop for random dice throws
{
random1 = rand()%6+1;
random2 = rand()%6+1;
SUM = random1 + random2;
counter++;
printf("Result of throw %d : %d + %d\n", counter, random1, random2);
if (SUM == i)
{
printf("You got your total in %d throws!\n", counter); //Print statement for final total
}
}
return 0;
}
int validator() //Validator function
{
int tries = 1, boss, returnvalue;
char k;
do{
boss = scanf("%d%c", &returnvalue,&k);
if (boss == 0)
{
printf ("Invalid input, Try again : ");
clear();
}
else if(returnvalue >= 13 || returnvalue <= 1)
{
printf("Invalid integer entered, please enter an integer greater than or equal to 2 and less than or equal to 12 : ");
}
else if(k != '\n')
{
printf("Trailing characters present, reenter : ");
clear();
}
else
{
tries = 0;
}
} while ( tries == 1);
return returnvalue;
}
void clear (void) { //Clear function
while ( getchar() != '\n' );
}
#包括
#包括
#包括
int验证器()//要验证的函数
无效清除(无效)//清晰功能
int main(){
int i=0,counter=0,输入,求和,random1,random2;//变量
printf(“掷骰子者”\n”);//打印语句
printf(“======================\n”);
printf(“寻求的总额:”);
input=validator();//调用函数验证器
srand(时间(空));
for(i=2;SUM!=input;i++)//for随机掷骰循环
{
random1=rand()%6+1;
random2=rand()%6+1;
总和=随机数1+随机数2;
计数器++;
printf(“抛出%d的结果:%d+%d\n”,计数器,随机数1,随机数2);
如果(总和=i)
{
printf(“您得到了%d次抛出的总数!\n”,计数器);//打印最终总数的语句
}
}
返回0;
}
int validator()//验证器函数
{
int trys=1,boss,返回值;
chark;
做{
boss=scanf(“%d%c”、&returnvalue、&k);
如果(boss==0)
{
printf(“无效输入,请重试:”);
清除();
}
else if(returnvalue>=13 | | returnvalue我想你应该改变这个条件if(SUM==I)
如下:
if (SUM == input)
当您要将总和
与输入
进行比较时