Ich habe Probleme mit der Codierung. Ich versuche, ein Programm zu machen, das einen Benutzer auffordert, Höhe und Breite für eine Form einzugeben. Wenn ich bedenke, dass ich eine Java-Klasse nehme, bin ich ein Neuling. Es muss zwei parallele Form von Sternchen geben, kann ein Quadrat oder ein Rechteck sein.Java Hausaufgaben Probleme Sternchen
Danke!
Der Code, den ich bisher habe irgendwie in
import java.util.Scanner;
public class rectangle {
public static void main(String... args) {
int recHeight = 0;
int recWidth = 0;
Scanner input = new Scanner(System.in);
do {
System.out.print("Enter height [-1 to quit] >> ");
recHeight = input.nextInt();
if (recHeight == -1) {
System.exit(0);
}
/* check if number is valid */
if (recHeight < 2 || recHeight > 24) {
System.err.println("--Error: please enter a valid number");
continue; // prompt again
System.out.print("Enter width [-1 to quit] >> ");
recWidth = input.nextInt();
if (recWidth == -1) {
System.exit(0);
}
/* check if number is valid */
if (recWidth < 2 || recWidth > 24) {
System.err.println("--Error: please enter a valid number");
continue; // prompt again
}
for (int col = 0; col < recHeight; col++) {
for (int row = 0; row < recWidth; row++) {
/* First or last row ? */
if (row == 0 || row == recWidth - 1) {
System.out.print("*");
if (row == recWidth - 1) {
System.out.println(); // border reached start a new line
}
} else { /* Last or first column ? */
if (col == recHeight - 1 || col == 0) {
System.out.print("*");
if (row == recWidth - 1) {
System.out.println();
}
} else {
System.out.print(" ");
if (row == recWidth - 1) {
System.out.println();
}
}
}
}
}
}
} while (true);
}
}
zu töten Was ist Ihr Problem? Welches Ergebnis erwartest du und was bekommst du? –
[Wie funktioniert die Annahme einer Antwort?] (Https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – shmosel