Brent
Brent

Reputation: 21

If and if else again

The problem I seem to be having is that I am unsure on how to make the program recognize if the player is in one of the "MVP" positions (C,SS,CF) before moving forward with my program logic.

These three positions qualify for "MVP" status only if their OPS is above 800 for everyone else, it has to be 900 or above to be considered for "MVP". Once again, I am having trouble with the "if" and "if else" statement. Again, This IS school given problem and I don't want the answer. Only insight into what I am doing wrong. I want to learn this not have it done for me. Thank you in advance.

import java.util.Scanner;

public class baseBall {

    public static void main(String[] args) {

        /* A good part of a baseball player's offensive value is captured by the    
    statistic OPS - the sum of the player's on base percentage and slugging 
    percentage. An MVP candidate will typically have an OPS above 900,
    however if they play a difficult defensive position (catcher, shortstop, or center field)
    then they are an MVP candidate if their OPS is above 800. Write a program that will prompt the user for
    a player's name, their on base percentage, slugging percentage, and defensive position and report whether the player is an MVP candidate*/


        Scanner input = new Scanner(System.in);
        System.out.println("Please enter players name: ");
        String name = input.next();
        System.out.println("Please enter On Base Percentage: ");
        double Obp = input.nextDouble();
        System.out.println("Please enter slugging Percentage: ");
        double Slg = input.nextDouble();
        System.out
                .println("Please enter position (P,C,1B,2B,3B,SS,LF,CF,RF): ");
        String ball = input.next();

        String position; 
        double Ops = Obp + Slg;

        if ( position.equalsIgnoreCase("ss")){
            if (position.equalsIgnoreCase("cf")){
                if (position.equalsIgnoreCase("c")){
                    System.out.println("MVP Candidate!");
            else
                 System.out.println("NOT an MVP Candidate!);

                }
            }
        }
    }
}

Upvotes: 2

Views: 78

Answers (3)

Marcello Davi
Marcello Davi

Reputation: 433

Your code is checking if the player position is c AND ss AND cf. But the player will be only in one position, it can't be in all the three, so your code will always print "Not an MVP Candidate!".

To make it simple your code is checking if the position is c AND ss AND CF, instead you want to check if the position is c OR ss OR cf.

So you have to use the conditional operator OR -> ||:

if(position.equalsIgnoreCase("ss") || position.equalsIgnoreCase("cf") || position.equalsIgnoreCase("c") {
     System.out.println("MVP Candidate!");
} else {
     System.out.println("NOT an MVP Candidate!);
}

Upvotes: 1

gian1200
gian1200

Reputation: 3864

Try doing it without nested IFs. Instead, try using Boolean operators like AND and OR.

As previously stated try using a paper first. Try drawing a decision tree to see what and where it might be going wrong.

Upvotes: 1

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

Your nested ifs will never be true. Think about this logically and on paper.

If position equals ss how can it equal cf and c at the same time? Always ask yourself: "does this make sense?" -- do this before committing code to screen and you'll be golden.

As a side recommendation, please get rid of those distracting // from your question text. They serve no purpose other than to annoy.

Upvotes: 0

Related Questions