Ingresar tantos números como desee en un arraylist
Publicado por andre (15 intervenciones) el 10/06/2021 09:20:05
escriba un programa que permita al usuario ingresar tantos números como desee en un arraylist y que finalice cuando el usuario presione enter
en mi caso logre hacer que ingrese solo 5 números pero me gustaría poder ingresar cualquier cantidad de números y que al final me despliegue la suma de todos los números ingresados
en mi caso logre hacer que ingrese solo 5 números pero me gustaría poder ingresar cualquier cantidad de números y que al final me despliegue la suma de todos los números ingresados
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
public class ArrayListSum {
public static void main(String[] args) {
// TODO Auto-generated method stub
int maxo=0;
String nombre="";
String entradaTeclado = "";
Scanner scanner =new Scanner(System.in);
int ban,num1 = 0;
do {
int[] arr=new int[5];
//do ingrese numero
do {
ban=0;
try {
for(int i=0;i<arr.length;i++)
{
System.out.println("ingrese numero ");
arr[i]=scanner.nextInt();
}
System.out.println("Los números son ");
int j=0;
int h1=0;
while(j<=4) {
System.out.println(+h1+" ["+arr[j]+"]");
j++;
h1++;
}
}catch(Exception e){System.out.println("Invalid answer please enter a whole number");ban=1;scanner.nextLine();}
}
while(ban!=0);
//llamada a metodos
int total = 0;
maxo=sumValues(total,arr);
//continua
do {
System.out.println("Do you wish to continue? answer 'Y' or 'N'");
Scanner entradaEscaner = new Scanner (System.in);
entradaTeclado = entradaEscaner.nextLine ();
if(entradaTeclado.equals("y")||entradaTeclado.equals("Y"))
{
}
else if(entradaTeclado.equals("n")||entradaTeclado.equals("N"))
{
System.out.println("finished program");
System.exit(0);
}
else
{
System.out.println("Invalid Answer! Reply with a 'Y' or 'N'");
}
}
while(!entradaTeclado.equals("y")&&!entradaTeclado.equals("Y"));
}
while(entradaTeclado.equals("y")||entradaTeclado.equals("Y"));
}
//metodo 2
static int sumValues(int total,int[] arr)
{
int[] sum=arr;
for(int i=0;i<arr.length;i++) {
total+=arr[i];
}
System.out.println("la suma es "+total);
return total;
}
}
Valora esta pregunta


0