Array and error handling in numbers game

Asked

Viewed 46 times

1

The game is to discover opponent’s numbers, but not lottery type, have to use reasoning to get to the correct number.

The rule is to play with 4 numbers at a time without repeating on the same line. About the array I have the following questions.

1st I can type 4 numbers directly and store directly in an array without specifying Input by Input, direct by size limit?

ex: pal[i] = entrada.nextInt()

Instead of Indice [i], direct save the 4 numbers, to leave the for just to make the comparison

2º If you can’t do this, how to limit only 1 number typed at a time?

3rd No error handling, in case of typing letter, just warn that can not and return to ask for the number, I did but it appears the two messages after entering letters, instead of giving the warning and only ask to type 4 numbers.

I would like tips more manually, because I’m at the beginning exploring loop for and array, I know there are libraries to facilitate but I haven’t gotten there yet, and the game is exploring where I start to have difficulties, in the precise case of direction only.

This game is done in the notebook, I will leave an example, but in the notebook the opponent sometimes passes the wrong values. In the "You" field, just choose the number and below is the opponent trying to find, then return as in the example, and on the right side is the player himself trying to guess the other player

inserir a descrição da imagem aqui

public class JogoNumeros {

    public static void main(String[] args) {
        Scanner entrada = new Scanner(System.in);
        Integer[] num = {1,2,3,4}; //Variável do jogador principal
        int[] pal =  new int[4]; //Variável do palpite do advesário
        List<Integer> n = Arrays.asList(num);
        int fim = 0;

        while(fim < 5){
            int bom = 0;
            int regular = 0; 
            int nenhum = 0;

            for (int i = 0; i < pal.length; i++){
                System.out.print("Digite 4 números: ");
                try{
                pal[i] = Integer.parseInt(entrada.nextLine());
                }catch(NumberFormatException e){
                    System.out.println("Digite apenas números"); //Após digitar letra fica aparecendo no console "Digite 4 números: " e "Digite apenas números"
                    pal[i] = entrada.nextInt();
                }

                nenhum = pal[i]; 
            }
             if( !n.contains(nenhum)){ //primeira comparação, caso não acerte nenhum
                System.out.println("Nenhum!!!!");
                }  

            for (int y = 0; y < pal.length; y++){
                if (pal[y] == num[y]){ //Caso exista o número e esteja na posição correta
                    bom += 1;
                }

                if ( n.contains(pal[y]) && pal[y]!= num[y]){
                    regular += 1; //Caso exista o número e não esteja na posição correta
                }
            }
                if(bom == 4){ //Acertando os 4 encerra o loop while
                    fim = bom;
                    System.out.println("Acertou");
                    break;
                }
            System.out.println(bom + " B e " + regular + " R");
            System.out.println();
        }
    }
}
  • Treats everything as string, simple ;)

  • @Articuno, as a beginner, is still not so simple for me. I think that even turning into String would end up with the same difficulties in limiting character to only number, and one number at a time, and also something that I don’t know if it’s possible, type the 4 numbers and play everything straight as if it were Indice+1, first falls in 0, second in 1 and so on.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.