Advertisement
Guest User

Untitled

a guest
Feb 5th, 2015
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.88 KB | None | 0 0
  1. // My code - gives 90/100
  2. import java.text.MessageFormat;
  3. import java.util.Scanner;
  4.  
  5. public class DozensEggs {
  6.     private static final Integer DAYS_OF_WEEK = 7;
  7.     private static final Integer DOZEN = 12;
  8.  
  9.     public static void main(String[] args) {
  10.         Scanner input = new Scanner(System.in);
  11.         long eggsTotal = 0;
  12.         long currentDayEggs = 0;
  13.         for (int i = 0; i < DAYS_OF_WEEK; i++) {
  14.             String[] eggsForDay = input.nextLine().split(" ");
  15.             currentDayEggs = Integer.parseInt(eggsForDay[0]);
  16.             if (eggsForDay[1].toLowerCase().contains("dozen")) {
  17.                 currentDayEggs *= DOZEN;
  18.             }
  19.  
  20.             eggsTotal += currentDayEggs;
  21.         }
  22.  
  23.         long dozens = eggsTotal / DOZEN;
  24.         long eggs = eggsTotal % DOZEN;
  25.  
  26.         System.out.println(MessageFormat.format("{0} dozens + {1} eggs", dozens, eggs));
  27.  
  28.         input.close();
  29.     }
  30. }
  31. -----------------------------------------------
  32. // 100/100 author code
  33. import java.util.Scanner;
  34.  
  35. public class DozenEggs {
  36.  
  37.     public static void main(String[] args) {
  38.        
  39.         //initialize variables
  40.         Scanner str = new Scanner(System.in);
  41.         int eggs = 0;
  42.         int dozens = 0;
  43.  
  44.        
  45.         for (int i = 0; i < 7; i++) {
  46.            
  47.             //read the input for actual "eggs" and "dozens"
  48.             String input = str.nextLine();
  49.             String[] currLine = input.split(" ");
  50.             int currEggs = 0;
  51.            
  52.             //check the input if it is "eggs" or "dozens"
  53.             if (currLine[1].toLowerCase().contains("dozen")) {
  54.                 currEggs = Integer.parseInt(currLine[0]) * 12;
  55.             } else if (currLine[1].toLowerCase().contains("egg")) {
  56.                 currEggs = Integer.parseInt(currLine[0]);
  57.             }
  58.            
  59.             //adding the input to total sum of "eggs"
  60.             eggs += currEggs;
  61.         }
  62.        
  63.         //calculating "eggs" and "dozens"
  64.         dozens = eggs / 12;
  65.         eggs = eggs % 12;
  66.        
  67.         System.out.println(dozens + " dozens + " + eggs + " eggs");
  68.     }
  69.  
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement