I am very new to programming. I understand that loops would make this look a lot more efficient. But for right now, I just need to figure out how to return the correct value. I need to figure out the best animal based on their value. The highest value should be returned.
I have changed the code to .equals on the comparing in the statements so now surveyresponse.equals("answer") It hasn't solved the problems for me.
The code is currently running, but only returns dragon as the answer.
- I need the code to return the animal that has the highest value.
The assignment calls for that dog wins all ties, and cats always lose the tie breaker.
import java.util.Scanner; public class PetSurvey{ public static void main(String[] args){ String surveyResponse; String y; int dogScore, catScore, dragonScore, z; dogScore = 0; catScore = 0; dragonScore = 0; z = 0; y = "best animal"; Scanner foo = new Scanner( System.in ); System.out.print(" What is your favorite pet? "); System.out.print(" a) Dogs "); System.out.print(" b) Cats "); System.out.print(" c) Dragons "); System.out.print(" d) I like cats, dogs and dragons "); surveyResponse = foo.next(); if (surveyResponse == "a"){ dogScore = dogScore + 3;} if (surveyResponse == "b"){ catScore = catScore + 3;} if (surveyResponse == "c"){ dragonScore = dragonScore + 3;} if (surveyResponse == "d"){ dogScore = dogScore + 1; catScore = catScore + 1; dragonScore = dragonScore +1;} System.out.print(" What is your favorite pet? "); System.out.print(" a) Dogs "); System.out.print(" b) Cats "); System.out.print(" c) Dragons "); System.out.print(" d) I like cats, dogs and dragons "); surveyResponse = foo.next(); if (surveyResponse == "a"){ dogScore = dogScore + 3;} if (surveyResponse == "b"){ catScore = catScore + 3;} if (surveyResponse == "c"){ dragonScore = dragonScore + 3;} if (surveyResponse == "d"){ dogScore = dogScore + 1; catScore = catScore + 1; dragonScore = dragonScore +1;} System.out.print(" What is your favorite pet? "); System.out.print(" a) Dogs "); System.out.print(" b) Cats "); System.out.print(" c) Dragons "); System.out.print(" d) I like cats, dogs and dragons "); surveyResponse = foo.next(); if (surveyResponse == "a"){ dogScore = dogScore + 3;} if (surveyResponse == "b"){ catScore = catScore + 3;} if (surveyResponse == "c"){ dragonScore = dragonScore + 3;} if (surveyResponse == "d"){ dogScore = dogScore + 1; catScore = catScore + 1; dragonScore = dragonScore +1;} if (dogScore > z){ z = dogScore;} if (catScore > z){ z = catScore;} if (dragonScore > z){ z = dragonScore;} if (dogScore == catScore){ z = dogScore;} if (catScore == dragonScore){ z = dragonScore;} if (z == dogScore){ y = "dog";} if (z == catScore){ y = "cat";} if (z == dragonScore){ y = "dragon";} System.out.print(" The most popular pet is: " + y + "."); } }
I would appreciate any help on this, thanks in advance.