将Double与方法中计算得出的Double值相等会得出0.0?可能是什么问题呢?

时间:2019-02-22 15:27:41

标签: java if-statement casting double

对于这些表达式:

J

我得到以下结果:

#include <winsock.h>
#include <Windows.h>
#include <stdio.h>

void ExecCmd() {
    HANDLE hPipeRead;
    HANDLE hPipeWrite;
    SECURITY_ATTRIBUTES saAttr = { sizeof(SECURITY_ATTRIBUTES) };
    STARTUPINFO si = { sizeof(STARTUPINFO) };
    PROCESS_INFORMATION pi = { 0 };
    BOOL fSuccess;
    BOOL bProcessEnded = FALSE;
    size_t total = 0;

    saAttr.bInheritHandle = TRUE;
    saAttr.lpSecurityDescriptor = NULL;

    if (!CreatePipe(&hPipeRead, &hPipeWrite, &saAttr, 0)) {
        return;
    }

    si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
    si.hStdOutput = hPipeWrite;
    si.hStdError = hPipeWrite;
    si.wShowWindow = SW_HIDE; 

    WCHAR k[] = L"cmd.exe /u /c net user";
    fSuccess = CreateProcessW(NULL, k, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);
    if (!fSuccess) {
        CloseHandle(hPipeWrite);
        CloseHandle(hPipeRead);
        return;
    }


    for (; !bProcessEnded;) {
        bProcessEnded = WaitForSingleObject(pi.hProcess, 50) == WAIT_OBJECT_0;

        while (TRUE) {
            DWORD dwRead = 0;
            DWORD dwAvail = 0;
            WCHAR *buffer = NULL;

            if (!PeekNamedPipe(hPipeRead, NULL, NULL, NULL, &dwAvail, NULL)) {
                break;
            }

            if (!dwAvail) {
                break;
            }

            buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (dwAvail + 2));
            if (buffer == NULL) {
                break;
            }

            if (!ReadFile(hPipeRead, buffer, dwAvail, &dwRead, NULL) || !dwRead) {
                break;
            }

            total += dwRead;

        }

    }
    CloseHandle(hPipeWrite);
    CloseHandle(hPipeRead);
    CloseHandle(pi.hProcess);
    CloseHandle(pi.hThread);
    return;

} 

int main() {
    ExecCmd();
    return 0;
}

这是返回一个Double的calculateQuantity()方法,但是此方法可以从输出中看到:

     if (lim != null) {


         for (int i = 0; i < itemlist.size(); i++) {              

            Double itemquantity = calculateQuantity(itemlist.get(i));

            Integer limitvalue = lim.getValue();

            System.out.println("CALCULATED VALUE IS: " + calculateQuantity(itemlist.get(i)));

            System.out.println("ITEMQUANTITY IS " + itemquantity);

            System.out.println("LIMIT VALUE IS " + limitvalue);

           if (itemquantity < limitvalue) {
               System.out.println("QUANTITY " + itemquantity + " vs LIMIT " + limitvalue);
           below.add(itemlist.get(i).getName());
        }}

所以我的问题是,当我尝试将calculateQuantity()的结果与Double值相等时为什么会得到0?并且如果语句起作用,CALCULATED VALUE IS: **210.0** ITEMQUANTITY IS **0.0** LIMIT VALUE IS **500** QUANTITY 0.0 vs LIMIT 500 又该如何工作(前者应该是Double / double,而后者应该是Integer / int)?

2 个答案:

答案 0 :(得分:1)

最有可能在其下的分支

    if(query.uniqueResult() == null || query.list().isEmpty())

正在射击。这意味着您的查询存在问题,超出了此问题的范围。

以下方法应该起作用:

    if(itemquantity < limitvalue)

因为Java支持不同数值类型之间的比较而无需显式转换。 double和int都在该类别中。

答案 1 :(得分:0)

第一次运行时可能会出现HibernateException,将一些打印语句放入catch(HibernateException e)块中,然后查看。