gpt4 book ai didi

似乎无法让我的 While 语句发挥作用

转载 作者:行者123 更新时间:2023-11-30 20:23:20 24 4
gpt4 key购买 nike

所以我试图编写一个程序,要求用户输入一个单词或短语,然后给出该单词的拼字游戏值。我遇到的问题是,当用户仅输入字母“q”或“Q”作为单词/短语时,程序的 while 循环需要关闭。

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string>
#include<conio.h>

这是决定字母值的函数

int scrabbleValue(char* word)
{

int value = 0;
int scrabbleTot = 0;
int index = 0;

while (*(word + index) != '\0')
{
switch (toupper(*(word + index)))
{
case 'A':
case 'E':
case 'I':
case 'L':
case 'N':
case 'O':
case 'R':
case 'S':
case 'T':
case 'U':
value = 1;
break;
case 'D':
case 'G':
value = 2;
break;
case 'B':
case 'C':
case 'M':
case 'P':
value = 3;
break;
case 'F':
case 'H':
case 'V':
case 'W':
case 'Y':
value = 4;
break;
case 'K':
value = 5;
break;
case 'J':
case 'X':
value = 8;
break;
case 'Q':
case 'Z':
value = 10;
break;
}
scrabbleTot += value;
*word++;
}
return scrabbleTot;

}

很好的主要陈述

int main()
{
char *ptr;
char check;
int wordvalue;

char name[100];

printf("\nEnter a word :");

scanf("%s", name);

ptr = name;
check = name[0];
wordvalue = scrabbleValue(ptr);
printf("Your word value is: %d\n", wordvalue);

这就是我遇到麻烦的地方。我似乎无法让这个 while 循环正常工作。我尝试了 name[0] 和“q”的字符串比较,但它无法编译。

    while (!strcmp() || !strcmp(name, "Q"))
{
printf("\nEnter a word :");
scanf("%s", name);
ptr = name;

wordvalue = scrabbleValue(ptr);
printf("Your word value is: %d\n", wordvalue);
}
return 0;

}

最佳答案

您可能需要这个主要功能。它更简单,没有无用的变量,也没有重复的代码。

int main()
{
int wordvalue;
char name[100];

while (1)
{
printf("\nEnter a word :");
scanf("%s", name);

if (strcmp(name, "Q") == 0)
break;

wordvalue = scrabbleValue(name);
printf("Your word value is: %d\n", wordvalue);
}

return 0;
}

这是未经测试的代码,可能无法编译,并且可能存在拼写错误。

关于似乎无法让我的 While 语句发挥作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36337100/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com