我的if和else statemenht有什么问题?

时间:2016-05-01 16:03:19

标签: java if-statement

我做了我的练习测试和#34;玩家已经在团队中了#34;如果选项等于y,将为变量newplayers的字符串输入发生3次。

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);
        }

    }

}

3 个答案:

答案 0 :(得分:0)

您应该删除if周围的for循环:

for (int i = 0; i <players.size() ; i++) {//its because of this loop that if your input is in players list it loops three times and prints "...already in team"
    if (newplayer.equalsIgnoreCase(players.get(i))){
        System.out.println("The player is already in the team   ");
    } else {
        players.add(newplayer);
    }
}

在这里你运行一个循环,这将循环三次(因为你的列表中有三个玩家)。一旦你发现玩家已经在团队中,你就不应该继续循环,方法是在你的方法中使用break,如下所示:

for (int i = 0; i <players.size() ; i++) {
   if (newplayer.equalsIgnoreCase(players.get(i))){
        System.out.println("The player is already in the team   ");
        break;
    } else {
        players.add(newplayer);
    }
}

或者更好的是,您将所有玩家都保存为小写,然后在播放器列表中使用contains方法,如:

if (players.contains(newplayer.toLowerCase())) {
     //player exist
} else {
     // add player
}

答案 1 :(得分:0)

问题是你的陈述中的其他部分(20次贯穿条件。)

 else {
    players.add(newplayer);
    }

对于不满足if的每个索引都会调用else。 只有在循环通过并且发现玩家没有团队时才需要调用它。

逻辑/伪代码:因此,一旦索引i完成其最后一次运行而不满足循环,就应该触发else语句。因此,如果循环已遍历,并且未达到break语句..这意味着玩家没有团队..然后添加播放器。

 for (int i = 0; i < players.size(); i++) {
            if (newplayer.equalsIgnoreCase(players.get(i))) {
                System.out.println("The player is already in the team   ");
             break;
            }
            if(i = players.size()-1&& !newplayer.equalsIgnoreCase(players.get(i))
            {
                 //If the loop is iterated through and does not satisfy the first conditional, then that means it will reach this conditional without breaking. Which also means that the player does not have a team

                    players.add(newplayer);
            }
        }

答案 2 :(得分:0)

for loop中提取添加玩家的逻辑检查玩家是否已经在那里将完成工作。 它还允许您通过在单独的方法中封装逻辑来重用搜索播放器的功能,并且它会更有效,因为它可以避免不必要的if检查。

试试这个。

String newplayer = sc.nextLine();
boolean playerExists = false;
for (int i = 0; i < players.size(); i++) {
    if (newplayer.equalsIgnoreCase(players.get(i))) {
        playerExists = true;
        break;
    }
}
if(playerExists){
    System.out.println("The player is already in the team   ");
} else {
    players.add(newplayer);
}

或采用单独的方法

if(playerExists()){
    System.out.println("The player is already in the team   ");
} else {
    players.add(newplayer);
}

// change access modifier and parameters according to your specific scenario...
private boolean playerExists(String player){
    for (int i = 0; i < players.size(); i++) {
        if (player.equalsIgnoreCase(players.get(i))) {
            return true;
        }
    }
    return false;
}