marking2112

Juice Diet

Jul 20th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.35 KB | None | 0 0
  1. import java.text.DecimalFormat;
  2. import java.util.Scanner;
  3.  
  4. public class TrainingDemo {
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.  
  8.         int raspberries = Integer.valueOf(scanner.nextLine());
  9.         int strawberries = Integer.valueOf(scanner.nextLine());
  10.         int cherries = Integer.valueOf(scanner.nextLine());
  11.         double juiceAllowed = Double.valueOf(scanner.nextLine());
  12.  
  13.         double juice = 0;
  14.         int ras = 0;
  15.         int str = 0;
  16.         int chr = 0;
  17.  
  18.         for (int raspberry = 0; raspberry <= raspberries; raspberry++) {
  19.             for (int strawberry = 0; strawberry <= strawberries; strawberry++) {
  20.                 for (int cherry = 0; cherry <= cherries; cherry++) {
  21.                     double juiceMade = raspberry * 4.5 + strawberry * 7.5 + cherry * 15.0;
  22.                     if (juiceMade <= juiceAllowed && juice < juiceMade) {
  23.                         juice = juiceMade;
  24.                         chr = cherry;
  25.                         ras = raspberry;
  26.                         str = strawberry;
  27.                     }
  28.                 }
  29.             }
  30.         }
  31.         DecimalFormat df = new DecimalFormat("0.##");
  32.  
  33.         System.out.println(String.format("%d Raspberries, %d Strawberries, %d Cherries. Juice: %s ml.", ras, str, chr, df.format(juice)));
  34.     }
  35. }
Add Comment
Please, Sign In to add comment