Logica e stampa
This commit is contained in:
parent
d9731037f2
commit
d6a8a7002d
@ -18,29 +18,68 @@ public class SequenzaNumeri {
|
||||
*/
|
||||
|
||||
static Scanner sc = new Scanner(System.in);
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
int daLeggere = -1;
|
||||
boolean error;
|
||||
int daLeggere = -1, i = 0, numeroCorrente, numeroPrecedente = 0, indicePositivi = 0, indiceNegativi = 0,
|
||||
indiceZero = 0, indicePari = 0, indiceDispari = 0;
|
||||
boolean error, isCrescente, isDecrescente;
|
||||
|
||||
System.out.println("Verrà chiesto di inserire la quantità di numeri da inserire.");
|
||||
Pausa();
|
||||
|
||||
do{
|
||||
do {
|
||||
error = false;
|
||||
daLeggere = InserimentoNumero();
|
||||
|
||||
if(daLeggere <= 0){
|
||||
if (daLeggere <= 0) {
|
||||
System.out.println("Errore: inserire un numero positivo.");
|
||||
Pausa();
|
||||
error = true;
|
||||
}
|
||||
} while (error);
|
||||
|
||||
System.out.println("Ora verranno chiesti i numeri:");
|
||||
Pausa();
|
||||
for (; i < daLeggere; i++) {
|
||||
numeroCorrente = InserimentoNumero();
|
||||
|
||||
if (numeroCorrente > 0) {
|
||||
indicePositivi++;
|
||||
} else if (numeroCorrente < 0) {
|
||||
indiceNegativi++;
|
||||
} else {
|
||||
indiceZero++;
|
||||
}
|
||||
while (error);
|
||||
|
||||
if (numeroCorrente % 2 == 0) {
|
||||
indicePari++;
|
||||
} else {
|
||||
indiceDispari++;
|
||||
}
|
||||
|
||||
if (numeroCorrente > numeroPrecedente) {
|
||||
isCrescente = true;
|
||||
isDecrescente = false;
|
||||
} else if (numeroCorrente < numeroPrecedente) {
|
||||
isDecrescente = true;
|
||||
isCrescente = false;
|
||||
}
|
||||
else {
|
||||
isCrescente = false;
|
||||
isDecrescente = false;
|
||||
}
|
||||
|
||||
System.out.println("Numeri positivi: " + indicePositivi);
|
||||
System.out.println("Numeri negativi: " + indiceNegativi);
|
||||
System.out.println("Numeri uguali a zero: " + indiceZero);
|
||||
System.out.println("Numeri pari: " + indicePari);
|
||||
System.out.println("Numeri dispari: " + indiceDispari);
|
||||
System.out.println("La sequenza " + (isCrescente ? "è crescente." : (isDecrescente ? "è decrescente." : "non è né crescente né decrescente")));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static void Pausa() {
|
||||
System.out.println("Premere un tasto per continuare. . .");
|
||||
sc.nextLine();
|
||||
@ -53,16 +92,14 @@ public class SequenzaNumeri {
|
||||
do {
|
||||
error = false;
|
||||
System.out.println("Inserire un numero: ");
|
||||
try{
|
||||
try {
|
||||
ritorno = sc.nextInt();
|
||||
}
|
||||
catch (InputMismatchException e) {
|
||||
} catch (InputMismatchException e) {
|
||||
System.out.println("Numero non valido.");
|
||||
Pausa();
|
||||
error = true;
|
||||
}
|
||||
}
|
||||
while (error);
|
||||
} while (error);
|
||||
return ritorno;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user