从整数转换为十六进制字符串以检查是否包含一个单词

huangapple go评论94阅读模式
英文:

Convert from int to hexadecimal string to check if it contains a word

问题

  1. 我们正在做以下的编程练习[Death by Coffee][1]
  2. 主要任务是将整数转换为十六进制并进行值的相加我们编写了以下代码
  3. public class Dinglemouse {
  4. public static int[] coffeeLimits /*☕*/ (final int year, final int month, final int day) {
  5. System.out.println("year: "+year);
  6. System.out.println("month: "+month);
  7. System.out.println("day: "+day);
  8. long healthNumber = Long.parseLong(String.valueOf(year)+String.valueOf(month)+String.valueOf(day));
  9. System.out.println("healthNumber: "+healthNumber);
  10. long cafe = Long.valueOf("CAFE",16);
  11. long decaf = Long.valueOf("DECAF",16);
  12. int cafeLimit = getLimit(cafe, healthNumber);
  13. System.out.println("\ncafeLimit: "+cafeLimit);
  14. int decafLimit = getLimit(decaf, healthNumber);
  15. System.out.println("\ndecafLimit: "+decafLimit);
  16. return new int []{cafeLimit, decafLimit};
  17. }
  18. public static int getLimit(long coffee, long healthNumber){
  19. int limit=0;
  20. while(limit<=5000 && !Long.toHexString(healthNumber).contains("dead")){
  21. limit++;
  22. healthNumber+=coffee;
  23. System.out.println("new healthNumber: "+Long.toHexString(healthNumber));
  24. }
  25. return limit>5000 ? 0 : limit;
  26. }
  27. }
  28. 我们想知道为什么代码通过以下的测试除了 exJohn
  29. import org.junit.Test;
  30. import static org.junit.Assert.*;
  31. import java.util.*;
  32. public class Tests {
  33. // Show returned limits
  34. private static int[] show(final int y, final int m, final int d, final int[] result) {
  35. System.out.println(String.format("%4d%02d%02d -> ",y,m,d)+Arrays.toString(result));
  36. return result;
  37. }
  38. @Test
  39. public void exJohn() {
  40. final int y=1950, m=1, d=19;
  41. assertArrayEquals(new int[]{2645,1162}, show(y,m,d,Dinglemouse.coffeeLimits(y,m,d)));
  42. }
  43. @Test
  44. public void exSusan() {
  45. final int y=1965, m=12, d=11;
  46. assertArrayEquals(new int[]{111,0}, show(y,m,d,Dinglemouse.coffeeLimits(y,m,d)));
  47. }
  48. @Test
  49. public void exElizabeth() {
  50. final int y=1964, m=11, d=28;
  51. assertArrayEquals(new int[]{0,11}, show(y,m,d,Dinglemouse.coffeeLimits(y,m,d)));
  52. }
  53. @Test
  54. public void exPeter() {
  55. final int y=1965, m=9, d=4;
  56. assertArrayEquals(new int[]{0,0}, show(y,m,d,Dinglemouse.coffeeLimits(y,m,d)));
  57. }
  58. }
  59. 我们已经打印出在 `getLimit` 循环内部得到的内容我们发现咖啡限制的最后一个 `healthNumber` 输出为
  60. ...
  61. new healthNumber: 2dc4cbb
  62. new healthNumber: 2dd17b9
  63. new healthNumber: 2dde2b7
  64. new healthNumber: 2deadb5
  65. cafeLimit: 889
  66. 而对于无咖啡因限制我们有
  67. ...
  68. new healthNumber: 10ff8a241
  69. new healthNumber: 110068ef0
  70. new healthNumber: 110147b9f
  71. new healthNumber: 11022684e
  72. decafLimit: 0
  73. 因此我们的代码结果是
  74. [889, 0]
  75. 而实际上应该是
  76. [2645, 1162]
  77. 我们认为可能是整数溢出导致的因此我们将所有变量都更改为 long但程序的行为仍然相同
  78. 我们已阅读过
  79. - [https://stackoverflow.com/questions/9321553/java-convert-integer-to-hex-integer](https://stackoverflow.com/questions/9321553/java-convert-integer-to-hex-integer)
  80. - [https://stackoverflow.com/questions/5258415/how-to-get-a-hex-value-from-a-decimal-integer-in-java](https://stackoverflow.com/questions/5258415/how-to-get-a-hex-value-from-a-decimal-integer-in-java)
  81. - [https://stackoverflow.com/questions/11194513/convert-hex-string-to-int](https://stackoverflow.com/questions/11194513/convert-hex-string-to-int)
  82. [1]: [https://www.codewars.com/kata/57db78d3b43dfab59c001abe](https://www.codewars.com/kata/57db78d3b43dfab59c001abe)
英文:

we are doing the following programming exercise: Death by Coffee.

The main task is to convert from integers to hexadecimals and add values. We have written the following code:

  1. public class Dinglemouse {
  2. public static int[] coffeeLimits /*☕*/ (final int year, final int month, final int day) {
  3. System.out.println(&quot;year: &quot;+year);
  4. System.out.println(&quot;month: &quot;+month);
  5. System.out.println(&quot;day: &quot;+day);
  6. long healthNumber = Long.parseLong(String.valueOf(year)+String.valueOf(month)+String.valueOf(day));
  7. System.out.println(&quot;healthNumber: &quot;+healthNumber);
  8. long cafe = Long.valueOf(&quot;CAFE&quot;,16);
  9. long decaf = Long.valueOf(&quot;DECAF&quot;,16);
  10. int cafeLimit = getLimit(cafe, healthNumber);
  11. System.out.println(&quot;\ncafeLimit: &quot;+cafeLimit);
  12. int decafLimit = getLimit(decaf, healthNumber);
  13. System.out.println(&quot;\ndecafLimit: &quot;+decafLimit);
  14. return new int []{cafeLimit, decafLimit};
  15. }
  16. public static int getLimit(long coffee, long healthNumber){
  17. int limit=0;
  18. while(limit&lt;=5000 &amp;&amp; !Long.toHexString(healthNumber).contains(&quot;dead&quot;)){
  19. limit++;
  20. healthNumber+=coffee;
  21. System.out.println(&quot;new healthNumber: &quot;+Long.toHexString(healthNumber));
  22. }
  23. return limit&gt;5000 ? 0 : limit;
  24. }
  25. }

We wonder why the code passes the following tests, except exJohn:

  1. import org.junit.Test;
  2. import static org.junit.Assert.*;
  3. import java.util.*;
  4. public class Tests {
  5. // Show returned limits
  6. private static int[] show(final int y, final int m, final int d, final int[] result) {
  7. System.out.println(String.format(&quot;%4d%02d%02d -&gt; &quot;,y,m,d)+Arrays.toString(result));
  8. return result;
  9. }
  10. @Test
  11. public void exJohn() {
  12. final int y=1950, m=1, d=19;
  13. assertArrayEquals(new int[]{2645,1162}, show(y,m,d,Dinglemouse.coffeeLimits(y,m,d)));
  14. }
  15. @Test
  16. public void exSusan() {
  17. final int y=1965, m=12, d=11;
  18. assertArrayEquals(new int[]{111,0}, show(y,m,d,Dinglemouse.coffeeLimits(y,m,d)));
  19. }
  20. @Test
  21. public void exElizabeth() {
  22. final int y=1964, m=11, d=28;
  23. assertArrayEquals(new int[]{0,11}, show(y,m,d,Dinglemouse.coffeeLimits(y,m,d)));
  24. }
  25. @Test
  26. public void exPeter() {
  27. final int y=1965, m=9, d=4;
  28. assertArrayEquals(new int[]{0,0}, show(y,m,d,Dinglemouse.coffeeLimits(y,m,d)));
  29. }
  30. }

We have printed what we get inside getLimit's loop, and we see that the last healthNumber being output for cafe limit is:

  1. ...
  2. new healthNumber: 2dc4cbb
  3. new healthNumber: 2dd17b9
  4. new healthNumber: 2dde2b7
  5. new healthNumber: 2deadb5
  6. cafeLimit: 889

And for the decaf limit we have:

  1. ...
  2. new healthNumber: 10ff8a241
  3. new healthNumber: 110068ef0
  4. new healthNumber: 110147b9f
  5. new healthNumber: 11022684e
  6. decafLimit: 0

So then our code's result is:
[889, 0]

When it should be:
[2645, 1162]

We thought it could be caused by int overflow so we changed all variables to long, however the program behaves equal.

We have read:

答案1

得分: 0

以下是您要求的翻译内容:

您的代码行

  1. long healthNumber = Long.parseLong(String.valueOf(year)+String.valueOf(month)+String.valueOf(day));

中的 "eats" 掉了日期和月份前面的零,您可以将其替换为类似以下方式:

  1. long healthNumber = year*10000+month*100+day;

以使其按预期工作。

英文:

Your line of code

  1. long healthNumber = Long.parseLong(String.valueOf(year)+String.valueOf(month)+String.valueOf(day));

"eats" leading zeroes in day and month, you can replace it with something like

  1. long healthNumber = year*10000+month*100+day;

to make it work as expected.

huangapple
  • 本文由 发表于 2020年4月10日 17:34:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/61137539.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定