我是靠谱客的博主 整齐招牌,这篇文章主要介绍第四章第十一题(十进制转十六进制)(Decimal to hex)第四章第十一题(十进制转十六进制)(Decimal to hex),现在分享给大家,希望可以做个参考。
第四章第十一题(十进制转十六进制)(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内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复