首页 > 解决方案 > 如何让用户输入一个以逗号分隔的彩票号码(例如 1,34,32,22,8,10),并将其存储在 C 中?

问题描述

我想要求用户输入一个彩票号码(1-49是范围,输入的数字不能超过6个),我想保存它。我想我可以使用一个数组。我刚开始使用 C 编程,所以我不确定。这就是我到目前为止所拥有的。(然后我必须将输入的号码与中奖号码进行比较并显示结果。)

int n,i;
int tickentNum[n]; //ticket number will be entered by user
int winNum [n];//this is the winning number

printf("Your 6 numbers of ticket  must be entered separated by a comma: 
and the tens digit MUST NOT be 0. \n");
printf("E.g. 1,2,4,21,22,18\n");

printf("Please enter your ticket number:___\n");
scanf("%d,",&ticketNum[i]);
        

标签: c

解决方案


首先,您将数组分配为 tickentNum。对于 winNum,为什么要初始化一个数组?这将为您提供 n 个 winNum 名额。此外,您必须在使用它之前给变量一个值,并且因为您说 n 不应大于 6,所以将其设置为 6。对于最后一行,我认为您正在尝试使用 for 循环,但您将它们命名错误所以它不起作用。


推荐阅读