java输出菱形为什么没有对齐???

package Test;

import java.util.Scanner;

public class TestLingXing02 {

 public static void main(String[] args) {
  // TODO 自动生成的方法存根
  
  Scanner in=new  Scanner(System.in);
  int i,a,k,x;
  double s; 
  
  System.out.println("请输入一个整数:");
  a = in.nextInt();

  for( i = 1; i <= a; i ++ )  
  {
   if ( i <= ( a+1 ) / 2 )
   {
    x = 2* i-1;
   }
   else
   {
    x = 2 * (a-i) + 1;
   }
   
   s =50-x/2;
   
   for ( k = 1; k < s; k ++ )
   {  
     System.out.print(" ");  
   }
   
   for ( k = 1; k <= x; k ++ )
   {
    System.out.print(" *");
   }
   System.out.println();
   
  }


 }

}


                                                  *
                                                 * * *
                                                * * * * *
                                               * * * * * * *
                                               * * * * * * *
                                                * * * * *
                                                 * * *
                                                  *


请大神解答!


qq_企鹅_13
浏览 1400回答 1
1回答

天将明96

for (k = 1; k <= x; k++) {     System.out.print("*"); //你的代码这里'*'前面多了个空格。。。 }你改一下就好了,别的没什么问题。有空格的情况。没有的情况。如果满足要求,望采纳!<(▰˘◡˘▰)>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java