Advertisement
porteno

Untitled

Dec 10th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1.  
  2. public class Q3 {
  3. public static void main(String[] args) {
  4. FlashLight[] flashLights = {new FlashLight("bigA", 120),
  5. new FlashLight("bigB", 180),
  6. new FlashLight("bigC", 50),
  7. new FlashLight("bigD", 1),
  8. new FlashLight("bigE", 10),
  9. new FlashLight("bigF", 5),
  10. new FlashLight("bigG", 1000)};
  11. func(flashLights, 301); //wrong, output bigA and bigb
  12. //threeFlashLights(flashLights, 301); //correct answer bigA, bigB, bigD
  13. }
  14. public static void func(FlashLight[] arr, double total){
  15. int count = 0;
  16. for(int i=0; i<arr.length; i++) {
  17. for (int j = 0; j < arr.length; j++) {
  18. if(arr[i].getPrice()+arr[j].getPrice()+arr[j+1].getPrice() == total) {
  19. count++;
  20. if(count==1)
  21. System.out.println(arr[i].getModel() + " " + arr[j].getModel() + " " + arr[j+1].getModel());
  22. }
  23. }
  24. }
  25.  
  26.  
  27. }
  28.  
  29.  
  30.  
  31. public static void threeFlashLights(FlashLight[] arr, double total){
  32.  
  33. FlashLight flsh1 = arr[0], flsh2= arr[0], flsh3= arr[0];
  34. boolean runLoop = true;
  35. for (int i = 0; i < arr.length-2 && runLoop; i++) {
  36. flsh1 = arr[i];
  37. for (int j = i+1; j < arr.length-1 && runLoop ; j++) {
  38. flsh2 = arr[j];
  39. for (int k = j+1; k < arr.length && runLoop; k++) {
  40. flsh3 = arr[k];
  41. if(flsh1.getPrice() + flsh2.getPrice() + flsh3.getPrice() == total)
  42. runLoop = false;
  43. }
  44. }
  45. }
  46. if(!runLoop)
  47. System.out.println(flsh1.getModel() + " " + flsh2.getModel() + " " + flsh3.getModel());
  48. else
  49. System.out.println("nothing found");
  50. }
  51.  
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement