概述
第四章第十一题(十进制转十六进制)(Decimal to hex)
-
*4.11(十进制转十六进制)编写一个程序,提示用户输入0~15之间的一个整数,显示其对应的十六进制数。对于不正确的输入数字,提示非法输入。
下面是一个运行示例:
Enter a decimal value (0 to 15): 11
The hex value is B
Enter a decimal value (0 to 15): 5
The hex value is 5
Enter a decimal value (0 to 15): 31
31 is an invalid input*4.11(Decimal to hex) Write a program that prompts the user to enter decimal digits and displays its corresponding hex value.
Here are some sample runs:
Enter a decimal value (0 to 15): 11
The hex value is B
Enter a decimal value (0 to 15): 5
The hex value is 5
Enter a decimal value (0 to 15): 31
31 is an invalid input -
参考代码:
package chapter04;
import java.util.Scanner;
public class Code_11 {
public static void main(String[] args) {
int deciNum;
System.out.print("Enter a decimal value (0 to 15): ");
Scanner input = new Scanner(System.in);
deciNum = input.nextInt();
if(0 <= deciNum && deciNum <= 9)
System.out.println("The hex value is " + deciNum);
else if(10 <= deciNum && deciNum <= 15)
System.out.printf("The hex value is %c", (deciNum - 10 + 'A'));
else
System.out.println(deciNum + " is an invalid input");
input.close();
}
}
- 结果显示:
Enter a decimal value (0 to 15): 11
The hex value is B
Process finished with exit code 0
最后
以上就是整齐招牌为你收集整理的第四章第十一题(十进制转十六进制)(Decimal to hex)第四章第十一题(十进制转十六进制)(Decimal to hex)的全部内容,希望文章能够帮你解决第四章第十一题(十进制转十六进制)(Decimal to hex)第四章第十一题(十进制转十六进制)(Decimal to hex)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复