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

问题描述 投票:0回答:1

我们正在执行以下编程练习: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,但是程序的行为相同。

我们已阅读:

java algorithm int hex logic
1个回答
0
投票

您的代码行

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

“吃”在日和月中的前导零,您可以将其替换为诸如]

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

使其按预期工作。

© www.soinside.com 2019 - 2024. All rights reserved.