Skip to content

Instantly share code, notes, and snippets.

@StaniTr
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StaniTr/0809cca28e4283149eaf to your computer and use it in GitHub Desktop.
Save StaniTr/0809cca28e4283149eaf to your computer and use it in GitHub Desktop.
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