-3
public String lowCard() {
    if (card1.getValue() < card2.getValue() && 
            card1.getValue() <card3.getValue() &&
            card1.getValue() < card4.getValue()) {
        return new string (card1.toString());
    }
    
    else (card2.getValue() < card1.getValue() &&
            card2.getValue() < card3.getValue() &&
            card2.getValue() < card4.getValue()) {
        return (card2.toString());
    }
    
    else (card3.getValue() < card1.getValue() &&
            card3.getValue() < card2.getValue() &&
            card3.getValue() < card4.getValue()) {
        return (card3.toString());
    }
    else
        return (card4.toString());
}

It should print.. 3 of clubs 2 of diamonds 5 of clubs 2 of clubs The low card is 2 of diamonds

but it prints... 3 of clubs 2 of diamonds 5 of clubs 2 of clubs The low card is 2 of clubs

Jens
  • 67,715
  • 15
  • 98
  • 113
Jay Khan
  • 5
  • 3
  • Where are these values coming from? String arrays? How are they set? You also cant have two elses like this. – croakPedlar Nov 03 '21 at 18:13
  • 3
    I don't think it prints anything, since this code won't even compile. – David Conrad Nov 03 '21 at 18:13
  • There is more code, the card values are stored as integers further up but I didn’t include that into the question. The app class has the printlns and use of the method. – Jay Khan Nov 03 '21 at 18:31
  • These are very important things to add for debugging. You need to show what type the values are. You need to show something you've actually ran and compiled (bc this 100% does not compile). How are these integers if it's `int` of clubs/diamonds? An int cant store all that info, so how are you creating these cards? – croakPedlar Nov 03 '21 at 19:04
  • I added the entire code below as an answer to the question. – Jay Khan Nov 03 '21 at 19:22

1 Answers1

0

So there's a few things happening here that we can make better.

  1. This code as written does not compile. We can't have 2 elses after an if. else should be the last in the decision tree with no conditions. When we do if/else if/else statements the syntax looks like this:

    if(condition){
       //do something 
    } else if (condition) {
       //do something
    } else {
      //do something
    }
    

Notice the placement of your current braces do not match this syntax.

  1. With that in mind, you need else ifs and you mentioned in a comment that these are integers. So we should change how we return it if we want a string value. We can do String.valueOf(num) or Integer.toString(num) It'll look like this:

    public String lowCard() {
     if ((card1 < card2) && 
         (card1 < card3) &&
         (card1 < card4)) 
     {
         return Integer.toString(card1);
     }
    
     else if ((card2 < card1) && 
              (card2 < card3) &&
              (card2 < card4))  
     {
         return  Integer.toString(card2);
     }
    
     else if ((card3 < card1) && 
              (card3 < card2) &&
              (card3 < card4))  
     {
         return  Integer.toString(card3);
     }
     else
         return  Integer.toString(card4);
     }
    

Now remember that these cards HAVE to be global variables because you haven't passed them into your function. As for where it says "of clubs" or "of diamonds" you got me. That's not listed in the question anywhere. If you complete your question, I can complete this answer.

croakPedlar
  • 202
  • 3
  • 14