Main, stampe, Pausa e Dimensione

This commit is contained in:
La Programmatrice Verde 2025-09-30 20:59:08 +02:00
parent a471157e9a
commit e65915a5de

View File

@ -4,6 +4,9 @@
*/ */
package quadrati_vari; package quadrati_vari;
import java.util.InputMismatchException;
import java.util.Scanner;
/** /**
* *
* @author Verde * @author Verde
@ -13,24 +16,26 @@ public class Quadrati_vari {
/** /**
* @param args the command line arguments * @param args the command line arguments
*/ */
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) { public static void main(String[] args) {
Console.Clear();
char unit = '*'; //unità usata per stampare le figure char unit = '*'; //unità usata per stampare le figure
char unit2 = '+'; //unità usata per stampare metà dell'ultima figura char unit2 = '+'; //unità usata per stampare metà dell'ultima figura
int scelta = -1; int scelta = -1;
do { do {
Console.WriteLine("Scegliere un'opzione:"); System.out.println("Scegliere un'opzione:");
Console.WriteLine("1. Stampa quadrato pieno"); System.out.println("1. Stampa quadrato pieno");
Console.WriteLine("2. Stampa quadrato vuoto"); System.out.println("2. Stampa quadrato vuoto");
Console.WriteLine("3. Stampa triangolo"); System.out.println("3. Stampa triangolo");
Console.WriteLine("4. Stampa quadrato diagonale"); System.out.println("4. Stampa quadrato diagonale");
Console.WriteLine("0. Esci"); System.out.println("0. Esci");
Console.Write("Scelta: "); System.out.println("Scelta: ");
try { try {
scelta = Convert.ToInt32(Console.ReadLine()); scelta = sc.nextInt();
sc.nextLine();
switch (scelta) { switch (scelta) {
case 0: case 0:
@ -52,14 +57,14 @@ public class Quadrati_vari {
Pausa(); Pausa();
break; break;
default: default:
Console.WriteLine("Errore: scelta non valida."); System.out.println("Errore: scelta non valida.");
Pausa(); Pausa();
break; break;
} }
} }
catch (FormatException) { catch (InputMismatchException e) {
Console.WriteLine("Errore: scelta non valida."); System.out.println("Errore: scelta non valida.");
Pausa(); Pausa();
} }
@ -68,29 +73,29 @@ public class Quadrati_vari {
} }
static void Pausa() { static void Pausa() {
Console.WriteLine("Premere un tasto per continuare. . ."); System.out.println("Premere un tasto per continuare. . .");
Console.ReadKey(); sc.nextLine();
} }
static uint Dimensione() { //restituisce un intero che indica la dimensione di un lato della figura static int Dimensione() { //restituisce un intero che indica la dimensione di un lato della figura
uint ritorno = 0; int ritorno = 0;
bool showErrorMessage; boolean showErrorMessage;
do { do {
showErrorMessage = false; showErrorMessage = false;
Console.Write("Inserire la dimensione della figura: "); System.out.println("Inserire la dimensione della figura: ");
try { try {
ritorno = Convert.ToUInt32(Console.ReadLine()); ritorno = sc.nextInt();
if (ritorno == 0) { if (ritorno <= 0) {
showErrorMessage = true; showErrorMessage = true;
} }
} }
catch (Exception) { catch (InputMismatchException e) {
showErrorMessage = true; showErrorMessage = true;
} }
if (showErrorMessage) { if (showErrorMessage) {
Console.WriteLine("Errore: inserire un numero superiore a 0."); System.out.println("Errore: inserire un numero superiore a 0.");
} }
} while (showErrorMessage); } while (showErrorMessage);
return ritorno; return ritorno;
@ -102,7 +107,7 @@ public class Quadrati_vari {
for (int j = 1; j <= p_dimensione; j++) { for (int j = 1; j <= p_dimensione; j++) {
Console.Write(p_unit); Console.Write(p_unit);
} }
Console.WriteLine(); System.out.println();
} }
} }
@ -112,7 +117,7 @@ public class Quadrati_vari {
for (int j = 1; j <= p_dimensione; j++) { for (int j = 1; j <= p_dimensione; j++) {
Console.Write(p_unit); Console.Write(p_unit);
} }
Console.WriteLine(); System.out.println();
} }
else { else {
for (int j = 0; j <= p_dimensione - 2; j++) { for (int j = 0; j <= p_dimensione - 2; j++) {
@ -121,7 +126,7 @@ public class Quadrati_vari {
} }
Console.Write(' '); Console.Write(' ');
} }
Console.WriteLine(); System.out.println();
} }
} }
} }
@ -131,7 +136,7 @@ public class Quadrati_vari {
for (int j = 1; j <= i; j++) { for (int j = 1; j <= i; j++) {
Console.Write(p_unit); Console.Write(p_unit);
} }
Console.WriteLine(""); System.out.println("");
} }
} }
@ -144,7 +149,7 @@ public class Quadrati_vari {
for (int k = (int)p_dimensione - --j; k > 0; k--) { for (int k = (int)p_dimensione - --j; k > 0; k--) {
Console.Write(p_unit2); Console.Write(p_unit2);
} }
Console.WriteLine(""); System.out.println("");
} }
} }
} }