我只需要使用标题中提到的循环将给定数字从十进制转换为十六进制。循环的意思是no。到目前为止,一切都像转换683一样。它打印2AB没问题,34821打印8805.问题是需要两位数之间的整数。例如,我无法将31388转换为7A9C。我知道我的问题是什么,但我不知道如何解决它。
import java.util.Scanner;
public class Lab12abHexNumbersTEST
{
public static void main(String[] args)
{
Scanner decimal = new Scanner(System.in);
System.out.println("Enter a decimal number between 1 and 50,000: ");
int num = decimal.nextInt();
String hex1 = "";
String letter = "";
String hex3 = "";
String hex_last = "";
int dec = num;
while (dec > 0)
{
hex1 = (dec % 16) + hex1;
if(dec % 16 >= 9){
hex1 = "";
if(dec % 16 == 15) {
letter = "F" + letter + hex1;
} else if (dec % 16 == 14) {
letter = "E" + letter + hex1;
} else if (dec % 16 == 13) {
letter = "D" + letter + hex1;
} else if (dec % 16 == 12) {
letter = "C" + letter + hex1;
} else if (dec % 16 == 11) {
letter = "B" + letter + hex1;
} else if (dec % 16 == 10) {
letter = "A" + letter + hex1;
}
System.out.println(letter);
}
hex3 = hex1 + letter;
dec = dec / 16;
}
hex_last = hex3;
System.out.println("The decimal number " + num + " in hexadecimal is "
+ hex_last);
}
}
我已经提供了解决这个问题的方法。
注意:我使用switch语句使代码更清晰,但我确定你知道如何将它们转换回if / else情况。如果您需要帮助,请询问。
注意:我直接使用StringBuilder而不是字符串。您可以轻松地将这些更改为字符串,但我选择了stringbuilder,因为它再次使代码更清晰。
public static void main(String[] args) {
int dec = 31388 ;
StringBuilder hexString = new StringBuilder();
while (dec != 0){
int hexDigit = dec%16; // obtain this remainder
dec /= 16; //update dec
if (hexDigit < 10) hexString.insert(0, hexDigit);
else {
switch (hexDigit) {
case 10 :
hexString.insert(0, "A");
break;
case 11 :
hexString.insert(0, "B");
break;
case 12 :
hexString.insert(0, "C");
break;
case 13 :
hexString.insert(0, "D");
break;
case 14 :
hexString.insert(0, "E");
break;
case 15 :
hexString.insert(0, "F");
break;
default: break;
}
}
} // while
System.out.println(hexString);
}
输出:7A9C
根据请求转换为if / else块:
else {
if (hexDigit == 10) {
hexString.insert(0, "A");
} else if (hexDigit == 11) {
hexString.insert(0, "B");
} else if (hexDigit == 12) {
hexString.insert(0, "C");
} else if (hexDigit == 13) {
hexString.insert(0, "D");
} else if (hexDigit == 14) {
hexString.insert(0, "E");
} else if (hexDigit == 15) {
hexString.insert(0, "F");
}
}
使用字符串,解决方案是:
public static void main(String[] args) {
int dec = 31388;
String answer = "";
while (dec != 0){
int hexDigit = dec%16; // obtain this remainder
dec /= 16; //update dec
if (hexDigit < 10) {
answer = hexDigit + answer;
}
else {
if (hexDigit == 10) {
answer = "A" + answer;
} else if (hexDigit == 11) {
answer = "B" + answer;
} else if (hexDigit == 12) {
answer = "C" + answer;
} else if (hexDigit == 13) {
answer = "D" + answer;
} else if (hexDigit == 14) {
answer = "E" + answer;
} else if (hexDigit == 15) {
answer = "F" + answer;
}
}
} // while
System.out.println(answer);
}
如果允许String函数:
import java.util.*;
public static void main(String[] args){
int dec = 31388 ;
String digits = "0123456789ABCEDF";
StringBuilder hexString = new StringBuilder();
while(dec != 0){
hexString.insert(0, digits.indexOf(dec % 16));
dec /= 16;
}
System.out.println(hexString);
}