Uma dúvida com array

4 respostas
P

tenho que fazer uma loteria
o usuario escolhe 6 numeros de 1 a 60

e faço um random para o computador escolher 6 numeros de 60, dai tenho que informar quantos numeros o usuario acertou
não estou conseguindo colocar estes 6 numeros no array.

segue o codigo
package Prova;
import java.util.*;
public class Questao7 {

	public static void main(String[] args) {
		recebeDados();

	}
	public static void recebeDados(){
		Scanner sc = new Scanner(System.in);
		int numero[]=new int[6];
		int sorteio[]= new int[6];
	
		for(int i =0;i<numero.length;i++){
			System.out.println("Digite o numero");
			numero[i]= sc.nextInt();	
			
			sorteio[i]=(int)(Math.random()*60)+1;
			
			

		    	
			
		}
		
		System.out.println(sorteio);
	}
	

	}

4 Respostas

gabrielmskate

Ficou um pouco diferente do modo que voce fez…

import java.util.Scanner;

public class Sorteio {

	int[] numeros = new int[6];
	int[] sorteio = new int[6];
	private int corretos = 0;
	Scanner sc = new Scanner(System.in);

	public void pegaNumeros() {
		System.out
				.println("Digite valores maiores que 0 e menores ou iguais a 60");
		for (int i = 0; i < numeros.length; i++) {

			System.out.println("Digito um número: ");
			numeros[i] = sc.nextInt();
		}
		this.geraNumeros();
	}

	private void geraNumeros() {
		for (int i = 0; i < sorteio.length; i++) {
			sorteio[i] = (int) (Math.random() * 60) + 1;
		}
		this.confereNemeros();
	}

	private void confereNemeros() {

		for (int i = 0; i < numeros.length; i++) {
			for (int y = 0; y < sorteio.length; y++) {
				if (sorteio[y] == numeros[i]) {
					corretos++;
				}
			}

		}
		System.out.println("Números corretos: " + corretos);
	}

	public static void main(String args[]) {
		Sorteio sort = new Sorteio();
		sort.pegaNumeros();
	}
}
P

gabrielmskate:
Ficou um pouco diferente do modo que voce fez…

import java.util.Scanner;

public class Sorteio {

	int[] numeros = new int[6];
	int[] sorteio = new int[6];
	private int corretos = 0;
	Scanner sc = new Scanner(System.in);

	public void pegaNumeros() {
		System.out
				.println("Digite valores maiores que 0 e menores ou iguais a 60");

tenho que mostrar os numeros sorteados e os numeros digitados
		for (int i = 0; i < numeros.length; i++) {

			System.out.println("Digito um número: ");
			numeros[i] = sc.nextInt();
		}
		this.geraNumeros();
	}

	private void geraNumeros() {
		for (int i = 0; i < sorteio.length; i++) {
			sorteio[i] = (int) (Math.random() * 60) + 1;
		}
		this.confereNemeros();
	}

	private void confereNemeros() {

		for (int i = 0; i < numeros.length; i++) {
			for (int y = 0; y < sorteio.length; y++) {
				if (sorteio[y] == numeros[i]) {
					corretos++;
				}
			}

		}
		System.out.println("Números corretos: " + corretos);
	}

	public static void main(String args[]) {
		Sorteio sort = new Sorteio();
		sort.pegaNumeros();
	}
}

cara tenho que mostrar tambem os numeros sorteados e os numeros que o usuario digitou

gabrielmskate

Não sei se esse é mesmo o melhor modo pra fazer isso.
Mas tenta da uma aprimorada, melhor o mode de imprimir… Fica ao seu critério!!
E seria bom fazer uma verificação pra saber se o número é negativo ou maior que 60.

import java.util.Scanner;

public class Sorteio {

	int[] numeros = new int[6];
	int[] sorteio = new int[6];
	private int corretos = 0;
	Scanner sc = new Scanner(System.in);

	public void pegaNumeros() {
		System.out
				.println("Digite valores maiores que 0 e menores ou iguais a 60");
		for (int i = 0; i < numeros.length; i++) {

			System.out.println("Digito um número de 1 a 60: ");
			numeros[i] = sc.nextInt();
		}
		this.geraNumeros();
	}

	private void geraNumeros() {
		for (int i = 0; i < sorteio.length; i++) {
			sorteio[i] = (int) (Math.random() * 60) + 1;
		}
		this.confereNemeros();
	}

	private void confereNemeros() {

		for (int i = 0; i < numeros.length; i++) {
			for (int y = 0; y < sorteio.length; y++) {
				if (sorteio[y] == numeros[i]) {
					corretos++;
				}
			}
		}
		this.imprimirResultados();
	}

	private void imprimirResultados() {
		System.out.println("Números sorteados:");
		for (int i = 0; i < sorteio.length; i++) {
			System.out.println(sorteio[i]);
		}

		System.out.println("Números escolhidos:");
		for (int i = 0; i < numeros.length; i++) {
			System.out.println(numeros[i]);
		}
		System.out.println("Números corretos: " + corretos);
	}

	public static void main(String args[]) {
		Sorteio sort = new Sorteio();
		sort.pegaNumeros();
		sort.imprimirResultados();
	}
}
P

Cara muitop bom tirei a dúvida que eu tinha
valeu pela ajuda.

Criado 20 de julho de 2009
Ultima resposta 21 de jul. de 2009
Respostas 4
Participantes 2