Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import java.util.Scanner;
public class Exam_Problem1_DozenEggs {
public static void main(String[] args) {
//keeps num of eggs
int eggs=0;
//keeps num of dozens;
int dozens=0;
//keeps sum of eggs;
int sumEggs=0;
//keeps sum of dozens
int sumDozens=0;
//
String egg="eggs";
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < 7; i++) {
String text = scanner.nextLine();
//split numbers from string ("eggs" or "dozens")
String[] words = text.split(" ");
//check if the input is eggs
if ("eggs".equals(words[1])){
//add the number of eggs in [eggs]
eggs=eggs+Integer.parseInt(words[0]);
}
else {
//if no eggs then dozens->add num of dozens
dozens=dozens+Integer.parseInt(words[0]);
}
}
//result
sumEggs=eggs%12;
//dozens are equal to dozens+dozens from eggs (egss/12)
sumDozens=eggs/12+dozens;
System.out.println(sumDozens+" dozens + "+sumEggs+" eggs");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.