我认为这是编译器的问题。最初,我使用dev c ++,它崩溃了。然后,我使用了代码块,但是在终端上什么也没显示。我想知道这是否是Windows 10问题。
#include <stdio.h>
#include <string.h>
#define NUM_PLANETS 9
int main(int argc, char *argv[]) {
char *planets[] = {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn",
"Uranus", "Neptune", "Pluto"};
int i, j;
for (i = 1; i < argc; i++) {
for (j = 0; j < NUM_PLANETS; j++) {
if (strcmp(argv[i], planets[j]) == 0) {
printf("%s is planet %d\n", argv[i], j + 1);
break;
}
if (j == NUM_PLANETS)
printf("%s is not a planet.\n", argv[i]);
}
}
return 0;
}
答案 0 :(得分:0)
以下建议的代码:
现在是建议的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NUM_PLANETS 9
int main(int argc, char *argv[])
{
char *planets[] =
{
"Mercury",
"Venus",
"Earth",
"Mars",
"Jupiter",
"Saturn",
"Uranus",
"Neptune",
"Pluto"
};
int i;
int j;
if (argc < 2 )
{
printf( "USAGE: %s <planet name> ...\n", argv[0] );
exit( EXIT_FAILURE );
}
for (i = 1; i < argc; i++)
{
for (j = 0; j < NUM_PLANETS; j++)
{
if (strcmp(argv[i], planets[j]) == 0)
{
printf("%s is planet %d\n", argv[i], j + 1);
break;
}
}
if (j == NUM_PLANETS)
printf("%s is not a planet.\n", argv[i]);
}
return 0;
}