samedi 19 juin 2021

Creating Java patterns

A java program that displays a pattern according to the user’s input.

Patterns:

num1= #

num2= \

num3= /

num4= _

This is my desired program:


Input:
3
3
1
2

Output:
###
   \
    \
     \
     /__

The problem is it’s not showing my desired output.When I entered an input, this is what displays:

Input:
3
3
2
1
Output:
###
\
 \

   /__

Can you help me fix this? This is my code:

import java.until.Scanner;
class Main{
public static void main(String []args){

Scanner input= new Scanner(System.in);
int i;
int num1=input.nextInt();
int num2=input.nextInt();
int num3=input.nextInt();
int num4=input.nextInt();

for(i=0; i<num1; i++) {   
System.out.print("# ");   
}   
//num2
 for(int b=1; b<=num2; b++){
            for (int j=2; j<=num2; j++){
                if(b==j || j==b){
                    System.out.print("\\");
                }else{
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
        for(i = 0; i < num3; i++){
        System.out.println();
        
        for(int j = 0; j < num3; j++) {
            if(i+j == num3 -1)
                System.out.print(" ".repeat(num1*2 - 2) + "/");
            else if( i != num3-1)
                System.out.print(" ");
        }        
    }
    
    //num4
    for(i=0; i<num4; i++) {   
        System.out.print("_");   
    }
 
}   
}

Aucun commentaire:

Enregistrer un commentaire