-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaterialExtraEjercicio7.java
85 lines (73 loc) · 2.53 KB
/
MaterialExtraEjercicio7.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
* Material Extra Ejercicio # 7
Realice un programa que calcule y visualice el valor máximo, el valor mínimo y el
promedio de n números (n>0). El valor de n se solicitará al principio del programa y
los números serán introducidos por el usuario. Realice dos versiones del programa,
una usando el bucle “while” y otra con el bucle “do - while”.
*/
package egg_ejercicios;
//import java.text.DecimalFormat;
import java.util.Scanner;
/**
*
* @author WALTER GOMEZ
*/
public class MaterialExtraEjercicio7 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner leer = new Scanner(System.in).useDelimiter("\n");
// DecimalFormat df = new DecimalFormat("###.##");
System.out.println("Ingrese el valor de N número a ingresar :");
int n = leer.nextInt();
int cont = 0, num, max = 0, min = 0, positivos = 0, suma = 0;
// el desarrollo con While
while(cont < n) {
System.out.println("Ingrese un numero");
num = leer.nextInt();
if(max == 0 && min == 0) {
max = num;
min = num;
}
if(num > max) {
max = num;
}
if(num < min) {
min = num;
}
if(num > 0) {
suma += num;
positivos += 1;
}
cont += 1;
}
System.out.println("El numero maximo fue: " + max);
System.out.println("El numero minimo fue: " + min);
System.out.println("El promedio fue: " +( suma / positivos));
// el desarrollo con Do - While
max = 0; min = 0; suma = 0; positivos = 0; cont = 0;
do{
System.out.println("Ingrese un numero");
num = leer.nextInt();
if(max == 0 && min == 0) {
max = num;
min = num;
}
if(num > max) {
max = num;
}
if(num < min) {
min = num;
}
if(num > 0) {
suma += num;
positivos += 1;
}
cont += 1;
} while(cont < n);
System.out.println("El numero maximo fue: " + max);
System.out.println("El numero minimo fue: " + min);
System.out.println("El promedio fue: " + (suma / positivos));
}
}