- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
该程序用于显示3x4 阵列中3 个团队中得分最高的获胜团队。
input
或 teamscore
函数有问题。我找不到错误,我在 input
函数中的输入无法在 teamscore
函数中存储和计算。
void input(int a[NROW][NCOL])
{
int x,y;
double team;
for(x=0;x<NROW;x++)
{
printf("\nEnter your team: ");
scanf("%i",&team);
for(y=0;y<NCOL;y++)
{
printf("\nEnter the score: ");
scanf("%lf",&a[x][y]);
}
}
}
int teamscore(int a[NROW][NCOL])
{
int x,y,highest,team;
double sum;
for(x=0;x<NROW;x++)
{
for(y=0;y<NCOL;y++)
{
sum = sum + a[x][y];
}
}
for(x=0;x<NROW;x++)
for(y=0;y<NCOL;y++)
if (a[x][y]>highest)
{
highest=a[x][y];
}
return highest;
}
我所有的输出都是 0。
这是我的全部代码。
#include <stdio.h>
#define NROW 3
#define NCOL 4
void initialize(int a[NROW][NCOL])
{
int x,y;
for(x=0;x<NROW;x++)
{
for(y=0;y<NCOL;y++)
{
a[x][y]=0;
}
}
}
/* Display array in a matrix form*/
void disp_arr(int a[NROW][NCOL])
{
int x,y;
for(x=0;x<NROW;x++)
{
for(y=0;y<NCOL;y++)
{
printf("%i ",a[x][y]);
}
printf("\n");
}
}
/* Input the invidual score for 3 team*/
void input(int a[NROW][NCOL])
{
int x,y;
double team;
for(x=0;x<NROW;x++)
{
printf("\nEnter your team: ");
scanf("%i",&team);
for(y=0;y<NCOL;y++)
{
printf("\nEnter the score: ");
scanf("%lf",&a[x][y]);
}
}
}
/* Calculate the total of score for each team and return the index for the
row with the highest team score */
int teamscore(int a[NROW][NCOL])
{
int x,y,highest,team;
double sum;
for(x=0;x<NROW;x++)
{
for(y=0;y<NCOL;y++)
{
sum = sum + a[x][y];
}
}
for(x=0;x<NROW;x++)
for(y=0;y<NCOL;y++)
if (a[x][y]>highest)
{
highest=a[x][y];
}
return highest;
}
int main()
{
int ar[NROW][NCOL];
int team;
initialize(ar);
disp_arr(ar);
input(ar);
disp_arr(ar);
team=teamscore(ar);
printf("\nThe winniing team is Team %i",&team);
return 0;
}
如果能提供一些帮助,我将不胜感激。
谢谢!
最佳答案
关于您希望团队
成为什么,您的代码含糊不清。您提示用户输入,然后尝试将 team
存储为 double
?除非您希望您的团队编号是,例如123.456
,让你的团队成为一个int
,例如团队 1
,团队 10
,等等。
您遇到的下一个问题是获取团队编号的输入,但没有提供任何方式让该条目可用于除 input
之外的任何其他函数。如果您不传递另一个参数来捕获 team
的用户输入,那么您的团队只是 a
的索引,并且没有必要提示该输入。要使用户输入在 main
中可用,只需传递另一个参数(或者只需将 +1 添加到 NCOL
并为团队使用 col 0
数字)。以下是捕获用户输入的团队号码(例如团队 125, 190, ...
)并将其返回给调用者的方法:
int main (void) {
int hi_index = 0,
teams[NROW] = {0}, /* note array of NROW for teams */
ar[NROW][NCOL] = {{0}};
...
input (ar, teams);
然后您可以通过将 teams
数组作为参数传递,使 input
捕获该信息,例如
/* Input the individual score for 3 team*/
void input (int a[NROW][NCOL], int teams[NROW])
您必须验证所有用户输入。当使用 scanf
时,这意味着验证 return —— 转换次数(在 format string 中指定)确实发生了.您还必须检查用户是否生成了手动 EOF
(在 windoze 上为 Ctrl+d 或 Ctrl+z)。如果用户取消输入,您的代码应该优雅地处理该情况。如果用户刚刚提供了无效输入并且由于匹配或输入失败导致转换失败,您需要确定是否通知用户并提示用户“再试一次”或者您是否将其视为取消。
要以这种方式处理用户输入,一种方法是循环直到您收到有效输入或用户取消,例如在您的 input
函数中,您可以执行以下操作:
for (;;) { /* loop until valid input or user canceled */
int rtn;
printf ("\nEnter your team: ");
rtn = scanf ("%d", &teams[x]);
if (rtn == 1)
break;
else if (rtn == EOF) {
fprintf (stderr, "user canceled input.\n");
exit (EXIT_FAILURE);
}
fprintf (stderr, "error: invalid input.\n");
/* (you should empty stdin here) */
empty_stdin();
}
注意:要清空stdin
,您可以简单地使用getchar()
读取直到'\n'
或者遇到EOF
,例如
/* function to empty stdin */
void empty_stdin()
{
int c = getchar();
while (c != '\n' && c != EOF)
c = getchar();
}
您的 printf
和 scanf
format specifier 不匹配已包含在注释中。
将所有这些部分放在一起,并假设您实际上想要整数作为团队编号,而不是像 123.456
这样奇怪的团队编号,您可以执行类似于以下操作的操作:
#include <stdio.h>
#include <stdlib.h> /* for exit, EXIT_FAILURE */
#define NROW 3
#define NCOL 4
/* Display array in a matrix form*/
void disp_arr (int a[NROW][NCOL], int teams[NROW])
{
int x, y;
putchar ('\n');
for ( x = 0; x < NROW; x++)
{
printf ("team %3d : ", teams[x]);
for (y = 0; y < NCOL; y++)
printf (" %4d", a[x][y]);
putchar ('\n');
}
}
/* function to empty stdin */
void empty_stdin()
{
int c = getchar();
while (c != '\n' && c != EOF)
c = getchar();
}
/* Input the individual score for 3 team*/
void input (int a[NROW][NCOL], int teams[NROW])
{
int x, y;
for (x = 0; x < NROW; x++)
{
for (;;) { /* loop until valid input or user canceled */
int rtn;
printf ("\nEnter your team: ");
rtn = scanf ("%d", &teams[x]);
if (rtn == 1)
break;
else if (rtn == EOF) {
fprintf (stderr, "user canceled input.\n");
exit (EXIT_FAILURE);
}
fprintf (stderr, "error: invalid input.\n");
empty_stdin();
}
for (y = 0; y < NCOL; y++)
{
for (;;) { /* same loop until valid or canceled */
int rtn;
printf ("Enter team[%d] score[%d]: ", teams[x], y + 1);
rtn = scanf ("%d", &a[x][y]);
if (rtn == 1)
break;
else if (rtn == EOF) {
fprintf (stderr, "user canceled input.\n");
exit (EXIT_FAILURE);
}
fprintf (stderr, "error: invalid input.\n");
empty_stdin();
}
}
}
}
/* Calculate the total of score for each team.
* return the index for the row with the highest team score
*/
int teamscore (int a[NROW][NCOL])
{
int x, y,
highest = 0,
hi_index = 0,
sum[NROW] = {0};
for (x = 0; x < NROW; x++) {
for (y = 0; y < NCOL; y++)
sum[x] += a[x][y];
if (sum[x] > highest) {
highest = sum[x];
hi_index = x;
}
}
return hi_index;
}
int main (void) {
int hi_index = 0,
teams[NROW] = {0},
ar[NROW][NCOL] = {{0}};
// initialize (ar); /* ar is not a VLA, initialize at declaration */
disp_arr (ar, teams);
input (ar, teams);
disp_arr (ar, teams);
hi_index = teamscore (ar);
printf ("\nThe winning team is Team %d\n", teams[hi_index]);
return 0;
}
示例使用/输出
$ ./bin/team
team 0 : 0 0 0 0
team 0 : 0 0 0 0
team 0 : 0 0 0 0
Enter your team: 123
Enter team[123] score[1]: 10
Enter team[123] score[2]: 12
Enter team[123] score[3]: 14
Enter team[123] score[4]: 16
Enter your team: 138
Enter team[138] score[1]: 12
Enter team[138] score[2]: 14
Enter team[138] score[3]: 16
Enter team[138] score[4]: 18
Enter your team: 187
Enter team[187] score[1]: 11
Enter team[187] score[2]: 13
Enter team[187] score[3]: 15
Enter team[187] score[4]: 17
team 123 : 10 12 14 16
team 138 : 12 14 16 18
team 187 : 11 13 15 17
The winning team is Team 138
无效和取消输入示例
$ ./bin/team
team 0 : 0 0 0 0
team 0 : 0 0 0 0
team 0 : 0 0 0 0
Enter your team: The Mighty Ducks
error: invalid input.
Enter your team: user canceled input.
检查一下,如果我对您的团队类型等的假设有任何错误,请告诉我。如果您还有其他问题,请告诉我。
关于c - 函数中的数组显示不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47586868/
C语言sscanf()函数:从字符串中读取指定格式的数据 头文件: ?
最近,我有一个关于工作预评估的问题,即使查询了每个功能的工作原理,我也不知道如何解决。这是一个伪代码。 下面是一个名为foo()的函数,该函数将被传递一个值并返回一个值。如果将以下值传递给foo函数,
CStr 函数 返回表达式,该表达式已被转换为 String 子类型的 Variant。 CStr(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CSng 函数 返回表达式,该表达式已被转换为 Single 子类型的 Variant。 CSng(expression) expression 参数是任意有效的表达式。 说明 通常,可
CreateObject 函数 创建并返回对 Automation 对象的引用。 CreateObject(servername.typename [, location]) 参数 serv
Cos 函数 返回某个角的余弦值。 Cos(number) number 参数可以是任何将某个角表示为弧度的有效数值表达式。 说明 Cos 函数取某个角并返回直角三角形两边的比值。此比值是
CLng 函数 返回表达式,此表达式已被转换为 Long 子类型的 Variant。 CLng(expression) expression 参数是任意有效的表达式。 说明 通常,您可以使
CInt 函数 返回表达式,此表达式已被转换为 Integer 子类型的 Variant。 CInt(expression) expression 参数是任意有效的表达式。 说明 通常,可
Chr 函数 返回与指定的 ANSI 字符代码相对应的字符。 Chr(charcode) charcode 参数是可以标识字符的数字。 说明 从 0 到 31 的数字表示标准的不可打印的
CDbl 函数 返回表达式,此表达式已被转换为 Double 子类型的 Variant。 CDbl(expression) expression 参数是任意有效的表达式。 说明 通常,您可
CDate 函数 返回表达式,此表达式已被转换为 Date 子类型的 Variant。 CDate(date) date 参数是任意有效的日期表达式。 说明 IsDate 函数用于判断 d
CCur 函数 返回表达式,此表达式已被转换为 Currency 子类型的 Variant。 CCur(expression) expression 参数是任意有效的表达式。 说明 通常,
CByte 函数 返回表达式,此表达式已被转换为 Byte 子类型的 Variant。 CByte(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CBool 函数 返回表达式,此表达式已转换为 Boolean 子类型的 Variant。 CBool(expression) expression 是任意有效的表达式。 说明 如果 ex
Atn 函数 返回数值的反正切值。 Atn(number) number 参数可以是任意有效的数值表达式。 说明 Atn 函数计算直角三角形两个边的比值 (number) 并返回对应角的弧
Asc 函数 返回与字符串的第一个字母对应的 ANSI 字符代码。 Asc(string) string 参数是任意有效的字符串表达式。如果 string 参数未包含字符,则将发生运行时错误。
Array 函数 返回包含数组的 Variant。 Array(arglist) arglist 参数是赋给包含在 Variant 中的数组元素的值的列表(用逗号分隔)。如果没有指定此参数,则
Abs 函数 返回数字的绝对值。 Abs(number) number 参数可以是任意有效的数值表达式。如果 number 包含 Null,则返回 Null;如果是未初始化变量,则返回 0。
FormatPercent 函数 返回表达式,此表达式已被格式化为尾随有 % 符号的百分比(乘以 100 )。 FormatPercent(expression[,NumDigitsAfterD
FormatNumber 函数 返回表达式,此表达式已被格式化为数值。 FormatNumber( expression [,NumDigitsAfterDecimal [,Inc
我是一名优秀的程序员,十分优秀!