I want to make this pattern:
!!!!!!!!!!!!!!!!!!!!!!
\\!!!!!!!!!!!!!!!!!!//
\\\\!!!!!!!!!!!!!!////
\\\\\\!!!!!!!!!!//////
\\\\\\\\!!!!!!////////
\\\\\\\\\\!!//////////
How would you go about it?
I first made the left side like this:
for (j = 1; j <= i; j++ ){
System.out.print("\\\\");
}
Then I made the right side like this:
for (j = 1; j <= i; j++ ){
System.out.print("//");
}
Both for-loops are in a for-loop like this:
public static void main(String[] args) {
//input should be customizable, like 4, 6, 8, 43 and so on
int input = 4;
for (int i = 0; i < input; i++){
//left side slashes
for (int j = 1; j <= i; j++ ){
System.out.print("\\\\");
}
//Here is the problem. It almost works like supposed to, just a on Line 2 appears 1 "!!" to much, then on line 1 there is 2 "!!" too much.
for (int k = input - i; k > 0; k--){
for(int l = 0; l < k; l++){
System.out.print("!!");
}
}
//right side slashes
for (int j = 1; j <= i; j++ ){
System.out.print("//");
}
//Line breaks
System.out.println();
}
}
You may use the repeat method of class String to simplify the code. Then it's just a matter of counting how many characters you need.
public class Pyramid {
public static void main(String[] args) {
int n = 6;
if (args.length > 0) {
n = Integer.parseInt(args[0]);
}
for (int i = 0; i < n; i++) {
System.out.println("\\\\".repeat(i) + "!!".repeat(2 * (n - i) - 1) + "//".repeat(i));
}
}
}
java Pyramid
!!!!!!!!!!!!!!!!!!!!!!
\\!!!!!!!!!!!!!!!!!!//
\\\\!!!!!!!!!!!!!!////
\\\\\\!!!!!!!!!!//////
\\\\\\\\!!!!!!////////
\\\\\\\\\\!!//////////
Of course, you can still replace the calls to repeat
with for
loops, and you should see the problem now: you don't need two nested loops for the middle !!
characters.
public class Pyramid {
public static void main(String[] args) {
int n = 6;
if (args.length > 0) {
n = Integer.parseInt(args[0]);
}
for (int i = 0; i < n; i++) {
// Left side slashes
for (int j = 0; j < i; j++) {
System.out.print("\\\\");
}
for (int j = 2 * (n - i) - 1; j > 0; j--) {
System.out.print("!!");
}
// Right side slashes
for (int j = 0; j < i; j++) {
System.out.print("//");
}
// Line break
System.out.println();
}
}
}
I got it right this time. Thanks a lot @Thomas
public class Main {
public static void main(String[] args) {
int input = 4;
for (int i = 0; i < input; i++){
for (int j = 1; j <= i; j++){
System.out.print("\\\\");
}
for (int j = (input*2-1) - (2*i); j > 0; j--){
System.out.print("!!");
}
for (int j = 1; j <= i; j++){
System.out.print("//");
}
System.out.println();
}
}
}
Try this.
public static void main(String[] args) throws Exception{
int n = 6;
for (int i = 0, excl = n * 4 - 2, slash = 0; i < n; ++i, excl -= 4, slash += 2)
System.out.println("\\".repeat(slash) + "!".repeat(excl) + "/".repeat(slash));
}
output:
!!!!!!!!!!!!!!!!!!!!!!
\\!!!!!!!!!!!!!!!!!!//
\\\\!!!!!!!!!!!!!!////
\\\\\\!!!!!!!!!!//////
\\\\\\\\!!!!!!////////
\\\\\\\\\\!!//////////
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.