我的数字菱形无法正常工作

时间:2017-03-09 04:09:58

标签: java loops

我的菱形应该在一个模式中打印数值。它在输入为7时有效,但是当我尝试重新编程它进入循环时,即对于任何给定的数字,它只输出1.请帮助。任何批评都会有所帮助。这是我的代码,适用于7。

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, sizeInPx);
        holder.imageButton.setLayoutParams(params);

这是我的代码,用于何时为任何给定值打印一个。

import java.util.Scanner;
public class NumericRhombus1 {

   public static void main(String [] args){
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter amount: ");
        int total = sc.nextInt();
        int SpacesAmount = total;
        int n = 1;
        int x = 0;
          int num = 1;
          int w = 0;
        for(int i = 1; i <= total; i++){  

             for(int j = SpacesAmount; j != 0; j--) {
                 System.out.print(" ");
                }                
           for(int a = 1; a <= 2 * i - 1; a++){  

                     if(a == 1 || a == (2 * i - 1))
                    System.out.print(n + "");        
                else if(a == 2 || a == (2 * i - 1) - 1)                 
                  System.out.print(n - 1 + "");                  
                else if(a == 3 || a == (2 * i - 1) - 2)
                  System.out.print(n - 2 + "");
                else if(a == 4 || a == (2 * i - 1) - 3)
                  System.out.print(n - 3 + "");
                else if(a == 5 || a == (2 * i - 1) - 4)
                  System.out.print(n - 4 + "");
                else if(a == 6 || a == (2 * i - 1) - 5)
                  System.out.print(n - 5 + "");
                else if(a == 7 || a == (2 * i - 1) - 6)
                  System.out.print(n - 6 + "");
                else{
                    System.out.print("0");
                }

           }                 
           System.out.println();  
           n++;
           w++;
           num++;
           SpacesAmount--;  
        }
        System.out.print(" ");
        int y = n - 1;
                for(int i = total - 1; i >= 1; i--){     

           if(i == 1 || i == 2)
              System.out.print("");
           for(int j = 1; j <= total - i; j++) {
                 System.out.print(" ");
           }      
           for(int j = 1; j <= 2 * i - 1; j++){  
                     if(j == 1 || j == (2 * i - 1))
                    System.out.print((y - 1) + "");                   
                else if(j == 2 || j == (2 * i - 1) - 1)
                   System.out.print((y - 2) + "");
                else if(j == 3 || j == (2 * i - 1) - 2)
                   System.out.print((y - 3) + "");
                else if(j == 4 || j == (2 * i - 1) - 3)
                   System.out.print((y - 4) + "");
                else if(j == 5 || j == (2 * i - 1) - 4)
                   System.out.print((y - 5) + "");
                else if(j == 6 || j == (2 * i - 1) - 5)
                   System.out.print((y - 6) + "");
                else if(j == 7 || j == (2 * i - 1) - 6)
                   System.out.print(y - 7 + "");
                else
                    System.out.print("0");              
           }               
           System.out.println();  
            System.out.print(" ");
            y--;
        }
    }
}

2 个答案:

答案 0 :(得分:1)

return $response->withJson($data);

希望你明白!!

答案 1 :(得分:1)

-nn 的两个嵌套 for 循环和一个 if else 语句。零点位于菱形的中心。

Try it online!

int n = 6;
for (int i = -n; i <= n; i++) {
    for (int j = -n; j <= n; j++)
        if (Math.abs(i) + Math.abs(j) <= n)
            System.out.print(Math.abs(j)+1);
        else
            System.out.print(" ");
    System.out.println();
}

输出:

      1      
     212     
    32123    
   4321234   
  543212345  
 65432123456 
7654321234567
 65432123456 
  543212345  
   4321234   
    32123    
     212     
      1      

另见:
How to print a diamond of random numbers?
Drawing numeric diamond