从 int 转换为十六进制字符串以检查它是否包含单词

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

我们正在进行以下编程练习:Death by Coffee

主要任务是将整数转换为十六进制并添加值。我们编写了以下代码:

public class Dinglemouse {

  public static int[] coffeeLimits /*☕*/ (final int year, final int month, final int day) {
    System.out.println("year: "+year);
    System.out.println("month: "+month);
    System.out.println("day: "+day);

    long healthNumber = Long.parseLong(String.valueOf(year)+String.valueOf(month)+String.valueOf(day));
    System.out.println("healthNumber: "+healthNumber);

    long cafe = Long.valueOf("CAFE",16);
    long decaf = Long.valueOf("DECAF",16);

    int cafeLimit = getLimit(cafe, healthNumber);
    System.out.println("\ncafeLimit: "+cafeLimit);
    int decafLimit = getLimit(decaf, healthNumber);
    System.out.println("\ndecafLimit: "+decafLimit);

    return new int []{cafeLimit, decafLimit};
  }

  public static int getLimit(long coffee, long healthNumber){
    int limit=0;
    while(limit<=5000 && !Long.toHexString(healthNumber).contains("dead")){
      limit++;
      healthNumber+=coffee;
      System.out.println("new healthNumber: "+Long.toHexString(healthNumber));
    }
    return limit>5000 ? 0 : limit;    
  }

}

我们想知道为什么代码通过了以下测试,exJohn 除外:

import org.junit.Test;
import static org.junit.Assert.*;
import java.util.*;

public class Tests {

  // Show returned limits
  private static int[] show(final int y, final int m, final int d, final int[] result) {
    System.out.println(String.format("%4d%02d%02d -> ",y,m,d)+Arrays.toString(result));
    return result;
  }

  @Test
  public void exJohn() {
    final int y=1950, m=1, d=19;
    assertArrayEquals(new int[]{2645,1162}, show(y,m,d,Dinglemouse.coffeeLimits(y,m,d)));
  }

  @Test
  public void exSusan() {
    final int y=1965, m=12, d=11;
    assertArrayEquals(new int[]{111,0}, show(y,m,d,Dinglemouse.coffeeLimits(y,m,d)));
  }

  @Test
  public void exElizabeth() {
    final int y=1964, m=11, d=28;
    assertArrayEquals(new int[]{0,11}, show(y,m,d,Dinglemouse.coffeeLimits(y,m,d)));
  }

  @Test
  public void exPeter() {
    final int y=1965, m=9, d=4;
    assertArrayEquals(new int[]{0,0}, show(y,m,d,Dinglemouse.coffeeLimits(y,m,d)));
  }

}

我们已经打印了我们在 getLimit 的循环中得到的内容,我们看到咖啡馆限制输出的最后一个 healthNumber 是:

...
new healthNumber: 2dc4cbb
new healthNumber: 2dd17b9
new healthNumber: 2dde2b7
new healthNumber: 2deadb5

cafeLimit: 889

对于无咖啡因的限制,我们有:

...
new healthNumber: 10ff8a241
new healthNumber: 110068ef0
new healthNumber: 110147b9f
new healthNumber: 11022684e

decafLimit: 0

那么我们代码的结果是: [889, 0]

什么时候应该: [2645, 1162]

我们认为这可能是由 int 溢出引起的,所以我们将所有变量都更改为 long,但程序的行为是相同的。

我们已阅读:

你的代码行

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

"eats" 日和月前导零,你可以用

之类的东西代替
long healthNumber = year*10000+month*100+day;

使其按预期工作。