usar metodo leer entero
This commit is contained in:
1 parent
591c09e37a
commit
be1108d422
2 files changed
+42
-2
No files matched your search
+24
-1
@@ -1,10 +1,33 @@
|
|||||||
|
import java.util.*;
|
||||||
|
|
||||||
public class Metodos1{
|
public class Metodos1{
|
||||||
public static void main(String[] args){
|
public static void main(String[] args){
|
||||||
System.out.println(potencias2(17));
|
int numero=0;
|
||||||
|
System.out.println("Introduce un entero");
|
||||||
|
numero = leerEntero();
|
||||||
|
int resultado = potencias2(numero);
|
||||||
|
System.out.printf("El resultado es %d\n",resultado);
|
||||||
}
|
}
|
||||||
public static int potencias2(int n){
|
public static int potencias2(int n){
|
||||||
int result = 1;
|
int result = 1;
|
||||||
while(1 << result <= n) result++;
|
while(1 << result <= n) result++;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
public static int leerEntero() {
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
boolean parar = false;
|
||||||
|
int n = 0;
|
||||||
|
while(!parar) {
|
||||||
|
try {
|
||||||
|
n = sc.nextInt();
|
||||||
|
parar = true;
|
||||||
|
} catch(Exception e){
|
||||||
|
System.out.println("Número invalido, intentalo otra vez");
|
||||||
|
} finally {
|
||||||
|
sc.next();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sc.close();
|
||||||
|
return n;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
+18
-1
@@ -7,7 +7,7 @@ public class Metodos3 {
|
|||||||
int num = 0;
|
int num = 0;
|
||||||
while(!detener){
|
while(!detener){
|
||||||
System.out.println("Introduce un numero: ");
|
System.out.println("Introduce un numero: ");
|
||||||
num = sc.nextInt();
|
num = leerEntero();
|
||||||
if(num < 0) {
|
if(num < 0) {
|
||||||
System.out.println("Tiene que ser un numero positivo");
|
System.out.println("Tiene que ser un numero positivo");
|
||||||
} else detener = true;
|
} else detener = true;
|
||||||
@@ -20,4 +20,21 @@ public class Metodos3 {
|
|||||||
while(n > 0) result *= (double)n--;
|
while(n > 0) result *= (double)n--;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
public static int leerEntero() {
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
boolean parar = false;
|
||||||
|
int n = 0;
|
||||||
|
while(!parar) {
|
||||||
|
try {
|
||||||
|
n = sc.nextInt();
|
||||||
|
parar = true;
|
||||||
|
} catch(Exception e){
|
||||||
|
System.out.println("Número invalido, intentalo otra vez");
|
||||||
|
} finally {
|
||||||
|
sc.next();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sc.close();
|
||||||
|
return n;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user