Array and error handling in numbers game

1

The game is to discover numbers of the opponent, 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. On the array I have the following doubts.

1ª Can I enter 4 numbers directly and store directly in an array without specifying index by direct index by size limit?

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

Instead of the index [i], save the 4 numbers directly, to leave it alone to make the comparison

2nd If you can not do this, how can I limit only 1 number entered at a time?

3rd In the error treatments, in the case of typing letter, just let me know that you can not and ask for the number again, I did but it appears both messages after entering letters, instead of giving the warning and just ask for enter 4 numbers.

I would like tips more manually, because I'm at the beginning exploring loop for and array, I know that there are libraries to facilitate but I have not yet gotten there, and the game idea is to explore where I start to have difficulties, direction only.

This game is made 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, hence would return as in the example, and on the right side is the player himself guessing the other player

publicclassJogoNumeros{publicstaticvoidmain(String[]args){Scannerentrada=newScanner(System.in);Integer[]num={1,2,3,4};//Variáveldojogadorprincipalint[]pal=newint[4];//VariáveldopalpitedoadvesárioList<Integer>n=Arrays.asList(num);intfim=0;while(fim<5){intbom=0;intregular=0;intnenhum=0;for(inti=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();
        }
    }
}
    
asked by anonymous 06.10.2018 / 22:47

0 answers