Olá será que alguem pode me ajudar, estou com um problema e não consigo resolver, preciso gerar uma cartela de bingo com 3 linhas e nove colunas, cada linha precisa ter 5 numeros aleatorios com 5 posições aleatorias tb, até ai tudo bem mas nao consigo fazer gerar de 1 a 90 somente de 0 a 90 fiz 3 arrays e cada um não pode ter os mesmos numeros dos outros segue o código alguém me ajuda?
public class Bingo {
public static void main(String[] args) {
int [] row = new int [9];
int [] row2 = new int [9];
int [] row3 = new int [9];
int p;
int v;
Random random = new Random();
int p1 = random.nextInt(7 - 1 + 1);
int p2 = random.nextInt(7 - 1 + 1);
int p3 = random.nextInt(7 - 1 + 1);
int p4 = random.nextInt(7 - 1 + 1);
int p5 = random.nextInt(7 - 1 + 1);
int posi1 = random.nextInt(7 - 0 + 1);
int posi2 = random.nextInt(7 - 0 + 1);
int posi3 = random.nextInt(7 - 0 + 1);
int posi4 = random.nextInt(7 - 0 + 1);
int posi5 = random.nextInt(7 - 0 + 1);
int posii1 = random.nextInt(7 - 0 + 1);
int posii2 = random.nextInt(7 - 0 + 1);
int posii3 = random.nextInt(7 - 0 + 1);
int posii4 = random.nextInt(7 - 0 + 1);
int posii5 = random.nextInt(7 - 0 + 1);
int v1 = random.nextInt( ((p1*10)+10) - ((p1*10)+1) ) + ((p1*10)+1)+1;
int v2 = random.nextInt( ((p2*10)+10) - ((p2*10)+1) ) + ((p2*10)+1)+1;
int v3 = random.nextInt( ((p3*10)+10) - ((p3*10)+1) ) + ((p3*10)+1)+1;
int v4 = random.nextInt( ((p4*10)+10) - ((p4*10)+1) ) + ((p4*10)+1)+1;
int v5 = random.nextInt( ((p5*10)+10) - ((p5*10)+1) ) + ((p5*10)+1)+1;
int v6 = random.nextInt( ((posi1*10)+10) - ((posi1*10)+1) ) + ((posi1*10)+1)+1;
int v7 = random.nextInt( ((posi2*10)+10) - ((posi2*10)+1) ) + ((posi2*10)+1)+1;
int v8 = random.nextInt( ((posi3*10)+10) - ((posi3*10)+1) ) + ((posi3*10)+1)+1;
int v9 = random.nextInt( ((posi4*10)+10) - ((posi4*10)+1) ) + ((posi4*10)+1)+1;
int v10 = random.nextInt( ((posi5*10)+10) - ((posi5*10)+1) ) + ((posi5*10)+1)+1;
int v11 = random.nextInt( ((posii1*10)+10) - ((posii1*10)+1) ) + ((posii1*10)+1)+1;
int v12 = random.nextInt( ((posii2*10)+10) - ((posii2*10)+1) ) + ((posii2*10)+1)+1;
int v13 = random.nextInt( ((posii3*10)+10) - ((posii3*10)+1) ) + ((posii3*10)+1)+1;
int v14 = random.nextInt( ((posii4*10)+10) - ((posii4*10)+1) ) + ((posii4*10)+1)+1;
int v15 = random.nextInt( ((posii5*10)+10) - ((posii5*10)+1) ) + ((posii5*10)+1)+1;
row[ p1 ] = v1;
row[ p2 ] = v2;
row[ p3 ] = v3;
row[ p4 ] = v4;
row[ p5 ] = v5;
row2[ posi1 ] = v6;
row2[ posi2 ] = v7;
row2[ posi3 ] = v8;
row2[ posi4 ] = v9;
row2[ posi5 ]= v10;
row3[ posii1 ] = v11;
row3[ posii2 ] = v12;
row3[ posii3 ] = v13;
row3[ posii4 ] = v14;
row3[ posii5 ]= v15;
System.out.println(Arrays.toString(row)+"\t"+"\n"+ Arrays.toString(row2)+
"\t"+"\n"+ Arrays.toString(row3) );
}
}
Dica: Use orientação objetos e evite repetição de código.
use um for para verificar se já existi algum numero igual no array(vc vai repetir muito codigo fazendo isso, use orientação a objetos),
o restante eu já fiz no seu codigo:
package livro.java.Arrays;
import java.util.Arrays;
import java.util.Random;
public class Bingo {
public static void main(String[] args) {
int[] row = new int[5];
int[] row2 = new int[5];
int[] row3 = new int[5];
Random random = new Random();
int v1 = random.nextInt(89) / 2 + 5; // (/ 2+5) foi um criterio simples para da uma diminuída nos resultados pois estava saindo muito autos
int v2 = random.nextInt(89) / 2 + 5;
int v3 = random.nextInt(89) / 2 + 5;
int v4 = random.nextInt(89) / 2 + 5;
int v5 = random.nextInt(89) / 2 + 5;
int v6 = random.nextInt(89) / 2 + 5;
int v7 = random.nextInt(89) / 2 + 5;
int v8 = random.nextInt(89) / 2 + 5;
int v9 = random.nextInt(89) / 2 + 5;
int v10 = random.nextInt(89) / 2 +5;
int v11 = random.nextInt(89) / 2 + 5;
int v12 = random.nextInt(89) / 2 + 5;
int v13 = random.nextInt(89) / 2 + 5;
int v14 = random.nextInt(89) / 2 + 5;
int v15 = random.nextInt(89) / 2 + 5;
row[0] = v1;
row[1] = v2;
row[2] = v3;
row[3] = v4;
row[4] = v5;
row2[0] = v6;
row2[1] = v7;
row2[2] = v8;
row2[3] = v9;
row2[4] = v10;
row3[0] = v11;
row3[1] = v12;
row3[2] = v13;
row3[3] = v14;
row3[4] = v15;
System.out.println(
Arrays.toString(row) + "\t" + "\n" + Arrays.toString(row2) + "\t" + "\n" + Arrays.toString(row3));
}
o problema é que algumas posições estão recebendo 0 como valor e eu estou tentanto por de 1 a 90 mas que apareça exatamente como a minha sáida totalmente aleatoria
eu preciso das posições aleatorias tambem compreende? vc pode me ajudar ?
O motivo da saída 0 é justamente as posições aleatória, tá acontecendo da msm posição recebe valor mais de uma vez, com consequência outras posição fica com o valor padrão (0), se vc tá gerando 5 posição para cada array, é uma posição recebe duas vezes, uma fica com 0
Eu “achei” uma codificação que faz algo parecido, mas um for não da conta não.
Deixo aqui registrado a implementação do código usando a sugestão de @AbelBueno, muito boa mesmo (vide classe Cartela).
package bingo;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class Bingo implements Runnable {
private final String local;
private final Sorteio sorteio = new Sorteio();
private final Set<Cartela> cartelas = new HashSet<>();
private int cont = 0;
public Bingo(String local) {
this.local = local;
}
public static void main(String[] args) {
new Thread(new Bingo("Cidade Nova")).start();
}
private int contar() {
return ++cont;
}
private void configurar(int totalCartelas, int linhas, int colunas) {
cont = 0;
while (cartelas.size() < totalCartelas) {
cartelas.add(new Cartela().getCartela(linhas, colunas,false));
}
cartelas.stream().forEach((cartela) -> {
System.out.printf("======= CARTELA %04d =======\n", contar());
for (int[] casa : cartela.getCasas()) {
System.out.println(" " + Arrays.toString(casa));
}
System.out.println("============================\n");
});
}
private void iniciarBingo(Scanner scan) {
configurar(9999, 3, 5);
while (true) {
System.out.println(local+"\nPara sortear aperte alguma tecla e depois enter");
scan.next();
sorteio.sortear();
System.out.println("Bola Sorteada: "+sorteio.getBola()+"\n"+sorteio.getMesaConfere()+"\n...");
if (sorteio.getBola() == 75) {
//apenas para sair do laço
break;
}
}
scan.close();
}
@Override
public void run() {
this.iniciarBingo(new Scanner(System.in));
}
}
package bingo;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Cartela {
private String doHash;
private int[][] casas;
private int pontos;
private List<Integer> pecas = new ArrayList<>();
public Cartela getCartela(int linhas, int colunas, boolean isOrdenado) {
StringBuilder sb = new StringBuilder();
//implementação usando a sugestão de AbelBueno
List<Integer> range = IntStream.range(1, 91).boxed().collect(Collectors.toList());
Collections.shuffle(range);
pecas = range.subList(0, linhas * colunas);
casas = new int[linhas][colunas];
if (isOrdenado) {
Collections.sort(pecas);
}
int controle = 0;
for (int[] linha : casas) {
for (int i = 0; i < linha.length; i++) {
sb.append(pecas.get(controle));
linha[i] = pecas.get(controle++);
}
}
this.doHash = sb.toString();
return this;
}
public int[][] getCasas() {
return casas;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
return Objects.equals(this.doHash, ((Cartela) obj).doHash);
}
@Override
public int hashCode() {
int hash = 7;
hash = 89 * hash + Objects.hashCode(this.doHash);
return hash;
}
public int getPontos() {
return pontos;
}
}
package bingo;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Sorteio {
private final static Random RAND = new Random();
private List<Integer> globo = new ArrayList<>();
private final List<Integer> mesaConfere = new ArrayList<>();
private int bola;
public Sorteio() {
globo = IntStream.range(1, 91).boxed().collect(Collectors.toList());
}
public final void sortear(){
int remover;
bola = globo.get(remover = RAND.nextInt(globo.size()));
globo.remove(remover);
mesaConfere.add(bola);
}
public int getBola() {
return bola;
}
public ArrayList<Integer> getGlobo() {
return (ArrayList) globo;
}
public ArrayList<Integer> getMesaConfere() {
return (ArrayList) mesaConfere;
}
}