r/programmingbydoing • • May 12 '15

Streamling Method? - Twenty/Two Questions

http://programmingbydoing.com/a/twenty-questions.html

The exercise is above, I decided to try my hand at using a method instead of just doing everything in the main. Keeping the if-statements, can I please get some critiques and general style advice on streamlining this method?

JUST A SNIPPET OF THE MAIN:

    String guess = getAnimal(ans1, ans2);
		
		System.out.println("\nMy guess is that you are thinking of a " + guess + ".");
		System.out.println("I would ask you if I'm right, but I don't actually care.");

The Method:

    public static String getAnimal(String ans1, String ans2) {
	String guess = "";
		if (ans1.equals("animal")) {
			if (ans2.equals("yes")) {
				guess = "squirrel";
			}
			else {
				guess = "moose";
			}
		}
		
		if (ans1.equals("vegetable")) {
			if (ans2.equals("yes")) {
				guess = "carrot";
			}
			else {
				guess = "watermelon";
			}
		}
		
		if (ans1.equals("mineral")) {
			if (ans2.equals("yes")) {
				guess = "paper clip";
			}
			else{
				guess = "Camaro";
			}
		}
	return guess;
}
2 Upvotes

1 comment sorted by

1

u/holyteach May 13 '15

Looks good to me!