0

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.

shubhs
  • 170
  • 6
Andy Reutzel
  • 33
  • 1
  • 1
  • 5

3 Answers3

1

It appears that your problem may be related to the way that you are comparing scores at the end of your code.

You could greatly simplify things by comparing the dog score to the others first (as dog wins all ties), then compare the dragon score to the cat score (as cat loses all ties), as follows:

if (dogScore >= catScore && dogScore >= dragonScore) {
    y = "dog";
}
else if (dragonScore >= catScore) {
    y = "dragon";
}
else {
    y = "cat";
}

This way you can get rid of z completely and just focus on comparing the actual scores for each animal.

I hope this helps but let me know if it's not clear.

Martin
  • 16,093
  • 1
  • 29
  • 48
  • This was a massive help. Identified the exact problem I was having with it running. Thank you so much. That makes complete sense. I really need to start working on simplifying my code. – Andy Reutzel Feb 24 '14 at 11:22
  • Don't beat yourself up too much, we all learnt to code once upon a time :) – Martin Feb 24 '14 at 11:22
1

There are three separate problems in your code.

The first has already been covered by Martin.

The second is that you are comparing strings using ==. This checks that the strings are actually the same String objects, not that the strings have the same value. You should use surveyResponse.equals("a").

The third you have already identified yourself in the duplicated code. Conversion to a loop is the correct way forwards.

Tim B
  • 40,716
  • 16
  • 83
  • 128
0

There are two major issues with Your code that make it work incorrectly. First of all, You can't use'==' to compare objects in Java (this compares object references), instead use equals (as people have pointed out earlier).

Secondly, your selection of 'z' is faulty.

if (dogScore > z){
z = dogScore;}
if (catScore > z){
z = catScore;}
if (dragonScore > z){
z = dragonScore;}

Here z points to first highest score, if dogScore == catScore, z already points to dog, so the following lines are unnecessary, moreover they break your logic.

if (dogScore == catScore){
z = dogScore;}

This will change z to dogScore, if person has selected dragon in all quiestions (as dogScore == 0 and catScore == 0)

if (catScore == dragonScore){
z = dragonScore;}

same as above, but z will be dragonScore if someone always picked dog.

endriu_l
  • 1,649
  • 16
  • 20