Como 'transformar de números para nomes', para rodar isso no Celular?

3 respostas
S

oi gente.
assim.. esse código foi feito em Eclipse, por um amigo meu, mas como naõ tenho noção nenhuma de eclipse resolvi pedir ajuda.

entendo um pouco de NetBeans, mas não estou conseguindo faze-lo rodar no JavaME.

alguém pode me dizer quais são os problemas?

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.swing.JOptionPane;

public class Sorteio2 {
	public static void main(String []args){
		inicio();
    }

	private static void inicio() {
		int numeroJogadores;
		do {
    		numeroJogadores = Integer.parseInt(JOptionPane.showInputDialog("Número de Jogadores: "));
    		if (numeroJogadores < 8) {
    			JOptionPane.showMessageDialog(null, "Favor Informar Numero maior que 8.");
    		} else {
    			calcular(numeroJogadores);
    		}	
    	} while(numeroJogadores < 8);		
	}

	private static void calcular(int numeroJogadores) {
		List<String> lista = new ArrayList<String>();
		List<String> listaFinal = new ArrayList<String>();
		int jogadoresCampo;
			
		for (int i = 0; i < numeroJogadores; i++) {
    		String nomeTemporario = JOptionPane.showInputDialog("Digite o Nome: ");
        	lista.add (nomeTemporario) ;
        }
    	Collections.shuffle(lista);
    	do {
    		jogadoresCampo = Integer.parseInt(JOptionPane.showInputDialog("Quantidade de Jogadores em campo: "));
    		if (jogadoresCampo < (numeroJogadores / 2)) {
    			JOptionPane.showMessageDialog(null, "Favor Informar um número menor ou igual à metade dos jogadores cadastrados.");
    		} else {
    			for (int j = 0; j < jogadoresCampo; j ++) {
    	    		Set sorteados = new HashSet();
    	    		String aux = "";
    	    		boolean novo = false;  
    	            
    	    		while (!novo) {
    	            	aux = lista.get(j);  
    		            if (!sorteados.contains(aux)) {
    		            	if (j < (jogadoresCampo / 2)) {
    		            		listaFinal.add("\n" + "Time 1: " + aux);
    		            		novo = true;  
        		                sorteados.add(aux);
    		            	} else {
	    		            	listaFinal.add("\n" + "Time 2: " + aux);
	    		            	novo = true;  
	    		                sorteados.add(aux);
    		            	}
    		            }
    	            }
    	    	}
    		}
    	} while (jogadoresCampo < (numeroJogadores / 2) );
    	
    	JOptionPane.showMessageDialog(null, listaFinal);
    	
    }
}

obrigado; :wink:

3 Respostas

E

No JavaME você não tem Swing, e o esquema é um pouco diferente do esquema do Java SE. Provavelmente você terá de pegar uma apostila de programação de JavaME mesmo, em vez de tentar rodar esse programa com “casca e tudo”. Dica: como há muito menos classes no JavaME, você terá de reescrever mais algumas coisas, não só a parte do Swing.

InicianteJavaHenriqu

Na verdade você só vai usar a lógica deste código (e sem ArrayList, pq J2ME não suporta, se eu estiver errado me corrijam)

S

então como poderia fazer para transformar isso, em vez de números,
sortear em uma lista de nomes digitados em um TField ou algo assim como no código anterior?

public class MSorteio {

    public static int[] getSorteio(int end) {

        int current = getRandomInt(end);
        int n = 0;

        // fill the array
        int[] foo = new int[end];
        for (int i = 0; i < foo.length; i++) {
            foo[i] = i + 1;
        }

        // fill the target
        int[] tgt = new int[end];
        while (n < tgt.length) {
            if (foo[current] >= 0) {
                tgt[n] = foo[current];
                foo[current] = -1;
                current = jump(foo.length, current, getNextJump(end));
                n++;
            } else {
                current = jump(foo.length, current, 1);
            }
        }

        return tgt;
    }


    /**
     * return the next random jump in the list
     *
     * @param end
     * @return
     */
    private static int getNextJump(int end) {
        int jump = getRandomInt(end);

        if (jump <= 0) {
            jump = 3;
        }

        if (jump >= end) {
            jump = end - 1;
        }

        return jump;
    }

    private static int jump(int length, int current, int jump) {
        current = current + jump;
        if (current >= length) {
            current = current - length;
        }
        return current;
    }

    private static int SEED_POS = 0;
    private static final int[] SEED = { 3456, 3245, 7768, 1123, 9875, 2345, 2387, 2378 };
    private static final int LIMIT = 9999;
    private static final int LIMIT_SIZE = Integer.toString(LIMIT).length();

    /**
     * Return a random integer, from zero to the informed value as limit
     *
     * @param limit
     * @return
     */
    public static int getRandomInt(int limit) {
        if (limit > LIMIT) {
            throw new RuntimeException("Number bigger than the random limit");
        }

        int factor = LIMIT / limit;
        int r = getRandomInt();
        int foo = r / factor;
        return foo;
    }

    /**
     * Get the last N numbers of the current time
     *
     * @return
     */
    public static int getRandomInt() {
        long a = System.currentTimeMillis() + SEED[SEED_POS++];
        if (SEED_POS >= SEED.length) {
            SEED_POS = 0;
        }
        String s = Long.toString(a);
        return Integer.parseInt(s.substring(s.length() - LIMIT_SIZE));
    }
}

desculpem minha ignorância pra essas coisas.

Criado 24 de outubro de 2011
Ultima resposta 25 de out. de 2011
Respostas 3
Participantes 3