我在解决这个班级问题时遇到了很多麻烦。我的预产期是明天,我仍然不知道该怎么做。当用户输入的输入转换为二进制、八进制和十六进制时,我编写了一个代码。现在,问题是现在他们要求我以用户输入双精度或浮点数并将其转换为十进制的方式修改代码。我最大的问题是使用十进制数。例如,5* .987 *。我将留下我已经创建的代码。如果有人可以提供帮助,我将不胜感激!谢谢 :)
import java.util.Scanner;
class EncodingTester {
public static void main(String args[]) {
byte largestPositiveByte = 127;
short largestPositiveShort = 32767;
int largestPositiveInt = 2147483647;
long largestPositiveLong = 9223372036854775807L;
long largestPositiveLongPlusOne = 9223372036854775807L;
Scanner in = new Scanner(System.in);
System.out.println("Next number (0 to stop): ");
long nextNumber = in.nextLong();
while (nextNumber != 0) {
int radix;
double logBase2 = Math.log(nextNumber) / Math.log(2);
double absoluteNumOfBits = 1 + Math.floor(logBase2);
double maxBits = Math.max(8, absoluteNumOfBits);
double numBits = Math.log(maxBits) / Math.log(2);
int newNumBits = (int) Math.pow(2, Math.ceil(numBits));
System.out.println("The absolute number of bits is: " +absoluteNumOfBits +". The final number of bits is: " +newNumBits +".");
radix = 10;
System.out.println("Decimal: " + String.format("%s", Double.toString(nextNumber)).replace(' ','0'));
radix = 2;
System.out.println("Binary: " + String.format("%"+newNumBits+"s", Long.toString(nextNumber,radix)).replace(' ','0'));
radix = 8;
System.out.println("Octal: " + String.format("%"+((int) Math.ceil(newNumBits/3.0))+"s", Long.toString(nextNumber,radix)).replace(' ','0'));
radix = 16;
System.out.println("Hexadecimal: 0x" + String.format("%"+newNumBits/4+"s", Long.toString(nextNumber,radix)).replace(' ','0'));
System.out.println("");
System.out.println("Next number: (0 to stop)");
nextNumber = in.nextLong();
}
System.out.println("Good Bye");
}
}