forked from harambeisnotdead/java-carande
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clases1.java
62 lines (49 loc) · 1.5 KB
/
clases1.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import java.util.Scanner;
class clases1 {
public static void main(String[] args) {
ParPrimo n = new ParPrimo(introducirN());
int op;
do {
menu();
op = introducirN();
switch (op) {
case 1: n.par(); break;
case 2: n.primo(); break;
case 3: n = new ParPrimo(introducirN()); break;
}
} while (op!=4);
}
public static void menu() {
System.out.println("\n\t\033[4mMenu\033[0m\n");
System.out.println(" 1. Par");
System.out.println(" 2. Primo");
System.out.println(" 3. Dar numero");
System.out.println(" 4. Salir\n");
}
public static int introducirN() {
Scanner scanner = new Scanner(System.in);
try {
System.out.print("Introduce un numero: ");
return scanner.nextInt();
} catch (java.util.InputMismatchException e) { return introducirN(); }
}
}
class ParPrimo {
public int n;
public ParPrimo(int a) {
n = a;
}
public void par() {
if (n%2 == 0) System.out.println(n+" es par");
else System.out.println(n+" no es par");
}
public void primo() {
boolean primo = true;
for (int i=n-1 ; i>1 ; i--) {
System.out.println(n+" % "+i+" = "+n%i);
if (n%i==0) primo = false;
}
if (primo) System.out.println(n+" es primo");
else System.out.println(n+" no es primo");
}
}