Como valido que el numero ingresado se encuentra dentro del rango
Publicado por andre (15 intervenciones) el 19/06/2021 11:00:58
tengo este código como tomo los valores de range y las opciones desde los otros metodos y como valido que el numero ingresado se encuentra dentro del rango
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
public class ValidationMethods {
public static void main(String[] args) {
//Initialize local variables
Scanner sIn = new Scanner(System.in); //Input Scanner for String
int intNum = 0;
double doubleNum = 0;
String choice = "";
String playAgain = "Y";
do {
int[] intRange1 = {};
intNum = getValidInt(sIn, "ingrese numero entero ", "Invalido . solo ingrese enteros.",intRange1);
System.out.println("el entero es : "+ intNum);
System.out.println("Ahora probaremos tu número entero en una ecuación matemática...");
System.out.printf("Sumar 10 a su número entero sería:10 + %d = %d.\n\n",intNum, (intNum + 10));
//Get an integer within a range from the user
int[] intRange2 = {10, 50};
intNum = getValidInt(sIn, "Ingrese un número entero entre 10 y 50: ", "Invalido .solo enteros entre 10 y 50 son aceptados",intRange2);
System.out.println("el entero es: "+ intNum);
System.out.println("Ahora probaremos tu número entero en una ecuación matemática..");
System.out.printf("Sumar 10 a su número entero sería: 10 + %d = %d.\n\n",intNum,(intNum + 10));
//Get a Double from the user
double[] doubleRange1 = {};
doubleNum = getValidDouble(sIn, "ingrese numero float ", "Invalido . solo se aceptan numeros float",doubleRange1);
System.out.println("el numero float ingresado es : " + doubleNum);
System.out.println("Ahora probaremos tu número float en una ecuación matemática...");
System.out.printf("Sumar 10 a su número float sería: 10 + %f = %f.\n\n", doubleNum, (doubleNum + 10));
//Get a Double within a range from the user
double[] doubleRange2 = {5.5, 10.75};
doubleNum = getValidDouble(sIn, "Ingrese numero float entre 5.5 y 10.75: ", "Invalido solo se aceptan numeros float entre 5.5 y 10.75 ",doubleRange2);
System.out.println("el numero float ingresado es " + doubleNum);
System.out.println("Ahora probaremos tu número float en una ecuación matemática..");
System.out.printf("Sumar 10 a su número float sería:10 + %f = %f.\n\n", doubleNum, (doubleNum + 10));
//Get either an "a", "b" or "c" from the user
String[] options1 = {"a","b","c"};
choice = getValidString(sIn, "Ingrese 'a', 'b' or 'c': ", "Invalido solo se aceptan lasletras 'a', 'b' or 'c' ", options1);
System.out.printf("la letra ingresada es: %s\n\n", choice);
//Get a word from the user
String[] options2 = {"Chocolate","Vanilla","Strawberry"};
choice = getValidString(sIn, "Ingrese Chocolate, Vanilla or Strawberry: ", "Invalido. solo ingresar 'Chocolate', 'Vanilla' and 'Strawberry' are acceptable answers.",options2);
System.out.printf("You chose: %s\n\n", choice);
//Get a 'Y' or 'N' from the user
String[] options3 = {"Y","y","N","n"};
playAgain = getValidString(sIn, "Desea jugar de nuevo? (Y/N): ", "Invalido. ingresar solo 'Y' o 'N'", options3);
}while(playAgain.equalsIgnoreCase("Y"));
}
/**getValidInt method validates user input is an Integer within range and returns it back to the calling method.
* Uses sIn to get user input from the console.
* Asks user question.
* If range is empty, range is ignored and only validates input is an integer.
* If range is not empty, validates user input is an integer within range.
* If user input is not valid, prints warning and repeats question.
* Returns validated input.
*/
public static int getValidInt(Scanner sIn, String question, String warning, int[] range){
int ban;
int b = 0;
do{
ban=0;
try {
System.out.println(question);
b = sIn.nextInt();
}catch(Exception e){System.out.println(warning);ban=1;sIn.nextLine();}
}
while(ban!=0);
return b;
}
public static double getValidDouble(Scanner sIn, String question, String warning, double[] range){
int ban;
double c = 0;
do{
ban=0;
try {
System.out.println(question);
c = sIn.nextDouble();
}catch(Exception e){System.out.println(warning);ban=1;sIn.nextLine();}
}
while(ban!=0);
return c;
}
public static String getValidString(Scanner sIn, String question, String warning, String[] choices){
int ban;
String c = "";
do{
ban=0;
try {
System.out.println(question);
c = sIn.nextLine();
}catch(Exception e){System.out.println(warning);ban=1;sIn.nextLine();}
}
while(ban!=0);
return c;
}
}
Valora esta pregunta


0