Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main(String[] args) {
- //Create a Scanner
- Scanner input = new Scanner(System.in);
- //Prompt user to enter any decimal number
- System.out.println("Enter a decimal number, please: ");
- int decNum = input.nextInt();
- int division = decNum;
- String allDigits = "";
- //Calculate the digitZero
- allDigits += setDigit(division);
- //Calculate the other digits
- do{
- division = division / 16;
- allDigits+=setDigit(division);
- } while (division >= 16);
- //Reverse the string into a normal hexadecimal view
- allDigits = new StringBuilder(allDigits).reverse().toString();
- //Display the result
- System.out.println("" + allDigits);
- }
- public static String setDigit(int Number) {
- int division = Number;
- int digit = division % 16;
- String hexDigits = "";
- if (digit > 9) {
- switch (digit) {
- case 10:
- hexDigits += "A";
- case 11:
- hexDigits += "B";
- case 12:
- hexDigits += "C";
- case 13:
- hexDigits += "D";
- case 14:
- hexDigits += "E";
- case 15:
- hexDigits += "F";
- }
- } else {
- hexDigits += Integer.toString(digit);
- }
- return hexDigits;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement