Guest User

Untitled

a guest
Dec 12th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. public static void main(String[] args) {
  2.  
  3.  
  4. for (int i=1; i<=5; i++) {
  5. System.out.println("Enter input:");
  6. Scanner input = new Scanner(System.in);
  7. String value = input.nextLine();
  8. System.out.println("Enter target:");
  9. int target = input.nextInt();
  10. input.close();
  11.  
  12. while (value.length() != 4)
  13. value = ("0" + value);
  14.  
  15. int a = value.charAt(0)-'0';
  16. int b = value.charAt(1)-'0';
  17. int c = value.charAt(2)-'0';
  18. int d = value.charAt(3)-'0';
  19.  
  20. int combo1 = a + b + c + d;
  21. int combo2 = (a*1000) + (b*100) + (c*10) + d;
  22. int combo3 = a + (b*100) + (c*10) + d;
  23. int combo4 = ((a*10) + b) + ((c*10) + d);
  24. int combo5 = ((a*100) + (b*10) + c + d);
  25. int combo6 = a + (b*10 + c) + d;
  26. int combo7 = ((a*10) + b) + c + d;
  27. int combo8 = a + b + ((c*10)) + d;
  28.  
  29. int best = 0;
  30.  
  31.  
  32.  
  33. if (combo1 < target) {
  34. best = combo1;
  35. }
  36.  
  37. if (combo2 < target && combo2 > best) {
  38. best = combo2;
  39. }
  40.  
  41. if (combo3 < target && combo3 > best) {
  42. best = combo3;
  43. }
  44.  
  45. if (combo4 < target && combo4 > best) {
  46. best = combo4;
  47. }
  48.  
  49. if (combo5 < target && combo5 > best) {
  50. best = combo5;
  51. }
  52.  
  53. if (combo6 < target && combo6 > best) {
  54. best = combo6;
  55. }
  56.  
  57. if (combo7 < target && combo7 > best) {
  58. best = combo7;
  59. }
  60.  
  61. if (combo8 < target && combo8 > best) {
  62. best = combo8;
  63. }
  64.  
  65.  
  66.  
  67. System.out.println(best);
  68.  
  69. }
  70.  
  71. }
Add Comment
Please, Sign In to add comment