krykz Posted January 10, 2016 at 12:01 AM Report Share #591745 Posted January 10, 2016 at 12:01 AM (edited) Boas , Estou a realizar um programa em vb que ao clicar em 2 botões ele dá random de 2 numeros em 2 textbox 's cada um e o numero maior tem de ficar numa listbox utilizando arrays para guardar os varios resultados . Porém nao esta 100% certo pois ele em vez de inserir os dados 1 a 1, repete consoante o numero de arrays que decidimos nas variaveis. Se alguém me conseguisse ajudar ficava agradecido Dim i As Integer = 0 Dim a As Integer = 0 Dim y, k As Integer Dim chamada As Integer Dim array1(0 To 10) As Integer Dim array2(0 To 10) As Integer Dim j1 As Integer = 0 Dim j2 As Integer = 0 ...... Sub pontos(ByRef v1 As Integer, ByRef v2 As Integer) If v1 > v2 Then MsgBox("O player 1 um ganhou com " & v1) result(j1) For Each y In array2 array2(y) = v1 ListBox1.Items.Add(v1) Next ' ElseIf v1 = v2 ' MsgBox("EMPATE") End If If v1 < v2 Then MsgBox("O player 2 um ganhou com " & v2) result(j2) For Each k In array1 array1(k) = v2 ListBox1.Items.Add(v2) Next ' ElseIf v1 = v2 'MsgBox("EMPATE") End If End Sub Edited January 10, 2016 at 12:04 AM by krykz Link to comment Share on other sites More sharing options...
vikcch Posted January 10, 2016 at 03:38 AM Report Share #591753 Posted January 10, 2016 at 03:38 AM não percebi bem qual é a tua duvida.... secalhar tens que limpar a listbox antes das comparações (If v1 > v2 Then) For Each y In array2 array2(y) = v1 ListBox1.Items.Add(v1) Next este codigo não faz sentido nenhum, o y aqui tem o valor do elemento do array, não é o indice... quando fazes array2(y) = v1 até devia estoirar se y for maior que 10..... queres adicionar o v1/v2 ao array? tipo na ultima casa livre? então o melhor é usares uma list, tambem dá com array mas é preciso mais codigo, do tipo inicializares o array com todos os elementos a -1, e quando fores vares o array e achares -1, esse é o livre e sais do ciclo.... como disse é um bocado dificil perceber o que queres fazer... podias colocar um exemplo do que está a acontecer e como queres que aconteça (com valores) Link to comment Share on other sites More sharing options...
krykz Posted January 10, 2016 at 11:49 AM Author Report Share #591762 Posted January 10, 2016 at 11:49 AM (edited) Boas, O programa resume-se em dois buttons, cada button dá random de um numero e coloca-o numa textbox, o numero que for mais alto deverá ser inserido na listbox e guardado em arrays. Vou deixar aqui uma print da form e do codigo. Public Class Form1 Dim i As Integer = 0 Dim a As Integer = 0 Dim y, k As Integer Dim chamada As Integer Dim array1() As Integer Dim array2() As Integer Dim j1 As Integer = 0 Dim j2 As Integer = 0 Sub pontos(ByRef v1 As Integer, ByRef v2 As Integer) If v1 > v2 Then MsgBox("O player 1 um ganhou com " & v1) result(j1) For Each y In array1 array1(y) = v1 ListBox1.Items.Add(array1(y)) Next ' ElseIf v1 = v2 ' MsgBox("EMPATE") End If If v1 < v2 Then MsgBox("O player 2 um ganhou com " & v2) result(j2) For Each k In array2 array2(k) = v2 ListBox1.Items.Add(array2(k)) Next ' ElseIf v1 = v2 'MsgBox("EMPATE") End If End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click i = dados() TextBox1.Text = i End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click If TextBox1.Text = "" Then MsgBox("Preencha todos os campos") Else a = dados() TextBox2.Text = a pontos(i, a) End If TextBox1.Clear() TextBox2.Clear() End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Close() End Sub Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click TextBox1.Clear() TextBox2.Clear() Label2.Text = "" Label3.Text = "" End Sub End Class Edited January 10, 2016 at 11:51 AM by krykz Link to comment Share on other sites More sharing options...
vikcch Posted January 10, 2016 at 01:55 PM Report Share #591764 Posted January 10, 2016 at 01:55 PM Sem uma duvida especifica fica dificil dizer mais alguma coisa... Fica aqui um exemplo de como eu faria esse programa: Controles: button1 = player 1 button2 = player 2 button3 = new game Public Class Form1 Private Const JOG_1 As Integer = 0 Private Const JOG_2 As Integer = 1 Private Const NR_RONDAS = 10 ' Array 2D Private resultados(1, NR_RONDAS - 1) As Integer Private ronda As Integer = 0 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Randomize() TextBox1.ReadOnly = True TextBox1.BackColor = Color.White TextBox2.ReadOnly = True TextBox2.BackColor = Color.White End Sub Private Sub calcularVencedor() Dim j1_pontos As Integer = resultados(JOG_1, ronda) Dim j2_pontos As Integer = resultados(JOG_2, ronda) Dim maxPontos As Integer = -1 Dim vencedor As String = String.Empty If j1_pontos > j2_pontos Then vencedor = "JOGADOR 1" maxPontos = j1_pontos ElseIf j1_pontos < j2_pontos Then vencedor = "JOGADOR 2" maxPontos = j2_pontos End If If j1_pontos = j2_pontos Then MsgBox("Empatados com " & j1_pontos & " pontos") ListBox1.Items.Add("#" & ronda + 1 & " - EMPATADOS - " & j1_pontos) Else MsgBox("Vencedor: " & vencedor & " - " & maxPontos & " pontos") ListBox1.Items.Add("#" & ronda + 1 & " - " & vencedor & " - " & maxPontos) End If End Sub Private Sub gerarJogada(ByVal jogador As Integer, ByRef txtBox As TextBox) If ronda >= NR_RONDAS Then MsgBox("Acabou o Jogo") Exit Sub End If Dim n As Integer = Int(Rnd() * 6) + 1 txtBox.Text = n resultados(jogador, ronda) = n If resultados(JOG_1, ronda) > 0 And resultados(JOG_2, ronda) > 0 Then calcularVencedor() ronda += 1 TextBox1.Text = String.Empty TextBox2.Text = String.Empty End If End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click gerarJogada(JOG_1, TextBox1) End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click gerarJogada(JOG_2, TextBox2) End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click 'Limpar array For i As Integer = 0 To NR_RONDAS - 1 resultados(JOG_1, i) = 0 resultados(JOG_2, i) = 0 Next ronda = 0 ListBox1.Items.Clear() TextBox1.Text = String.Empty TextBox2.Text = String.Empty End Sub End Class não devias usar nomes tipo button1, textbox1, listbox1, etc... as textbox's se não são para receber dados do utilizador não deveriam ser textbox's o array faz mais sentido ser dinâmico ou usar lists como referi no outro post o codigo não tem comentarios mas acho que dá pra perceber... Link to comment Share on other sites More sharing options...
krykz Posted January 10, 2016 at 02:12 PM Author Report Share #591766 Posted January 10, 2016 at 02:12 PM Dá para perceber muito bem , obrigado Link to comment Share on other sites More sharing options...
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