1
I am creating a code to develop my studies in java and gave me an idea/ interesting challenge:
One person has 5 Megasena cards and always plays with the same cards. I made a little code where you type the numbers of the last draw and it checks to see if it matches the cards. Look:
public static void main(String[] args) {
int[] cartela1 = {13,25,32,47,59,60};
int[] cartela2 = {14,26,33,48,55,60};
int[] cartela3 = {18,63,41,25,21,36};
int[] cartela4 = {14,26,33,48,55,60};
int[] cartela5 = {13,25,32,47,59,60};
int[] numSorteio = new int[6];
int i,j;
int count = 0;
Scanner scan = new Scanner(System.in);
for (i=0; i<numSorteio.length; i++) {
System.out.println("Digite o número sorteado na posição: " +i);
numSorteio[i] = scan.nextInt();
}
for (i=0; i<6; i++) {
for (j=0; j<6; j++) {
if (numSorteio[j] == cartela1[i]) {
System.out.println("Seu número " + cartela1[i]+ " foi sorteado!");
count++;
}
}
}
System.out.println("Você acertou " +count+ " números na cartela 1!");
}
As you can see my code works perfectly showing in the output the numbers I hit and the amount.
However I could only do for the cartela1, if I repeated the FOR 5 times would be perfect for 5 cartelas, however I wanted to make it shorter and elegant.
There is really a lot there that I don’t know yet and I don’t understand it, but I’m learning it. Your code has kept me awake the last few days. After much headbanging I discovered that there is an addiction. It only matches between a number in the "number" array and a number in one of the "A,B,C or D" arrays if it matches the numbers being in the same position in the arrays; for example: ARRAY_CARTELA: {1,19,21,37,48,54} ARRAY_SORTEIO: {1,18,19,29,44,54} The winning numbers are: 01, 19 and 54. The code says it was only 01 and 54, because it beats the position in the arrays, but 19 it ignores.
– Kellyson
You are absolutely right. I have adjusted the code and now it should work smoothly. Mark the question as correct if it meets what you need.
– StatelessDev