【发布时间】:2018-04-28 13:16:37
【问题描述】:
我正在尝试以下代码。
int index, use, comp;
for (index = 0; index < 3; index++)
{
if (user1.equalsIgnoreCase(options[index]))
{
use = index;
}
}
for (index = 0; index < 3; index++)
{
if (opt.equalsIgnoreCase(options[index]))
{
comp = add + index;
}
}
int sum = comp + use;
在int sum = comp + use; 行,我收到一条错误消息,指出变量 comp 和 use 未初始化。如何将执行循环期间获得的值存储在这些变量中?
【问题讨论】:
-
if不是循环 -
int index = 0, use = 0, comp = 0;应该让它工作。问题是 if 语句不能保证运行,在这种情况下,use变量不会有赋值。