mardi 18 avril 2017

Why does my switch case not work in java?

As you can see, I am trying to do this: int x = 0 if true, x is 1 if false, x is 2 then switch case x to do different things i tried different methods but it always ends up showing one of the two options as dead code any functions within the code are already proven correct. here is the code I have written:

 static void modifyPlayer(ArrayList<Player> listOfPlayers)
            throws IOException {
    Scanner modP = new Scanner(System.in);      
    Scanner found = new Scanner(System.in);

    System.out.print("Enter Login Name to search: ");
    String name = found.next();

    int x = listOfPlayers.size();
    searchPlayer(listOfPlayers, name);
    int u = 0;
    if(true){
        u = 1;
    }
    if(false){
        u = 2;
    }
    switch(u){
    case 1:
    {
        // Modifying specific data
        for(int i = 0; i < x; i++){
        if(listOfPlayers.get(i).getLoginname().equals(name)){
                System.out.print("Enter new Chips: ");
                third = modP.next();
                listOfPlayers.get(i).setChips(third);

            System.out.print("Enter new User Name: ");
                fourth = modP.next();
                listOfPlayers.get(i).setUsername(fourth);

                System.out.print("Enter new Email: ");
                fifth = modP.next();
                listOfPlayers.get(i).setEmail(fifth);

            System.out.print("Enter new `Birthdate: ");
                sixth = modP.next();
                listOfPlayers.get(i).setBirthdate(sixth);
            }
            displayAll(listOfPlayers);
        }
    }
        break;
    case 2: 
    {
        System.out.println("Player not found. Add player?: ");
        System.out.println("1. Yes");
        System.out.println("2. No");

        Scanner elseOption = new Scanner(System.in);
        int opt = elseOption.nextInt();

        if(opt == 1){
            createNewPlayer(listOfPlayers);
        }

        else{
            System.out.println("Yet to add this function.");
        }
        displayAll(listOfPlayers);
    }
        break;
    }






Comments
0 Comments

0 commentaires:

Enregistrer un commentaire