Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // My code - gives 90/100
- import java.text.MessageFormat;
- import java.util.Scanner;
- public class DozensEggs {
- private static final Integer DAYS_OF_WEEK = 7;
- private static final Integer DOZEN = 12;
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- long eggsTotal = 0;
- long currentDayEggs = 0;
- for (int i = 0; i < DAYS_OF_WEEK; i++) {
- String[] eggsForDay = input.nextLine().split(" ");
- currentDayEggs = Integer.parseInt(eggsForDay[0]);
- if (eggsForDay[1].toLowerCase().contains("dozen")) {
- currentDayEggs *= DOZEN;
- }
- eggsTotal += currentDayEggs;
- }
- long dozens = eggsTotal / DOZEN;
- long eggs = eggsTotal % DOZEN;
- System.out.println(MessageFormat.format("{0} dozens + {1} eggs", dozens, eggs));
- input.close();
- }
- }
- -----------------------------------------------
- // 100/100 author code
- import java.util.Scanner;
- public class DozenEggs {
- public static void main(String[] args) {
- //initialize variables
- Scanner str = new Scanner(System.in);
- int eggs = 0;
- int dozens = 0;
- for (int i = 0; i < 7; i++) {
- //read the input for actual "eggs" and "dozens"
- String input = str.nextLine();
- String[] currLine = input.split(" ");
- int currEggs = 0;
- //check the input if it is "eggs" or "dozens"
- if (currLine[1].toLowerCase().contains("dozen")) {
- currEggs = Integer.parseInt(currLine[0]) * 12;
- } else if (currLine[1].toLowerCase().contains("egg")) {
- currEggs = Integer.parseInt(currLine[0]);
- }
- //adding the input to total sum of "eggs"
- eggs += currEggs;
- }
- //calculating "eggs" and "dozens"
- dozens = eggs / 12;
- eggs = eggs % 12;
- System.out.println(dozens + " dozens + " + eggs + " eggs");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement