Magda Rodrigues Posted May 7, 2013 at 08:27 AM Report #506009 Posted May 7, 2013 at 08:27 AM (edited) Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Randomize() Dim value0 As Integer Dim value1 As Integer Dim value2 As Integer Dim value3 As Integer Dim value4 As Integer Dim value5 As Integer Dim value6 As Integer Dim value7 As Integer value0 = (50 * Rnd() + 1) value1 = (50 * Rnd() + 1) value2 = (50 * Rnd() + 1) value3 = (50 * Rnd() + 1) value4 = (50 * Rnd() + 1) value5 = (50 * Rnd() + 1) value6 = (9 * Rnd() + 1) value7 = (9 * Rnd() + 1) TextBox1.Text = value0 TextBox2.Text = value1 TextBox3.Text = value2 TextBox4.Text = value3 TextBox5.Text = value4 TextBox6.Text = value5 TextBox7.Text = value6 TextBox6.BackColor = Color.Gold TextBox7.BackColor = Color.Gold End Sub End Class tenho aqui este pequeno código que gerar aleatoriamente a chave do EuroMilhoes, mas tenho que pedir ao utilizador para inserir a sua chave e no fim comparar dando o resultado se é premiado ou não... alguém me pode ajudar?? Edited May 7, 2013 at 10:04 AM by ribeiro55
Xpirito Posted May 7, 2013 at 09:38 AM Report #506029 Posted May 7, 2013 at 09:38 AM (edited) cria umas textbox que serão preenchidas pelo utilizador; comparas o que o utilizador escreveu com o que foi gerado pelo teu programa; se textbox1.text = textboxUtilizador então acertou e por aí fora até teres as textboxes todas verificadas no final podes analizar quantas textbox estão iguais e fazer o cálculo. Por exemplo se acertou 2 textbox o prémio é X se acertou 3textbox e 2textboxEstrelas o premio é Y Não é assim tão dificil, mas não contes com o programa feito. Aqui ajudamos, não fazemos pelos outros. Boa sorte. (Já agora, para não fazeres confusão, mais tarde deves experimentar escrever os resultados do teu programa num label e deixar as textboxes para inserção de dados do utilizador. Assim será mais fácil programares e não te perdes com tantas textboxes.) Edited May 7, 2013 at 09:39 AM by Xpirito 1 Report Para perguntas idiotas, respostas estúpidas!
ribeiro55 Posted May 7, 2013 at 10:09 AM Report #506040 Posted May 7, 2013 at 10:09 AM Antes disso, Magda, repara que com esse bloco de código, nada impede que possa calhar uma chave assim: 1 1 1 1 1 1 + 1 1 Tens de pensar num mecanismo que tenha consciência dos números que já saíram. Sérgio Ribeiro "Great coders aren't born. They're compiled and released""Expert coders do not need a keyboard. They just throw magnets at the RAM chips"
Magda Rodrigues Posted May 7, 2013 at 12:14 PM Author Report #506066 Posted May 7, 2013 at 12:14 PM eu ja estive a ver, para que nao repita o mesmo numero, obrigada pela a dica eu ja reparei nisso, e ja so me falta por a comparar porque deu me um erro
ruiribeiro Posted May 7, 2013 at 01:58 PM Report #506091 Posted May 7, 2013 at 01:58 PM (edited) Impedir numeros repetidos Public Class Form1 Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load TextBox1.Text = GeraNumeroAleatorio(TXT.Text) TextBox2.Text = GeraNumeroAleatorio(TXT.Text) TextBox3.Text = GeraNumeroAleatorio(TXT.Text) TextBox4.Text = GeraNumeroAleatorio(TXT.Text) TextBox5.Text = GeraNumeroAleatorio(TXT.Text) End Sub Public Function GeraNumeroAleatorio(ByVal valor As String) As Int32 Dim numeroAleatorio As Int32 = Int(valor * Rnd() + 1) Dim numeroRepetido As Boolean = VerificaNumeroERepetido(numeroAleatorio) If numeroRepetido Then Return GeraNumeroAleatorio(valor) End If Return numeroAleatorio End Function Public Function VerificaNumeroERepetido(ByVal numeroAleatorio As Int32) As Boolean Dim control As Control For Each control In Me.Controls If TypeOf control Is TextBox Then If control.Text = numeroAleatorio.ToString() Then Return True End If End If Next Return False End Function End Class Edited May 7, 2013 at 02:23 PM by ribeiro55 .NET/T-SQL, JAVA, PHP, Javascript Developer | Business Intelligence | Gestão de Sistemas de Informação Empresariais
ribeiro55 Posted May 7, 2013 at 02:52 PM Report #506113 Posted May 7, 2013 at 02:52 PM (edited) E se existirem mais TextBoxes? Não é muito boa ideia suportarmo-nos em objectos específicos no interface. Um exemplo de uma implementação mais genérica, já com ordenação como bónus 🙂 : Private Sub GerarChave(ParamArray TextBoxes As TextBox()) If TextBoxes.Count <> 7 Then Throw New Exception("Número incorrecto de textboxes...") Dim R As New Random : Dim Chave As New List(Of Integer) : Dim Estrelas As New List(Of Integer) Dim Num As Integer = R.Next(1, 50) While Chave.Count < 5 If Not Chave.Contains(Num) Then Chave.Add(Num) Num = R.Next(1, 50) End While Num = R.Next(1, 10) While Estrelas.Count < 2 If Not Estrelas.Contains(Num) Then Estrelas.Add(Num) Num = R.Next(1, 10) End While Chave.Sort() : Estrelas.Sort() Dim Indice_Chave As Integer = 0 : Dim Indice_Estrela As Integer = 5 For Each Numero As Integer In Chave TextBoxes(Indice_Chave).Text = Numero.ToString Indice_Chave += 1 Next For Each Numero As Integer In Estrelas TextBoxes(Indice_Estrela).Text = Numero.ToString Indice_Estrela += 1 Next End Sub Basta passar-lhe as textboxes em questão: GerarChave(TextBox1, TextBox2, TextBox3, TextBox4, TextBox5, TextBox6, TextBox7) Poderiamos tornar isto mais genérico, na medida em que desta forma ainda assume que o resultado é para ser apresentado em textboxes. Mas trata-se apenas de teres mais exemplos para analisares. A comparação, Magda, não é complicada também. O que já tentaste fazer? Edited May 7, 2013 at 02:59 PM by ribeiro55 Sérgio Ribeiro "Great coders aren't born. They're compiled and released""Expert coders do not need a keyboard. They just throw magnets at the RAM chips"
ruiribeiro Posted May 7, 2013 at 03:16 PM Report #506117 Posted May 7, 2013 at 03:16 PM (edited) ribeiro55, concordo, mas não era o meu objectivo fornecer o peixe, mas sim, ensinar a pescar 😉 Edited May 7, 2013 at 03:21 PM by ribeiro55 .NET/T-SQL, JAVA, PHP, Javascript Developer | Business Intelligence | Gestão de Sistemas de Informação Empresariais
Bruno Neves Posted May 8, 2013 at 10:19 AM Report #506230 Posted May 8, 2013 at 10:19 AM (edited) Private Sub cmd_gerarchave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_gerarchave.Click Dim N1, N2, N3, N4, N5, e1, e2 As Integer Dim file As IO.StreamWriter Dim chave(5) As Integer Dim estrela(2) As Integer 'programa nao deixa sequer fazer randomize se 'nao tiver pelo menos 2 eur If saldo <= 1 Then MsgBox("É impossivel Jogar, não possui saldo suficiente!", MsgBoxStyle.Exclamation) Else If njogadas = 6 And nestrelas = 3 Then Randomize() N1 = Int(Rnd() * 50 + 1) ' txt_numeroaleatorio1.Text = N1 chave(1) = N1 'MsgBox("chave 1" & N1) 'MsgBox("chave 1" & chave(1)) Randomize() N2 = Int(Rnd() * 50 + 1) Do While N1 = N2 Randomize() N2 = Int(Rnd() * 50 + 1) Loop ' txt_numeroaleatorio2.Text = N2 chave(2) = N2 'MsgBox("chave 2" & N2) 'MsgBox("chave 2" & chave(2)) Randomize() N3 = Int(Rnd() * 50 + 1) Do While N3 = N1 Or N3 = N1 Randomize() N3 = Int(Rnd() * 50 + 1) Loop ' txt_numeroaleatorio3.Text = N3 chave(3) = N3 'MsgBox("chave 3" & N3) 'MsgBox("chave 3" & chave(3)) Randomize() N4 = Int(Rnd() * 50 + 1) Do While N4 = N1 Or N4 = N2 Or N4 = N3 Randomize() N4 = Int(Rnd() * 50 + 1) Loop ' txt_numeroaleatorio4.Text = N4 chave(4) = N4 'MsgBox("chave 4" & N4) 'MsgBox("chave 4" & chave(4)) Randomize() N5 = Int(Rnd() * 50 + 1) Do While N5 = N1 Or N5 = N2 Or N5 = N3 Or N5 = N4 Randomize() N5 = Int(Rnd() * 50 + 1) Loop 'txt_numeroaleatorio5.Text = N5 chave(5) = N5 'MsgBox("chave 5" & N5) 'MsgBox("chave 5" & chave(5)) 'fim numeros aleatorios 'Dim str As String = "" 'For i = 1 To 5 ' str = str & chave(i) & "; " 'Next Dim tmp As Integer For i = 1 To 4 For j = i + 1 To 5 If chave(i) > chave(j) Then tmp = chave(j) chave(j) = chave(i) chave(i) = tmp End If Next Next 'str = "" 'For i = 1 To 5 ' str = str & chave(i) & "; " 'Next 'MsgBox("ver aqui dps ordenação --" & str) txt_numeroaleatorio1.Text = chave(1) txt_numeroaleatorio2.Text = chave(2) txt_numeroaleatorio3.Text = chave(3) txt_numeroaleatorio4.Text = chave(4) txt_numeroaleatorio5.Text = chave(5) Randomize() e1 = Int(Rnd() * 11 + 1) 'txt_estrelaaleatoria1.Text = e1 estrela(1) = e1 Randomize() e2 = Int(Rnd() * 11 + 1) Do While e2 = e1 Randomize() e2 = Int(Rnd() * 11 + 1) Loop 'txt_estrelaaleatoria2.Text = e2 estrela(2) = e2 If e1 < e2 Then txt_estrelaaleatoria1.Text = e1 txt_estrelaaleatoria2.Text = e2 Else txt_estrelaaleatoria1.Text = e2 txt_estrelaaleatoria2.Text = e1 End If ''Enviar para ficheiro txt Dim extensao = ".txt" file = My.Computer.FileSystem.OpenTextFileWriter(getMaosDirectorio() + getUtilizador() + extensao, True) file.WriteLine(txt_numeroaleatorio1.Text + "-" + txt_numeroaleatorio2.Text + "-" + txt_numeroaleatorio3.Text + "-" + txt_numeroaleatorio4.Text + "-" + txt_numeroaleatorio5.Text + "/" + txt_estrelaaleatoria1.Text + "-" + txt_estrelaaleatoria2.Text + " * " + txt_suachavenumero1.Text + "-" + txt_suachavenumero2.Text + "-" + txt_suachavenumero3.Text + "-" + txt_suachavenumero4.Text + "-" + txt_suachavenumero5.Text + "/" + txt_suachaveestrela1.Text + "-" + txt_suachaveestrela2.Text) file.Close() '' modificar o saldo actual subSaldo(2) txt_montanteutilizador.Text = getSaldo().ToString() file = My.Computer.FileSystem.OpenTextFileWriter(getMovimentosDirectorio() + getUtilizador() + extensao, True) file.WriteLine("Jogada : -2") file.Close() Else MsgBox("Não escolheu os 5 nºs ou as estrelas", MsgBoxStyle.Exclamation) End If Boas, vê o codigo desse botao, espero que ajude 😉 Edited May 8, 2013 at 11:05 AM by ribeiro55
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now