English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Dans ce programme, vous apprendrez à utiliser les fonctions en Java pour convertir un nombre octal en décimal et inversement.
public class DecimalOctal { public static void main(String[] args) { int decimal = 78; int octal = convertDecimalToOctal(decimal); System.out.printf("%d Decimal = %d Octal", decimal, octal); } public static int convertDecimalToOctal(int decimal) { int octalNumber = 0, i = 1; while (decimal != 0) { octalNumber += 8) * i; decimal /= 8; i *= 10; } return octalNumber; } }
When running the program, the output is:
78 Decimal = 116 Octal
This conversion occurs to:
8 | 788 | 9 -- 6 8 | 1 -- 1 8 | 0 -- 1 (116)
public class OctalDecimal { public static void main(String[] args) { int octal = 116; int decimal = convertOctalToDecimal(octal); System.out.printf("%d octal = %ddecimal", octal, decimal); } public static int convertOctalToDecimal(int octal) { int decimalNumber = 0, i = 0; while(octal != 0) { decimalNumber += (octal % 10) * Math.pow(8, i); ++i; octal/=10; } return decimalNumber; } }
When running the program, the output is:
116 Octal = 78 Decimal
This conversion occurs to:
1 * 82 + 1 * 81 + 6 * 80 = 78