View difference between Paste ID: 8dfFim8h and BikTDLND
SHOW: | | - or go back to the newest paste.
1
package tp0Modulo;
2
 
3
import java.util.Scanner;
4
 
5
public class Punto4 {
6
 
7
    public static void main(String[] args) {
8
        Scanner sc = new Scanner(System.in);
9
        Punto4Primos obj = new Punto4Primos();
10
        int numero = validarNumero(sc, "ingrese un numero positivo");
11
 
12
        System.out.println("el numero " + numero + " es primo? " + obj.esPrimo(numero));
13
 
14
    }
15
 
16
    public static int validarNumero(Scanner valorIngresado, String mensaje) {
17
        int numero;
18
        String linea;
19
        while (true) {
20
            try {
21
                System.out.println(mensaje);
22
                linea = valorIngresado.nextLine();
23
                numero = Integer.parseInt(linea);
24
                break;
25
            } catch (Exception e) {
26
                System.out.println("ERROR!!! INGRESE UN NUMERO POSITIVO");
27
            }
28
        }
29
        return numero;
30
    }
31
}
32
33
########esta parte copiar en una nueva clase ########
34
35
package tp0Modulo;
36
 
37
public class Punto4Primos {
38
 
39
    public boolean esPrimo(int x) {
40
        boolean primo = true;
41
        if (x == 1 || ((x % 2) == 0 && x > 2)) {
42
            primo = false;
43
 
44
        } else {
45
            for (int i = 2; i < (x / 2); i++) {
46
                if (x % i == 0) {
47
                    primo = false;
48
                    break;
49
                }
50
            }
51
        }
52
        return primo;
53
    }
54
 
55
}