Esercizio 2
This commit is contained in:
@@ -26,8 +26,7 @@ public class LinkedList {
|
|||||||
do {
|
do {
|
||||||
System.out.println("Scegliere un'opzione:");
|
System.out.println("Scegliere un'opzione:");
|
||||||
System.out.println("1. Esercizio 1");
|
System.out.println("1. Esercizio 1");
|
||||||
System.out.println("2. ");
|
System.out.println("2. Esercizio 2");
|
||||||
System.out.println("3. ");
|
|
||||||
System.out.println("0. Esci");
|
System.out.println("0. Esci");
|
||||||
System.out.print("Opzione: ");
|
System.out.print("Opzione: ");
|
||||||
|
|
||||||
@@ -43,11 +42,7 @@ public class LinkedList {
|
|||||||
pausa();
|
pausa();
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
|
esercizio2();
|
||||||
pausa();
|
|
||||||
break;
|
|
||||||
case 3:
|
|
||||||
|
|
||||||
pausa();
|
pausa();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
@@ -95,7 +90,8 @@ public class LinkedList {
|
|||||||
System.out.println(listaCaratteriInseriti.toString());
|
System.out.println(listaCaratteriInseriti.toString());
|
||||||
listaCaratteriInseriti.sort();
|
listaCaratteriInseriti.sort();
|
||||||
|
|
||||||
for (int i = 0; i < listaCaratteriInseriti.getSize() && ('a' + i) < listaCaratteriInseriti.getElementAt(listaCaratteriInseriti.getSize() - 1); i++) {
|
for (int i = 0; i < listaCaratteriInseriti.getSize()
|
||||||
|
&& ('a' + i) < listaCaratteriInseriti.getElementAt(listaCaratteriInseriti.getSize() - 1); i++) {
|
||||||
if (listaCaratteriInseriti.getElementAt(i) != 'a' + i) {
|
if (listaCaratteriInseriti.getElementAt(i) != 'a' + i) {
|
||||||
listaCaratteriInseriti.addElementAt((char) ('a' + i), i);
|
listaCaratteriInseriti.addElementAt((char) ('a' + i), i);
|
||||||
}
|
}
|
||||||
@@ -104,4 +100,77 @@ public class LinkedList {
|
|||||||
System.out.println("Lista caratteri aggiunti:");
|
System.out.println("Lista caratteri aggiunti:");
|
||||||
System.out.println(listaCaratteriInseriti.toString());
|
System.out.println(listaCaratteriInseriti.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void esercizio2() {
|
||||||
|
String numero;
|
||||||
|
boolean error;
|
||||||
|
boolean exit;
|
||||||
|
Lista<Integer> numeri = new Lista<>();
|
||||||
|
Lista<Integer> pari = new Lista<>();
|
||||||
|
Lista<Integer> dispari = new Lista<>();
|
||||||
|
int somma = 0;
|
||||||
|
int media;
|
||||||
|
int scarto = 0;
|
||||||
|
int j = 0;
|
||||||
|
|
||||||
|
do {
|
||||||
|
error = false;
|
||||||
|
System.out.print("Inserire un numero(q per terminare l'inserimento): ");
|
||||||
|
numero = sc.nextLine();
|
||||||
|
exit = !numero.equals("q");
|
||||||
|
if (exit) {
|
||||||
|
try {
|
||||||
|
numeri.addLast(Integer.parseInt(numero));
|
||||||
|
} catch (NumberFormatException _) {
|
||||||
|
System.out.println("Errore: inserire un numero.");
|
||||||
|
pausa();
|
||||||
|
error = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} while (exit || error);
|
||||||
|
|
||||||
|
for (int i = 0; i < numeri.getSize(); i++) {
|
||||||
|
somma += numeri.getElementAt(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
media = somma / numeri.getSize();
|
||||||
|
|
||||||
|
do {
|
||||||
|
error = false;
|
||||||
|
System.out.print("Inserire lo scarto dalla media per eliminare gli elementi: ");
|
||||||
|
try {
|
||||||
|
scarto = sc.nextInt();
|
||||||
|
if (scarto <= 0) {
|
||||||
|
System.out.println("Error: inserire un numero positivo maggiore di uno.");
|
||||||
|
pausa();
|
||||||
|
}
|
||||||
|
} catch (InputMismatchException _) {
|
||||||
|
System.out.println("Errore: inserire un numero.");
|
||||||
|
pausa();
|
||||||
|
error = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
} while (error);
|
||||||
|
|
||||||
|
while (j < numeri.getSize()){
|
||||||
|
if (numeri.getElementAt(j) == (media - scarto) || numeri.getElementAt(j) == (media + scarto)) {
|
||||||
|
if (numeri.getElementAt(j) % 2 == 0) {
|
||||||
|
pari.addLast(numeri.getElementAt(j));
|
||||||
|
} else {
|
||||||
|
dispari.addLast(numeri.getElementAt(j));
|
||||||
|
}
|
||||||
|
numeri.removeElementAt(j);
|
||||||
|
j = -1;
|
||||||
|
}
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("Lista iniziale: ");
|
||||||
|
System.out.println(numeri.toString());
|
||||||
|
System.out.println("Lista pari: ");
|
||||||
|
System.out.println(pari.toString());
|
||||||
|
System.out.println("Lista dispari: ");
|
||||||
|
System.out.println(dispari.toString());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user