java计算J2EE应用程序中对象的出现次数
我是新手
我试图统计J2EE web应用程序中对象的出现次数。很简单
我有一个Oracle数据库,有一个名为Warnings的表。每个警告可以有三种状态之一:
- 活跃的
- 自动清除
- 清除
我的表中有一个已抑制的警告列和一个已取代的警告列。这些列所包含的值随后用于确定警告的状态。 没有“警告状态”列,因此警告的状态计算如下:
注意-我想指出,我目前有5个警告对象,其中一个已清除。因此,我有4个活动警告和1个清除警告
我在servlet中有一些逻辑:
for (Warning warning : warnings)
{
// logic to check and count occurrences of each type of warning
int activeCount = 0;
int clearedCount = 0;
int autoclearedCount = 0;
// warning is active if it is not superseded and not suppressed
if( ((warning.getWarningSuperseded() != null && warning.getWarningSuperseded().equals("N"))
&& ((warning.getWarningSuppressed() != null && warning.getWarningSuppressed().equals("N")))))
{
activeCount++;
}
// warning is cleared if it is superseded
if(warning.getWarningSuperseded() != null && warning.getWarningSuperseded().equals("Y"))
{
clearedCount++;
}
// warning is autocleared if it is suppressed
else if (warning.getWarningSuppressed() != null && warning.getWarningSuppressed().equals("Y"))
{
autoclearedCount++;
}
}
所以,我想我在这里要做的是,在伪代码中:
if warning not superseded and warning not suppressed
then increment activeCount;
if warning is superseded
then increment clearedCount;
if warning is suppressed
then increment autoclearedCount;
在Eclipse中调试时,我将activeCount设置为8,clearedCount设置为3,autoclearedCount设置为1。 我不太明白为什么会这样,因为就我而言,我只有5个警告对象,4个激活,1个清除
然后我所做的只是检查我是否有我认为我有多少警告:
warningCount = warningServices.getWarningCount(MyServletHelper.getCurrentSchema(request));
这将返回4-getWarningCount()方法仅返回活动警告的计数
我只能假设我的逻辑中存在某种缺陷——我不知道这是否只是因为我度过了漫长的一天或什么,但这对我来说毫无意义
有时候,只要有一双新的眼睛,我就能看到我做错了什么
# 1 楼答案
除了gvo的回答之外,Tom,您应该在if块中使用continue操作符,或者您可以正确地使用if-else-elseif。 即:
相应地更改代码,并让我们知道结果
[已编辑] 你写的代码。。。从技术上讲,如果封盖是一个通行证,程序控制可以进入两个
# 2 楼答案
您确定只有5个警告对象吗?你是如何得到你提到的价值观的
您应该尝试调试(System.out.println)以了解代码的工作方式
但是第一件真正让我吃惊的事情是,你在循环中重新初始化了你的值
每次输入新警告时,都会丢失以前的值。。所以我甚至不明白,在循环结束时,值怎么会比值大
按照问题中的编辑:调试时尝试使用更简单的东西。我不知道
但我相信
在靠近计数器++的ifs中。,循环之后。 有时候,越简单越好。 值错误的原因可能不在您显示的代码示例中