lamuria Posted November 15, 2009 at 04:23 PM Report Share #296324 Posted November 15, 2009 at 04:23 PM pessoal tenho aqui um código fonte em que o o método criado não está a fazer nada. alguém me pode ajudar solucionar o problema? o objectivo do programa é: inserir um intervalo de valores e dps o método deveria retornar um valor dentro do intervalo que eu inseri. import java.util.*; public class ex2{ public static int genRandom(int inf, int sup){ int Randomvalue= ((int)Math.random())*100; if(inf<=Randomvalue && Randomvalue<=sup){ System.out.println(inf+"<="+Randomvalue+"<="+sup); } return Randomvalue; } public static void main(String[] args){ int inf, sup; Scanner input= new Scanner (System.in); inf = input.nextInt(); sup = input.nextInt(); int valores= genRandom(inf, sup); } } obrigado EDIT: Assunto modificado! Link to comment Share on other sites More sharing options...
KarlMTC Posted November 15, 2009 at 04:56 PM Report Share #296331 Posted November 15, 2009 at 04:56 PM import java.util.*; public class ex2 { public static void main(String[] args) { int inf, sup; Scanner teclado = new Scanner (System.in); System.out.println("Digite o número mais baixo do intervalo: "); inf = teclado.nextInt(); teclado.nextLine(); //limpa o buffer System.out.println("Digite o número mais alto do intervalo: "); sup = teclado.nextInt(); teclado.nextLine(); //limpa o buffer System.out.println("Número aleatorio dentro do intervalo: "); System.out.println( genRandom(inf, sup) ); } public static int genRandom(int inf, int sup) { int intervaloDeValores = sup - inf; int randomvalue = (int)(Math.random() * intervaloDeValores) + inf; return randomvalue; } } A explicação é simples: crias um valor aleatorio que esteja dentro do tramo (superior menos inferior) e depois somas o valor inferior para que o valor mínimo do aleatorio seja o mínimo do tramo. Link to comment Share on other sites More sharing options...
Damon4hire Posted November 15, 2009 at 08:23 PM Report Share #296374 Posted November 15, 2009 at 08:23 PM Que tal assim? import java.util.Random; ----- ----- Random rdm = new Random(); int numero = rdm.nextInt(maximo) + minimo; o nextInt vai.t dar um numero entre 0 e maximo, e depois acrescentas o minimo. Link to comment Share on other sites More sharing options...
vasco16 Posted November 15, 2009 at 08:42 PM Report Share #296376 Posted November 15, 2009 at 08:42 PM Que tal assim? import java.util.Random; ----- ----- Random rdm = new Random(); int numero = rdm.nextInt(maximo) + minimo; o nextInt vai.t dar um numero entre 0 e maximo, e depois acrescentas o minimo. Acho que o objectivo dele é arranjar um numero aletoario dentro de um intervalo especifico. Link to comment Share on other sites More sharing options...
Damon4hire Posted November 15, 2009 at 08:55 PM Report Share #296378 Posted November 15, 2009 at 08:55 PM sim eu sei. O nextInt(50), neste caso, gera-te um numero aleatorio entre 0 e 49, porque a contar com o zero faz 50 numeros. A ideia é acrescentar o valor do inicio do intervalo ao resultado. Por exemplo, queremos um numero entre 1 e 40. Fazemos: int variavel = rdm.nextInt(40) + 1; O nextInt(40) vai-te dar um numero entre 0 e 39, e acrescentas 1 ao resultado, para se der 0 ele passa a 1, ou se der 39 passa a 40. Link to comment Share on other sites More sharing options...
vasco16 Posted November 15, 2009 at 09:04 PM Report Share #296381 Posted November 15, 2009 at 09:04 PM sim eu sei. O nextInt(50), neste caso, gera-te um numero aleatorio entre 0 e 49, porque a contar com o zero faz 50 numeros. A ideia é acrescentar o valor do inicio do intervalo ao resultado. Por exemplo, queremos um numero entre 1 e 40. Fazemos: int variavel = rdm.nextInt(40) + 1; O nextInt(40) vai-te dar um numero entre 0 e 39, e acrescentas 1 ao resultado, para se der 0 ele passa a 1, ou se der 39 passa a 40. e se quiseres entre 30 e 78 ? Link to comment Share on other sites More sharing options...
Damon4hire Posted November 15, 2009 at 09:12 PM Report Share #296384 Posted November 15, 2009 at 09:12 PM Random rdm = new Random(); it var = rdm.nextInt(49) + 30; se der 0, fica 0 + 30 = 30, se der 48 (49 nao conta porque com 0 faz 49 numeros), fica 48 + 30 = 78. Link to comment Share on other sites More sharing options...
vasco16 Posted November 15, 2009 at 09:59 PM Report Share #296395 Posted November 15, 2009 at 09:59 PM Random rdm = new Random(); it var = rdm.nextInt(49) + 30; se der 0, fica 0 + 30 = 30, se der 48 (49 nao conta porque com 0 faz 49 numeros), fica 48 + 30 = 78. Acho que existe um metodo mais simples de fazer isto, mas se funciona obrigado. 🙂 Link to comment Share on other sites More sharing options...
Damon4hire Posted November 15, 2009 at 10:07 PM Report Share #296397 Posted November 15, 2009 at 10:07 PM lol se houver com menos d 2 linhas d codigo, diz-me. 🙂😛🙂 Link to comment Share on other sites More sharing options...
Djizasse Posted November 17, 2009 at 12:09 PM Report Share #296636 Posted November 17, 2009 at 12:09 PM lol se houver com menos d 2 linhas d codigo, diz-me. 😄🙂😛 (new java.util.Random()).nextInt(49)+30; :b Link to comment Share on other sites More sharing options...
vasco16 Posted November 17, 2009 at 12:46 PM Report Share #296639 Posted November 17, 2009 at 12:46 PM (new java.util.Random()).nextInt(49)+30; :b LOL 😛 Link to comment Share on other sites More sharing options...
Damon4hire Posted November 17, 2009 at 04:46 PM Report Share #296663 Posted November 17, 2009 at 04:46 PM (cai que nem 1 patinho lol) 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