Problem med simpelt java program, som skal oprette firkant af asteriks.
hej EksperterLæser h. Deitels Java - How to program 8.'ed.
Opg. 4,26
Opgaven lyder at man skal lave et program som udskriver en firkant af "*"-tegn.
Jeg kan lave, toppen, bunden og den venstre side, men kan ikke få den til at lave højre side, hvad gør jeg galt?
package JAVA.HTP8E;
import java.util.Scanner;
public class SquareOfAsterisks
{
public static void main( String[] args )
{
Scanner input = new Scanner( System.in );
int counter1 = 0;
int counter2 = 0;
int sidelenght;
int height;
System.out.println("Please enter the lenght of the side of the square (1-20): ");
sidelenght = input.nextInt();
height = sidelenght-2;
System.out.println("");
while (counter1 < sidelenght)
{
System.out.print("*");
counter1++;
}
System.out.println("");
counter1 = 0;
while (counter1 < height)
{
while (counter2 < sidelenght)
{
if (counter2 == 0 )
{
System.out.print("*");
}
else if (counter2 != sidelenght )
{
System.out.print(" ");
}
else
{
System.out.print("*");
}
counter2++;
}
counter1++;
System.out.println("");
counter2 = 0;
}
counter1 = 0;
while (counter1 < sidelenght)
{
System.out.print("*");
counter1++;
}
System.out.println("");
}
}