Logica e stampa
This commit is contained in:
parent
d9731037f2
commit
d6a8a7002d
@ -18,10 +18,12 @@ public class SequenzaNumeri {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
static Scanner sc = new Scanner(System.in);
|
static Scanner sc = new Scanner(System.in);
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
int daLeggere = -1;
|
int daLeggere = -1, i = 0, numeroCorrente, numeroPrecedente = 0, indicePositivi = 0, indiceNegativi = 0,
|
||||||
boolean error;
|
indiceZero = 0, indicePari = 0, indiceDispari = 0;
|
||||||
|
boolean error, isCrescente, isDecrescente;
|
||||||
|
|
||||||
System.out.println("Verrà chiesto di inserire la quantità di numeri da inserire.");
|
System.out.println("Verrà chiesto di inserire la quantità di numeri da inserire.");
|
||||||
Pausa();
|
Pausa();
|
||||||
@ -35,12 +37,49 @@ public class SequenzaNumeri {
|
|||||||
Pausa();
|
Pausa();
|
||||||
error = true;
|
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() {
|
static void Pausa() {
|
||||||
System.out.println("Premere un tasto per continuare. . .");
|
System.out.println("Premere un tasto per continuare. . .");
|
||||||
sc.nextLine();
|
sc.nextLine();
|
||||||
@ -55,14 +94,12 @@ public class SequenzaNumeri {
|
|||||||
System.out.println("Inserire un numero: ");
|
System.out.println("Inserire un numero: ");
|
||||||
try {
|
try {
|
||||||
ritorno = sc.nextInt();
|
ritorno = sc.nextInt();
|
||||||
}
|
} catch (InputMismatchException e) {
|
||||||
catch (InputMismatchException e) {
|
|
||||||
System.out.println("Numero non valido.");
|
System.out.println("Numero non valido.");
|
||||||
Pausa();
|
Pausa();
|
||||||
error = true;
|
error = true;
|
||||||
}
|
}
|
||||||
}
|
} while (error);
|
||||||
while (error);
|
|
||||||
return ritorno;
|
return ritorno;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user