我花了3个小时试图让这段代码工作,但是每当我尝试结束在一个循环和控制台连续循环。我已经尝试了所有的方法-我创建了一个函数,它只返回I并重新分配值,但似乎不起作用。
出于某种原因,每当我尝试使用逻辑运算符时,它都不想对我起作用。mes->H and[x]的值类似于“2D、4D、3C、5C、6H、7H”等,而西装则是“H”、“C”、“D”、“S”等。
int newtrick_value(struct Message *mes, int suit) {
int len = strlen(mes->hand);
int x = 0, i = 50, y = 0;
while (i < 63) {
while(x < len) {
if (((mes->hand[x] == i || mes->hand[x] == y) && mes->hand[x+1] == suit)){
if(i>= 58){
return y;
}
return i;
}
x++;
continue;
}
if(i >= 57) {
y = determine_letter(i);
}
i ++;
x = 0;
continue;
}
return 0;
}
最佳答案
作为一个MWE,它将“calling determine letter”输出到控制台6次:
#include <stdio.h>
#include <string.h>
int newtrick_value(char* hand, int suit) {
int len = strlen(hand);
int x = 0, i = 50, y = 0;
while (i < 63)
{
x = 0;
while(x < len)
{
printf("i%d, y%d, suit %d, hand[x] %d, hand[x+1] %d\n", i, y, suit, hand[x], hand[x+1]);
if (((hand[x] == i || hand[x] == y) && hand[x+1] == suit))
{
if(i>= 58)
{
return y;
}
return i;
}
x++;
}
if(i >= 57)
{
printf("Calling determine letter\n");
//y = determine_letter(i);
}
i++;
}
return 0;
}
int main(void)
{
newtrick_value("2H,3H", (int)"H");
return 0;
}
据我所知,我没有改变任何逻辑(只是一些格式)。它不应该做出这种行为,因为西装和价值应该匹配。
奇怪的是,指纹显示了以下信息:
i50, y0, suit 134514349, hand[x] 50, hand[x+1] 72
i50, y0, suit 134514349, hand[x] 72, hand[x+1] 44
i50, y0, suit 134514349, hand[x] 44, hand[x+1] 51
i50, y0, suit 134514349, hand[x] 51, hand[x+1] 72
i50, y0, suit 134514349, hand[x] 72, hand[x+1] 0
i51, y0, suit 134514349, hand[x] 50, hand[x+1] 72
i51, y0, suit 134514349, hand[x] 72, hand[x+1] 44
i51, y0, suit 134514349, hand[x] 44, hand[x+1] 51
i51, y0, suit 134514349, hand[x] 51, hand[x+1] 72
i51, y0, suit 134514349, hand[x] 72, hand[x+1] 0
i52, y0, suit 134514349, hand[x] 50, hand[x+1] 72
i52, y0, suit 134514349, hand[x] 72, hand[x+1] 44
i52, y0, suit 134514349, hand[x] 44, hand[x+1] 51
i52, y0, suit 134514349, hand[x] 51, hand[x+1] 72
i52, y0, suit 134514349, hand[x] 72, hand[x+1] 0
i53, y0, suit 134514349, hand[x] 50, hand[x+1] 72
i53, y0, suit 134514349, hand[x] 72, hand[x+1] 44
i53, y0, suit 134514349, hand[x] 44, hand[x+1] 51
i53, y0, suit 134514349, hand[x] 51, hand[x+1] 72
i53, y0, suit 134514349, hand[x] 72, hand[x+1] 0
i54, y0, suit 134514349, hand[x] 50, hand[x+1] 72
i54, y0, suit 134514349, hand[x] 72, hand[x+1] 44
i54, y0, suit 134514349, hand[x] 44, hand[x+1] 51
i54, y0, suit 134514349, hand[x] 51, hand[x+1] 72
i54, y0, suit 134514349, hand[x] 72, hand[x+1] 0
i55, y0, suit 134514349, hand[x] 50, hand[x+1] 72
i55, y0, suit 134514349, hand[x] 72, hand[x+1] 44
i55, y0, suit 134514349, hand[x] 44, hand[x+1] 51
i55, y0, suit 134514349, hand[x] 51, hand[x+1] 72
i55, y0, suit 134514349, hand[x] 72, hand[x+1] 0
i56, y0, suit 134514349, hand[x] 50, hand[x+1] 72
i56, y0, suit 134514349, hand[x] 72, hand[x+1] 44
i56, y0, suit 134514349, hand[x] 44, hand[x+1] 51
i56, y0, suit 134514349, hand[x] 51, hand[x+1] 72
i56, y0, suit 134514349, hand[x] 72, hand[x+1] 0
i57, y0, suit 134514349, hand[x] 50, hand[x+1] 72
i57, y0, suit 134514349, hand[x] 72, hand[x+1] 44
i57, y0, suit 134514349, hand[x] 44, hand[x+1] 51
i57, y0, suit 134514349, hand[x] 51, hand[x+1] 72
i57, y0, suit 134514349, hand[x] 72, hand[x+1] 0
Calling determine letter
i58, y0, suit 134514349, hand[x] 50, hand[x+1] 72
i58, y0, suit 134514349, hand[x] 72, hand[x+1] 44
i58, y0, suit 134514349, hand[x] 44, hand[x+1] 51
i58, y0, suit 134514349, hand[x] 51, hand[x+1] 72
i58, y0, suit 134514349, hand[x] 72, hand[x+1] 0
Calling determine letter
i59, y0, suit 134514349, hand[x] 50, hand[x+1] 72
i59, y0, suit 134514349, hand[x] 72, hand[x+1] 44
i59, y0, suit 134514349, hand[x] 44, hand[x+1] 51
i59, y0, suit 134514349, hand[x] 51, hand[x+1] 72
i59, y0, suit 134514349, hand[x] 72, hand[x+1] 0
Calling determine letter
i60, y0, suit 134514349, hand[x] 50, hand[x+1] 72
i60, y0, suit 134514349, hand[x] 72, hand[x+1] 44
i60, y0, suit 134514349, hand[x] 44, hand[x+1] 51
i60, y0, suit 134514349, hand[x] 51, hand[x+1] 72
i60, y0, suit 134514349, hand[x] 72, hand[x+1] 0
Calling determine letter
i61, y0, suit 134514349, hand[x] 50, hand[x+1] 72
i61, y0, suit 134514349, hand[x] 72, hand[x+1] 44
i61, y0, suit 134514349, hand[x] 44, hand[x+1] 51
i61, y0, suit 134514349, hand[x] 51, hand[x+1] 72
i61, y0, suit 134514349, hand[x] 72, hand[x+1] 0
Calling determine letter
i62, y0, suit 134514349, hand[x] 50, hand[x+1] 72
i62, y0, suit 134514349, hand[x] 72, hand[x+1] 44
i62, y0, suit 134514349, hand[x] 44, hand[x+1] 51
i62, y0, suit 134514349, hand[x] 51, hand[x+1] 72
i62, y0, suit 134514349, hand[x] 72, hand[x+1] 0
Calling determine letter
因此,通过将“H”转换为整数而给出的合适值似乎特别怪异。
我已经假设了你在你的结构中是如何表示
mes->hand
的,但希望这仍然有效。不过,我注意到一件事,那就是你为什么把这件诉讼当作一件int
,而不是一件char
?这似乎是一件更合乎逻辑的事情。因此,如果你进入一个无限循环,我怀疑
mes->hand
可能不会以空结尾,这意味着strlen(mes->hand)
将返回一些未知值,这意味着while(x < len)
似乎永远不会终止。您能检查一下mes->hand
以确保它是一个有效的(并且是预期的)字符串吗?