Jogo caça ao Tesouro

3 respostas Resolvido
java
Priscilla_Betuyaku

Olá pessoal, sou nova nessa área e meu professor da faculdade pediu para desenvolver um jogo de caça ao Tesouro (parecido com batalha naval) onde, precisa ser de 2 a 4 players, e eles precisam ser maiores de 12 anos. O problema é, já coletei os dados do usuário, porém não sei como sair da parte da validação e fazer o loop. O tabuleiro já está pronto, mas meu código está todo bagunçado =(
Pode ser uma pergunta meio estúpida mas eu travei nessa parte. Me ajudem, por favor!

public static void main(String[] args) {

String namePlayer;
    int numPlayers;
    

   // ArrayList<String> players = new ArrayList();
   // ArrayList<Integer> scores = new ArrayList();

    System.out.println("Welcome to the Pirate Peter's Treasure Hunt Game, are you ready for this adventure? ");
    Scanner sc = new Scanner(System.in); //take the information from the user
    
    for(int counter=0;counter<4;counter++){
        
        System.out.println("Please enter the number of players between 2 and 4"); //show the instructions
        numPlayers = sc.nextInt();
        
        if((numPlayers == 2) || (numPlayers == 3) || (numPlayers == 4)){ //check if the number of players are between 2 and 4
            System.out.println("ARGH! What's the name of the Pirate? ");
            namePlayer = sc.nextLine();
        }
        else {
            System.out.println("OH God! This number of players are not right! Enter a number between 2 and 4: ");
            numPlayers = sc.nextInt();
        }

3 Respostas

B
System.out.println("Welcome to the Pirate Peter's Treasure Hunt Game, are you ready for this adventure? ");
        Scanner sc = new Scanner(System.in); //take the information from the user
        
        System.out.println("Please enter the number of players between 2 and 4"); //show the instructions
        int numPlayers = sc.nextInt();
        
        String[] piratas = new String[numPlayers];
        
        for(int i = 1; i <= numPlayers; i++){
            System.out.println("ARGH! What's the name of the " + i + "º Pirate? ");
            piratas[i] = sc.nextLine();
        }

agora vc consegue seguir…

Priscilla_Betuyaku

@blayd2015 Obrigada, mas ainda não deu certo, está repetindo a o nome do jogador, por exemplo quando digita 2 jogadores só é possível digitar 1, quando coloca 3 só entra com 2 jogadores e assim por diante, já tentei alterar o valor do i=0, i=1, i=2. Alterei um pouco o que você me mandou mas ainda está errado =/

int numPlayers;
String namePlayer;

System.out.println("Welcome to the Pirate Peter's Treasure Hunt Game, are you ready for this adventure? ");
    Scanner sc = new Scanner(System.in); //take the information from the user
    System.out.println("How many Pirates are going to play? (2-4)"); //show the instructions
    numPlayers = sc.nextInt();
    
    

    for (int i = 1; i <= numPlayers; i++) {
        if (numPlayers == 2) {
            System.out.println("What's the name of the " + i + " Pirate?");
            namePlayer = sc.nextLine();
        }
        else if(numPlayers == 3){
            System.out.println("What's the name of the " + i + " Pirate?");
            namePlayer = sc.nextLine();
        }
        else if(numPlayers == 4){
            System.out.println("What's the name of the " + i + " Pirate?");
            namePlayer = sc.nextLine();
        }
        
        
    }
Lucas_Camara
Solucao aceita
System.out.println("Welcome to the Pirate Peter's Treasure Hunt Game, are you ready for this adventure? \n\n ");
Scanner sc = new Scanner(System.in);
int numPlayers = 0;

/*
 * Obtém a quantidade de jogadores, que deve ser entre 2 e 4
 */
boolean validNumPlayers = false;

while (!validNumPlayers) {
	System.out.print("Please enter the number of players between 2 and 4: ");
	numPlayers = sc.nextInt();
	validNumPlayers = (numPlayers >= 2 && numPlayers <= 4);
}

/*
 * Obtém os nomes dos jogadores
 */
String[] pirates = new String[numPlayers];

System.out.println(pirates.length);

for(int i = 0; i < numPlayers; i++) {
	System.out.print("ARGH! What's the name of the " + i + "º Pirate? ");
	pirates[i] = sc.next();
	System.out.println();
}

/*
 * Imprime os nomes dos jogadores
 */
System.out.println("Foram cadastrados " + pirates.length + " piratas, e eles são:");

for (String pirate : pirates) {
	System.out.println("> " + pirate);
}

sc.close();
Criado 30 de abril de 2020
Ultima resposta 1 de mai. de 2020
Respostas 3
Participantes 3