mercredi 29 décembre 2021

My code doesn't work with methods. (Java)

I wrote a pattern code but can't manage to work it with methods. When I delete all the methods (except main method) it works. I must use 2 extra methods for this program, so I can't delete them. I put down there 2 images of 2different outputs (with methods, without methods). Can you help me out with this. Thx..enter image description hereenter image description here

import java.util.Scanner;

public class asdas3 {

 public static void main(String[] args) {
    
     Scanner input = new Scanner(System.in);
     
     System.out.print("Enter a Letter: ");
     String letter1 = input.next();
     char letter = 0;
     if((letter1.length() == 1)) {
         if ( 65 <= letter1.charAt(0) && letter1.charAt(0) <= 90)
             letter = letter1.charAt(0);  
         else if ( 97 <= letter1.charAt(0) && letter1.charAt(0) <= 122) {
             letter = letter1.charAt(0);                                              
         }
         else 
             System.out.println("Invalid input !");
     }
     else {
         System.out.println("Invalid input !");
     }
     char[][] diamond = new char[0][0];
     constructDiamond (letter);
     printDiamond (diamond);
    
   }
 
 public static char[][] constructDiamond (char letter) {
      
     int a = 0;
     a = letter;
     if(65 <= a && a <= 90) {
     a -=64; }
     else if (97 <= a && a <= 122) {
     a-=96;
     }
      
     char[][] diamond = new char[(2*a)][(2*a)];
     if(a == 1)
         System.out.println("A");
     else {             
         char changingLetter = 65;
         for(int i = 0 ; i < a ; i++) {                         // Row                                 
             diamond[i][a - i] =  changingLetter;
             diamond[i][a + i] =  changingLetter; 
             changingLetter++;
         }
         changingLetter = 65;
         int p = 0;
         for(int j = (2*a)-1 ; j > a ; j--) {   
             diamond[j][a - p] = changingLetter;
             diamond[j][a + p] = changingLetter;
         p++;
         changingLetter++;
         }
     
     }
        
      return diamond;     
      
  }
 
  public static void printDiamond (char[][] diamond) {
      
      for(int n = 0 ; n <= diamond.length-1 ; n++) {
          for (int m = 0 ; m <= diamond[1].length-1 ; m ++) {
              System.out.println(diamond[n][m]);                  
          }
          System.out.print("/n");
      }
      
              
  }
    

}

Aucun commentaire:

Enregistrer un commentaire