下面我创建了一个进入while循环的函数。在while循环中,调用if语句来遍历列表并检查数字以查看哪一个是最大的,哪一个是最小的。当我运行程序时,只调用一个printf()
,并且不止一次打印相同的printf()
。它似乎选择两个数字并在相同的printf()
函数下打印它们。我知道firstNumber = firstNumber->next;
应该遍历列表。不是secondNumber = secondNumber->next->next;
应该指向列表中的下一个数字吗?
typedef struct A_NewNumber {
struct A_NewNumber *next;
double newNum;
} NewNumber;
void NumberSize(NewNumber *start) {
NewNumber *firstNumber = start;
NewNumber *secondNumber = start;
if(start != NULL) {
while (secondNumber != NULL && secondNumber->next != NULL) {
secondNumber = secondNumber->next->next;
firstNumber = firstNumber->next;
if(secondNumber > firstNumber) {
printf("The biggest number is:%lf \n",secondNumber->newNum);
} else {
printf("The smallest number is:%lf \n",firstNumber->newNum);
}
firstNumber = firstNumber->next;
}
}
}
完整程序的示例运行:
Please enter a number or
'quit' to stop or 'print' to print/calculate
12
Please enter a number or
'quit' to stop or 'print' to print/calculate
13
Please enter a number or
'quit' to stop or 'print' to print/calculate
14
Please enter a number or
'quit' to stop or 'print' to print/calculate
15
Please enter a number or
'quit' to stop or 'print' to print/calculate
16
Please enter a number or
'quit' to stop or 'print' to print/calculate
17
Please enter a number or
'quit' to stop or 'print' to print/calculate
print
Numbers:12.000000
Numbers:13.000000
Numbers:14.000000
Numbers:15.000000
Numbers:16.000000
Numbers:17.000000
The biggest number is:14.000000
The biggest number is:16.000000
The smallest number is:17.000000
答案 0 :(得分:2)
if()
内的比较应该是值,而不是指针。
if( num1 > num2) {
// Do something.
} else {
// Do something else.
}
这是因为您在firstNumber = firstNumber->next;
循环中执行了两次while()
语句。
firstNumber = firstNumber->next;
firstNumber = firstNumber->next;
这会使secondNumber == firstNumber;
答案 1 :(得分:0)
你能说明结果是什么吗?
我认为问题在于PreparedStatement.setString()
和firstNumber
的分配。他们似乎确实指向同一个对象。因此,修改一个将修改另一个。而且,我认为跟踪列表的头部是安全的。我会做这样的事情:
secondNumber
我不确定这是不是你想要实现的......