Write a Java method getBinaryString(int toCalc) that takes as argument a (strictly) positive integer toCalc, and returns the binary representation of toCalc as a String. For instance, if toCalc = 13, the method should return the String "1101", since 13 = 1*2^3 + 1*2^2 + 0*2^1 + 1*2^0.
时间: 2023-04-07 21:00:32 浏览: 91
可以回答这个问题。下面是 Java 代码:
```
public static String getBinaryString(int toCalc) {
if (toCalc == 0) {
return "0";
}
StringBuilder sb = new StringBuilder();
while (toCalc > 0) {
sb.append(toCalc % 2);
toCalc /= 2;
}
return sb.reverse().toString();
}
```
阅读全文