Java程序显示玩家在用户输入和用户输入运动上的玩家名称上播放的运动

时间:2017-07-05 08:29:32

标签: java

Player 1 -- chess , hockey , carom 
Player 2 -- cricket , badminton , carom 
Player 3 -- ludo , badminton , pool 
Player 4 -- Table tennis , badminton , snooker 

需要显示在用户输入的球员姓名上播放的运动 需要在用户输入的球员名称上显示,由他打的运动

用户可以添加玩家名称,也可以为个人玩家添加更多运动

在采访中被问到 卡住..请帮助核心java 提前致谢

1 个答案:

答案 0 :(得分:0)

这是你的解决方案:

package com.sujit;

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

public class Sport {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter player name : ");
        String playerName = sc.next();
        List<String> list = getSportByPlayerName(playerName);
        for (String string : list) {
            System.out.println(string);
        }
    }

    private static List<String> getSportByPlayerName(String playerName) {
        List<String> listOfSports = new ArrayList<>();  
        if(playerName.equalsIgnoreCase("player1")) {
            listOfSports.add("chess");
            listOfSports.add("hockey");
            listOfSports.add("carrom");
            return listOfSports;
        }
        else if(playerName.equalsIgnoreCase("player2")) {
            listOfSports.add("cricket");
            listOfSports.add("badminton");  
            listOfSports.add("carrom");
            return listOfSports;
                }
        else if(playerName.equalsIgnoreCase("player3")) {
            listOfSports.add("ludo");
            listOfSports.add("badminton");
            listOfSports.add("pool");
            return listOfSports;
        }
        else if(playerName.equalsIgnoreCase("player4")) {
            listOfSports.add("table tennis");
            listOfSports.add("snooker");
            listOfSports.add("badminton");
            return listOfSports;
        }
        else
            return null;
    }
}