dimanche 1 mai 2016

What is wrong in my if and else statemenht?

I am fairly new to java and still practice. I did the test run of my exercise and the "The player is already in the team" will occur 3 times for what ever string input for variable newplayers if option equals y. Please help me, thank you!

import java.util.ArrayList;
import java.util.Scanner; 

public class Players{
public static void main(String[] args){
    ArrayList<String> players=new ArrayList<>();
    players.add("Torres"); players.add("Ronaldo");players.add("Rooney");
    System.out.println("This is the current items in the players \n"+players);

    Scanner sc=new Scanner(System.in);


    System.out.println("Do you want to buy more players ????  y/n");
    String option=sc.nextLine();



    if (option.equalsIgnoreCase("y")){

        System.out.println("Please enter your favorite player  ");
        String newplayer=sc.nextLine();
    for (int i = 0; i <players.size() ; i++) {
        if (newplayer.equalsIgnoreCase(players.get(i))){
            System.out.println("The player is already in the team   ");


        }
        else {
        players.add(newplayer);
        }
    }

        System.out.println("New and old players are  "+ players);
    }
 else {
        System.out.println("The current players we have are "+players);
    }

     }



}

Aucun commentaire:

Enregistrer un commentaire