Enter edge length of your rhomboid: 5
Here is your rhomboid:
*****
*****
*****
*****
*****
I need to print that rhomboid with scanner. I get like: * * * * * *
My code was like that normally I'm not that bad but i couldn't even do the first line:
import java.util.Scanner;
public class rhomboid {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter edge lenght of your rhomboid: ");
int edgelenght = scan.nextInt();
System.out.println("Here is your rhomboid:");
while(edgelenght > 0){
System.out.print(" ");
System.out.print("*");
edgelenght--;
So your code will just print 1D output.. Output:- *****
So, to solve this you need two loops one for row and column.Now a little modification in 2D printing for rhombus is, first now there must be gap of 4 spaces before printing,it can be achieved by using one more variable k as shown below.
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter edge lenght of your rhomboid: ");
int edgelenght = scan.nextInt();
int k = edgelenght - 1;
for (int i = 0; i < edgelenght; i++) {
for (int j = 0; j < k + edgelenght; j++) {
if (j < k) {
System.out.print(" ");
} else {
System.out.print("*");
}
}
k--;
System.out.println();
}
}
What you get is what you wrote in your code.
while(edgelenght > 0){
System.out.print(" ");
System.out.print("*");
edgelenght--;
}
will print edgelenght times a space " " and after that a "*".
What you want is something like this:
for(int line = 0; line < edgeLength; line++){
// in line 0 print 4 spaces (5-1), in line 3 print 1 (5-1-3), in line 4 (the last one) print 0
for(int space = 0; space < edgeLength - line - 1; space++){
System.out.print(" ");
}
for(int asterix = 0; asterix < edgeLength; asterix++){
System.out.print("*");
}
// print a newline
System.out.println("");
}
You need to first loop over the lines.
For every line you need a loop to print spaces. And one to print *.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.