Jsp + html

4 respostas
W

Gente Boa Tarde.
Estou meio com urgência então nem vou me aprensentar.

Mas fiz um programa (inda to na facul :D) fazendo com que qualquer valor digitado retornasse os mesmos em ordem crescente ou decrescente (dependendo da escolha do usuário) e o professor pediu como um “extra” para que os valores retornados no vetor aparecessem em uma tabela, mas ta impossivel de eu consiguir isso.

Aki vai como esta por enquanto:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>        
        <%               
        String nome = request.getParameter("nome");
        String op = request.getParameter("op");
        String vet[] = nome.split(",");
        int vet1[] = new int[vet.length];
        for(int i = 0 ;i<vet.length;i++)
        {
            vet1[i] =  Integer.parseInt(vet[i]);
        }
        for (int j=0; j <vet1.length;j++)
        {
            if (op.equals("1"))
            {
                for(int i=0 ;i<vet1.length-1;i++)
                {
                    if (vet1[i] > vet1[i+1])
                    {
                    int aux = vet1[i];
                    vet1[i] = vet1[i+1];
                    vet1[i+1] = aux;                
                    }
                }
            }
             if (op.equals("2"))
            {
                for(int i=0 ;i<vet1.length-1;i++)
                {
                    if (vet1[i] < vet1[i+1])
                    {
                    int aux = vet1[i];
                    vet1[i] = vet1[i+1];
                    vet1[i+1] = aux;                
                    }
                }
            }
        }
        for (int i= 0; i<vet1.length;i++)
            {                    
            out.println("<br>Vetor ["+i+"]="+vet1[i]);            
            }      
          %>           
    </body>
</html>

Obrigado

4 Respostas

julianolandim

boa noite willbre10,
Para fazer isso é só você usar uma table (em html) no seu for.
Antes do for você abre a tag e no meio do for você coloca as tr e as td para criar as linhas e as colunas.
Então vamos lá, eu refiz o teu código é so você testar e principalmente entender o que eu fiz.

<html>  
    <head>  
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
    </head>  
    <body>          
        <%                 
        String nome = request.getParameter("nome");  
        String op = request.getParameter("op");  
        String vet[] = nome.split(",");  
        int vet1[] = new int[vet.length];  
        for(int i = 0 ;i<vet.length;i++)  
        {  
            vet1[i] =  Integer.parseInt(vet[i]);  
        }  
        for (int j=0; j <vet1.length;j++)  
        {  
            if (op.equals("1"))  
            {  
                for(int i=0 ;i<vet1.length-1;i++)  
                {  
                    if (vet1[i] > vet1[i+1])  
                    {  
                    int aux = vet1[i];  
                    vet1[i] = vet1[i+1];  
                    vet1[i+1] = aux;                  
                    }  
                }  
            }  
             if (op.equals("2"))  
            {  
                for(int i=0 ;i<vet1.length-1;i++)  
                {  
                    if (vet1[i] < vet1[i+1])  
                    {  
                    int aux = vet1[i];  
                    vet1[i] = vet1[i+1];  
                    vet1[i+1] = aux;                  
                    }  
                }  
            }  
        } 
		%>
        //***** aqui foi a parte que modifiquei.
        <br>
        <table width="300" border="1" cellspacing="0" cellpadding="0">
        <% 
        for (int i= 0; i<vet1.length;i++)  
            {                     
            out.println("<tr><td>Vetor ["+i+"]="+vet1[i]+"</td></tr>");              
            }        
          %>
        </table>
        <br>
    </body>  
</html>

Espero que eu tenha ajudado e que seja isso mesmo que você esteja precisando.
T+

julianolandim

Lembre-se que você pode abrir a tag jsp e fechar aonde você quiser no seu HTML

W

Isso eu nao sabia.

Eu so meio cabeça dura e isso é bom, consegui faze sem olha isso que vc me mando, até pq eu queria algo diferente.
Eu queria o nome e a posição do vetor em cima e seu valor embaixo, eu consigui, mas para isso precisei separar os FOR. ficando assim:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body> 
        <table border ="1">         
              
        <%               
        String nome = request.getParameter("nome");
        String op = request.getParameter("op");
        String vet[] = nome.split(",");
        int vet1[] = new int[vet.length];
        for(int i = 0 ;i<vet.length;i++)
        {
            vet1[i] =  Integer.parseInt(vet[i]);
        }
        for (int j=0; j <vet1.length;j++)
        {
            if (op.equals("1"))
            {
                for(int i=0 ;i<vet1.length-1;i++)
                {
                    if (vet1[i] > vet1[i+1])
                    {
                    int aux = vet1[i];
                    vet1[i] = vet1[i+1];
                    vet1[i+1] = aux;                
                    }
                }
            }
             if (op.equals("2"))
            {
                for(int i=0 ;i<vet1.length-1;i++)
                {
                    if (vet1[i] < vet1[i+1])
                    {
                    int aux = vet1[i];
                    vet1[i] = vet1[i+1];
                    vet1[i+1] = aux;                
                    }
                }
            }
        }
       out.println ("<tr>");
        for (int i= 0; i<vet1.length;i++)
            {                    
            out.println("<td>Vetor ["+i+"] </td>");               
            }      
       out.println ("</tr><tr>");
            for (int i= 0; i<vet1.length;i++)
            {                    
            out.println("<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+vet1[i]+"</td>");         
            }
       out.println ("</td>");
          %>     
          </td>            
        </table>           
    </body>
</html>

Nao sei se tem coisa a mais ou a menos, só sei que rodo como eu queria. Muito Obrigado :D.

julianolandim

ja que você fez sem olhar é melhor ainda, sinal que você conseguiu entender.
se funcionou como você queria é sinal que esta certo. :thumbup:

Criado 8 de setembro de 2011
Ultima resposta 8 de set. de 2011
Respostas 4
Participantes 2