diff options
author | Jake Wharton <jakewharton@gmail.com> | 2018-04-23 14:22:09 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-04-23 14:22:09 -0400 |
commit | 9ea843d73170f75d735114cdca6ba2463965cafb (patch) | |
tree | 183003ca1ee2a049593dd9b02742ff1534425334 | |
parent | 8e9b2ec3c3e9d09e19c88ef3d1fc92ab2f6472e2 (diff) | |
download | javapoet-9ea843d73170f75d735114cdca6ba2463965cafb.tar.gz |
Initialize multiplication result to 1.
Otherwise the result is always 0.
-rw-r--r-- | README.md | 4 |
1 files changed, 2 insertions, 2 deletions
@@ -104,7 +104,7 @@ we want to make the operation and range configurable. Here's a method that gener private MethodSpec computeRange(String name, int from, int to, String op) { return MethodSpec.methodBuilder(name) .returns(int.class) - .addStatement("int result = 0") + .addStatement("int result = 1") .beginControlFlow("for (int i = " + from + "; i < " + to + "; i++)") .addStatement("result = result " + op + " i") .endControlFlow() @@ -117,7 +117,7 @@ And here's what we get when we call `computeRange("multiply10to20", 10, 20, "*") ```java int multiply10to20() { - int result = 0; + int result = 1; for (int i = 10; i < 20; i++) { result = result * i; } |