在Java中不断收到“可能的从双精度到整数的有损转换”错误

时间:2019-01-24 04:33:08

标签: java

> Task :compileTestGroovy FAILED
file or directory '/Users/abc/Documents/code/dev/practice-service/src/test/groovy', not found
file or directory '/Users/abc/Documents/code/dev/practice-service/src/test/groovy', not found
Task ':compileTestGroovy' is not up-to-date because:
  Task has failed previously.
Excluding []
file or directory '/Users/abc/Documents/code/dev/practice-service/src/test/groovy', not found
Starting process 'Gradle Worker Daemon 1'. Working directory: /Users/abc/.gradle/workers Command: /Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/Home/bin/java -Djava.security.manager=worker.org.gradle.process.internal.worker.child.BootstrapSecurityManager -Dfile.encoding=UTF-8 -Duser.country=GB -Duser.language=en -Duser.variant -cp /Users/abc/.gradle/caches/4.7/workerMain/gradle-worker.jar worker.org.gradle.process.internal.worker.GradleWorkerMain 'Gradle Worker Daemon 1'
Successfully started process 'Gradle Worker Daemon 1'
Started Gradle worker daemon (0.458 secs) with fork options DaemonForkOptions{executable=/Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/Home/bin/java, minHeapSize=null, maxHeapSize=null, jvmArgs=[], classpath=[/Users/abc/.gradle/caches/modules-2/files-2.1/org.codehaus.groovy/groovy-ant/2.4.15/2faf2c49f25ce9868b2eed1aa7dc1ecd98d33353/groovy-ant-2.4.15.jar, /Users/abc/.gradle/caches/modules-2/files-2.1/org.codehaus.groovy/groovy-groovydoc/2.4.15/7bace00f98542934e10c069d067df6e77e17f6cd/groovy-groovydoc-2.4.15.jar, /Users/abc/.gradle/caches/modules-2/files-2.1/org.codehaus.groovy/groovy-templates/2.4.15/b5cbe971f2950452c1d431ea0135dd8de2d5f58e/groovy-templates-2.4.15.jar, /Users/abc/.gradle/caches/modules-2/files-2.1/org.codehaus.groovy/groovy-xml/2.4.15/40865f59469e6b0808c9c304bd17038997220c72/groovy-xml-2.4.15.jar, /Users/abc/.gradle/caches/modules-2/files-2.1/org.codehaus.groovy/groovy/2.4.15/74b7e0b99526c569e3a59cb84dbcc6204d601ee6/groovy-2.4.15.jar, /Users/abc/.gradle/caches/modules-2/files-2.1/org.apache.ant/ant-junit/1.9.4/b063757dacaa716b643c2f9270e469183636ff11/ant-junit-1.9.4.jar, /Users/abc/.gradle/caches/modules-2/files-2.1/org.apache.ant/ant/1.9.4/6d473e8653d952045f550f4ef225a9591b79094a/ant-1.9.4.jar, /Users/abc/.gradle/caches/modules-2/files-2.1/org.apache.ant/ant-launcher/1.9.4/334b62cb4be0432769679e8b94e83f8fd5ed395c/ant-launcher-1.9.4.jar, /Users/abc/.gradle/caches/modules-2/files-2.1/org.apache.ant/ant-antlr/1.9.4/4cf30025981dc2344324a0ef0e40063c3ef08f0a/ant-antlr-1.9.4.jar, /Users/abc/.gradle/wrapper/dists/gradle-4.7-all/4cret0dgl5o3b21weaoncl7ys/gradle-4.7/lib/ant-1.9.9.jar, /Users/abc/.gradle/wrapper/dists/gradle-4.7-all/4cret0dgl5o3b21weaoncl7ys/gradle-4.7/lib/ant-launcher-1.9.9.jar], keepAliveMode=SESSION}.
Compiling with JDK Java compiler API.
/Users/abc/Documents/code/dev/practice-service/build/generated-test-sources/contracts/org/springframework/cloud/contract/verifier/tests/ContractVerifierTest.java:3: error: package com.mycompany.practice.base.producer does not exist
import com.mycompany.practice.base.producer.PracticeMessagingBase;
                                               ^
/Users/abc/Documents/code/dev/practice-service/build/generated-test-sources/contracts/org/springframework/cloud/contract/verifier/tests/ContractVerifierTest.java:16: error: cannot find symbol
public class ContractVerifierTest extends PracticeMessagingBase {
                                          ^
  symbol: class PracticeMessagingBase
/Users/abc/Documents/code/dev/practice-service/build/generated-test-sources/contracts/org/springframework/cloud/contract/verifier/tests/ContractVerifierTest.java:24: error: cannot find symbol
                        process();
                        ^
  symbol:   method process()
  location: class ContractVerifierTest
3 errors
startup failed:
Compilation failed; see the compiler error output for details.

1 error

:compileTestGroovy (Thread[Task worker for ':',5,main]) completed. Took 1.807 secs.

这是我要添加的功能,但我不断遇到这些错误:

public static int calcCarbonFootprint ( double[] person ) {

    double kgCO2;
    double metricTons;
    kgCO2 = (double)(calcTransit(person[3]));
    kgCO2 += (double)calcAutoOwnership(person[2]);
    kgCO2 += (double)calcAutoUsage(person[0],person[1]);
    metricTons = kgCO2/KGS_PER_METRIC_TON;

    return (int)metricTons;
}

我尝试了许多不同的方法,但最终都遇到了相同的错误。我能得到一些指示吗?

1 个答案:

答案 0 :(得分:0)

该错误是由于尝试将具有64位的double数字传递给具有32位的int参数引起的。

您可以更改calc*方法的参数以接受double而不是int。这样,计算将尽可能精确。

...,也可以将person[n]强制转换为intdouble类型编号中的一些数字数据以及计算方法的准确性都会消失。

示例代码:https://ideone.com/ghgRo9