Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

148
Visualizações
Modify array with a method of the same class

i have class Deck

import java.util.Random;
public class Deck implements DeckActions{
    private static Card[] cards=new Card[52];
    public Deck () {
        int i=0;                                    
        for (Suit suit : Suit.values()){
            for (Rank rank : Rank.values()){
                cards[i++]=new Card(rank,suit); 
            }
        }
    }

    public Card draw() {                                 
        return cards[0];
    }

    public void shift(int times) {                          
        Random randomGenerator=new Random();
        int card1=randomGenerator.nextInt(52);
        int card2=randomGenerator.nextInt(52);

        for (int counter=0;counter < times; counter++){

        Card temp=new Card(null,null);
        temp=cards[card1];
        cards[card1]=cards[card2];
        cards[card2]=temp;
        }
    }
    public void show() {                            
        for (int i=0;i<cards.length;i++){
            System.out.println(cards[i].toString());    
        }   
    }
}

that implements interface

public interface DeckActions {

    public Card draw();                 
    public void shift(int times);       
    public void show();                 

}

the problem i have is that the Shift method doesn't actually change the deck array. What am i missing? I really can't figure out.

This is my main class, i tried to call the method with an int like 100, or 52, but show always shows the same array

public class TestCard {

    public static void main (String [] args) {
        Deck deck=new Deck();
            deck.show();
        deck.shift(52);
        System.out.println("Shifted");
        deck.show();

    }
}
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

The problem is that lines which generate random indexes of cards to swap are outside loop, so all the time you swap the same 2 cards. Try:

 public void shift(int times) {                          
    Random randomGenerator=new Random();

    for (int counter=0;counter < times; counter++){

       int card1=randomGenerator.nextInt(52);
       int card2=randomGenerator.nextInt(52);

       Card temp=new Card(null,null);
       temp=cards[card1];
       cards[card1]=cards[card2];
       cards[card2]=temp;
    }
}

P.S: any reason for field cards to be static?

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda