我是靠谱客的博主 缓慢画笔,这篇文章主要介绍第五章第三十八题(十进制转换八进制)(Decimal to octal)第五章第三十八题(十进制转换八进制)(Decimal to octal),现在分享给大家,希望可以做个参考。

第五章第三十八题(十进制转换八进制)(Decimal to octal)

  • **5.38(十进制转换为八进制)编写程序,提示用户输入一个十进制整数,然后显示对应的八进制值。在这个程序中不要使用Java的Integer.toOctalString(int)方法。
    **5.38(Decimal to octal) Write a program that prompts the user to enter a decimal integer and displays its corresponding octal value. Don’t use Java’s Integer.toOctalString(int) in this program.
  • 参考代码:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package chapter05; import java.util.Scanner; public class Code_38 { public static void main(String[] args) { String octalString = ""; int decimalNumber; System.out.print("Enter a decimal integer: "); Scanner inputScanner = new Scanner(System.in); decimalNumber = inputScanner.nextInt(); do { octalString = decimalNumber % 8 + octalString; decimalNumber /= 8; }while(decimalNumber > 0); System.out.printf("Its corresponding octal value is %s", octalString); } }
  • 结果显示:
复制代码
1
2
3
4
5
Enter a decimal integer: 45 Its corresponding octal value is 55 Process finished with exit code 0

最后

以上就是缓慢画笔最近收集整理的关于第五章第三十八题(十进制转换八进制)(Decimal to octal)第五章第三十八题(十进制转换八进制)(Decimal to octal)的全部内容,更多相关第五章第三十八题(十进制转换八进制)(Decimal内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(84)

评论列表共有 0 条评论

立即
投稿
返回
顶部