c - 如何让用户输入一个以逗号分隔的彩票号码(例如 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]);
解决方案
首先,您将数组分配为 tickentNum。对于 winNum,为什么要初始化一个数组?这将为您提供 n 个 winNum 名额。此外,您必须在使用它之前给变量一个值,并且因为您说 n 不应大于 6,所以将其设置为 6。对于最后一行,我认为您正在尝试使用 for 循环,但您将它们命名错误所以它不起作用。
推荐阅读
- c - How do I loop over an array in a function in C?
- python - Herfindahl 指数使用过去 5 年的值
- pandas - 在熊猫中将 groupby 维度从 2 更改为 1
- windows - Could not find a part of the path 'c:\windows\system32\inetsrv\~\INSA.cfg'
- c# - C# ScreenCapture
- git - 带有标签的 Git 子目录过滤器
- json - How to execute 2 different rest api json payload in powershell
- clickhouse - ClickHouse 元组到列
- r - 如何在 R 中处理数据框中的重叠日期
- c++ - :-1: error: LNK1104: cannot open file 'MyProject.obj'