Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. public static void main(String[] args) {
  2.  
  3. //Create a Scanner
  4. Scanner input = new Scanner(System.in);
  5.  
  6. //Prompt user to enter any decimal number
  7. System.out.println("Enter a decimal number, please: ");
  8. int decNum = input.nextInt();
  9.  
  10. int division = decNum;
  11. String allDigits = "";
  12.  
  13. //Calculate the digitZero
  14. allDigits += setDigit(division);
  15. //Calculate the other digits
  16. do{
  17. division = division / 16;
  18. allDigits+=setDigit(division);
  19. } while (division >= 16);
  20.  
  21. //Reverse the string into a normal hexadecimal view
  22. allDigits = new StringBuilder(allDigits).reverse().toString();
  23. //Display the result
  24. System.out.println("" + allDigits);
  25. }
  26.  
  27. public static String setDigit(int Number) {
  28. int division = Number;
  29. int digit = division % 16;
  30. String hexDigits = "";
  31. if (digit > 9) {
  32. switch (digit) {
  33. case 10:
  34. hexDigits += "A";
  35. case 11:
  36. hexDigits += "B";
  37. case 12:
  38. hexDigits += "C";
  39. case 13:
  40. hexDigits += "D";
  41. case 14:
  42. hexDigits += "E";
  43. case 15:
  44. hexDigits += "F";
  45. }
  46. } else {
  47. hexDigits += Integer.toString(digit);
  48. }
  49. return hexDigits;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement