Advertisement
Guest User

ProximoPrimo

a guest
Mar 19th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.01 KB | None | 0 0
  1. package clase3;
  2. import java.util.*;
  3.  
  4. // nroAleatorio =  (int) (Math.random() * 100) + 1;
  5.  
  6.  
  7. public class ProximoPrimo {
  8.  
  9.     public static void main(String[] args)
  10.     {
  11.         boolean gano=false;
  12.         int nroAleatorio, nroJugador;
  13.         Scanner scan = new Scanner(System.in);
  14.         Random generador = new Random();
  15.         nroAleatorio = 1 + generador.nextInt(100);
  16.         System.out.println("nro Aleatorio: "+ nroAleatorio);
  17.        
  18.         while(!gano)
  19.         {
  20.         System.out.println("Ingrese el nro primo que le sigue: ");
  21.         nroJugador = scan.nextInt();
  22.        
  23.         if(proximoPrimo(nroAleatorio)==nroJugador)
  24.         {
  25.             System.out.println("Acerto");
  26.             gano = true;
  27.         }
  28.         else
  29.             System.out.println("Fallaste");
  30.         }
  31.     }
  32.    
  33.     static boolean esPrimo(int nro)
  34.     {
  35.         int cont = 0;
  36.         for(int x=1; x<=nro; x++)
  37.         {
  38.             if(nro%x == 0)
  39.                 cont += 1;
  40.         }
  41.         if(cont == 2)
  42.         {
  43.             return true;
  44.         }
  45.         else
  46.             return false;
  47.     }
  48.    
  49.     static int proximoPrimo(int aleatorio)
  50.     {
  51.         int i = aleatorio + 1 ;
  52.         while(!esPrimo(i))
  53.         {
  54.             i++;
  55.         }
  56.         return i;
  57.  
  58.     }
  59.  
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement