当前位置: 代码迷 >> 综合 >> 第五章第三十八题(十进制转换八进制)(Decimal to octal)
详细解决方案
第五章第三十八题(十进制转换八进制)(Decimal to octal)
热度:4 发布时间:2024-03-06 04:48:26.0
第五章第三十八题(十进制转换八进制)(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.
参考代码:
package chapter05;import java.util.Scanner;publicclassCode_38{
publicstaticvoidmain(String[] args){
String octalString ="";int decimalNumber;System.out.print("Enter a decimal integer: ");Scanner inputScanner =newScanner(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);}}
结果显示:
Enter a decimal integer:45
Its corresponding octal value is 55
Process finished with exit code 0