Jump to content

Programa para simular numeros e estrelas euromilhões


Recommended Posts

Posted

Boa tarde estou aqui com alguns problemas com o desafio que o meu setor lançou  :wallbash:

Até agora tenho conseguido me desenrrascar sozinho mas neste momento tenho aqui algo que não estou a conseguir controlar porque atribuo numeros aleatórios e eles repetem-se estou a tentar fazer com que não se repitam consegui fazer com que as estrelas n se repitam pq só tenho de controlar dois numeros agora na chave é mais diicil  :wallbash:

aqui fica o codigo se puderem ajudar

import javax.swing.*;
class euroalunos{



public static void main (String args []){
	double Aleat;
	double Aleat1;
	String [] aluno= new String[20];{
		int x=0,y=0,w=0,z=0;
		int var1,var2,var3;
		int[] var= new int[20];
		int[] var4= new int[8];
		while (x<20)
		{
			aluno[x]=JOptionPane.showInputDialog("Introduza o nome "+(x+1));
			System.out.println("O nome inserido foi:"+ aluno[x]);
			x++;
		}
		for(z=0;z<8;z++)
		{
			var4[z]= (int) (Math.random() *8)+1;
			do
			{
			var4[z+1]= (int) (Math.random() *8)+1;
			System.out.println(var4[z+1]);	
			}
			while(var4[z+1]==var4[z]);
			z++;
		}	
		for(x=0;x<20;x++)
		{
			var[x]= (int) (Math.random() *49)+1;

			if(x%5==0)
			System.out.println("GRUPO "+(++y)+" estrelas "+var4[(x/5*2)]+" " + var4[(x/5*2+1)]);			
			System.out.println(aluno[x]+" "+var[x] );

		}




			var2=Integer.parseInt(JOptionPane.showInputDialog("Qual o numero da grelha que pretende ver?"));			

		x=(var2-1)*5;
		JOptionPane.showMessageDialog(null,(var[x])+" "+(var[x+1])+" "+(var[x+2])+" "+(var[x+3])+" "+(var[x+4]), "Informacao de grupos",JOptionPane.INFORMATION_MESSAGE);
		var3=Integer.parseInt(JOptionPane.showInputDialog("Qual o numero que pretende verefica se existe de 1 a 49 ?"));
	while(var3>49)

	var3=Integer.parseInt(JOptionPane.showInputDialog("Qual o numero que pretende verefica se existe de 1 a 49 ?"));

	for(x=0;x<20;x++)
	{
	if (var3==var[x]){
	w=1;
	x=20;
	JOptionPane.showMessageDialog(null,"existe", "Informacao de grupos",JOptionPane.INFORMATION_MESSAGE);
	}
	}
	if (w==0){
	JOptionPane.showMessageDialog(null,"não existe", "Informacao de grupos",JOptionPane.INFORMATION_MESSAGE);
	}
	}
	}
	} 

:lamo:

obrigado desde já algo mal no post digam? 😁

Posted

Boas desde já obrigado pela dica do codigo 😁

se podesses alongar essa parte de estar tudo em main e ajudar mais no sentido de me orientares agradecia e desculpa a ignorancia  ?

Posted

Acho que o grande problema é estares a iniciar variáveis mesmo "à pazada".

Alem disso, ao usares a mesma variável para vários fins, é bastante provável que fiquem valores pendurados.

Tenta modular isso. Por exemplo, as variáveis de controlo do for, só precisam de existir dentro do ciclo.

Em vez de

                      int z = 0
                      for(z=0;z<8;z++){
                                var4[z]= (int) (Math.random() *8)+1;
                                do
                                {
                                var4[z+1]= (int) (Math.random() *8)+1;
                                System.out.println(var4[z+1]); 
                                }
                                while(var4[z+1]==var4[z]);
                                z++;
                        }

Podes ter

                  for(int z =0; z <var4.length; z++){
		var4[z]= (int) (Math.random() *8)+1;
		do{
			var4[z+1]= (int) (Math.random() *8)+1;
			System.out.println(var4[z+1]); 
		}
		while(var4[z+1]==var4[z]);
		z++;
	}

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.