有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用简单的代码在Java或Python上创建一个Hour Glass模式?

所以我想知道,是否有任何简单的代码可以使用Java或Python使用奇数或偶数输入制作一个小时玻璃图案?因为我的代码并不简单(我使用的是Python)

以下是输出示例:

Expected Output

然后,这是我的代码:

def evenGlassHour(target):
 jsp=1
 jtop=target
 jbot=2
 jbotspace=int(target/2)
 eventarget=int(target/2)
 temp=""
 for i in range(eventarget):
     for j in range(i):
         temp+=" "
     for jsp in range(jtop):
         temp+="@"
     jtop-=2
     temp+="\n"
 for i in range(eventarget-1):
     for j in range(jbotspace-2):
         temp+=" "
     for j in range(jbot+2):
         temp+="@"
     jbot+=2
     jbotspace-=1
     temp+="\n"

 print(temp)

def oddGlassHour(target):
 jsp=1
 jtop=target
 jbot=1
 jbotspace=int(target/2)
 oddtarget=int(target/2)
 temp=""
 for i in range(oddtarget):
     for j in range(i):
         temp+=" "
     for jsp in range(jtop):
         temp+="@"
     jtop-=2
     temp+="\n"
 for i in range(oddtarget+1):
     for j in range(jbotspace):
         temp+=" "
     for j in range(jbot):
         temp+="@"
     jbot+=2
     jbotspace-=1
     temp+="\n"

 print(temp)

target=int(input("Input : "))

if(target%2==0):
 evenGlassHour(target)
else:
 oddGlassHour(target)

这是我代码的结果:

 Input : 6
 @@@@@@
  @@@@
   @@
  @@@@
 @@@@@@

 Input : 7
 @@@@@@@
  @@@@@
   @@@
    @
   @@@
  @@@@@
 @@@@@@@

共 (4) 个答案

  1. # 1 楼答案

    在java中,您可以编写如下内容:

    public static void printPattern(int size) {
        int n = size; 
        boolean upper = true;
        for(int i = 0; size%2 == 0? i< size-1 : i<size; i++){            
            String str = String.join("", Collections.nCopies(n, "@"));
            String pad = String.join("", Collections.nCopies((size-n)/2 , " "));
            System.out.println(pad+str+pad);
            if(n-2>0 && upper){
                n-=2;
            }
            else {
                n+=2;
                upper = false;
            }           
        }
    }
    
  2. # 2 楼答案

    在Python中,您可以利用这样一个事实,即可以将字符串乘以x,并将字符串与自身连接x次,如:

    "test" * 3 # becomes testtesttest
    

    此外,您还可以对沙漏的顶部和底部使用相同的函数,方法是对range使用不同的值:

    def evenGlassHour(target, direction = 1):
        for i in range(target, 1, -2) if direction == 1 else range(4, target+1, 2):
            pad = int((target - i) / 2)
            print((" " * pad) + "@" * i + " " * pad)
        if direction == 1:
            evenGlassHour(target, -1)
    
    def oddGlassHour(target, direction = 1):
        for i in range(target, 1, -2) if direction == 1 else range(1, target+1, 2):
            pad = int((target - i) / 2)
            print((" " * pad) + "@" * i + " " * pad)
        if direction == 1:
            oddGlassHour(target, -1)
    
    target=int(input("Input : "))
    
    if target % 2 == 0:
        evenGlassHour(target)
    else:
        oddGlassHour(target)
    

    编辑:您甚至可以删除递归调用,只需将两个范围^{}放在一起,使函数更小:

    from itertools import chain
    
    def evenGlassHour(target):
        for i in chain(range(target, 1, -2), range(4, target+1, 2)):
            pad = int((target - i) / 2)
            print((" " * pad) + "@" * i + " " * pad)
    

    最后,您可以使函数接受要打印的所需符号(任意长度),如下所示:

    def evenGlassHour(target, symbol = "@"):
        for i in chain(range(target, 1, -2), range(4, target+1, 2)):
            pad = int((target - i) / 2) * len(symbol)
            print((" " * pad) + symbol * i + " " * pad)
    

    您还可以将这两个功能结合起来,使其更加荒谬:

    from itertools import chain
    def glassHour(t, s = "@"):
        for i in chain(range(t, 1, -2), range((4 if t % 2 == 0 else 1), t+1, 2)): 
            print((" " * (int((t - i) / 2)*len(s)) + s * i + " " * (int((t - i) / 2)*len(s))))
    
    target=int(input("Input : "))
    glassHour(target, "$$")
    
  3. # 3 楼答案

    可以将字符串格式与str.zfill和递归一起使用:

    def _glass(_input, _original, flag=True):
      if _input in {1, 2}:
        return ('00' if _input == 2 else '0').center(_original) if flag else ''
      if flag:
        return ('0'*(_input)).center(_original)+'\n'+_glass(_input-2, _original, flag=flag)
      return _glass(_input-2, _original, flag=flag)+'\n'+('0'*(_input)).center(_original)
    
    def print_glasses(_input):
      print(_glass(_input, _input)+_glass(_input, _input, False))
    

    for i in range(3, 8):
      print_glasses(i)
      print('-'*20)
    

    输出:

    000
     0 
    000
    --------------------
    0000
     00 
    0000
    --------------------
    00000
     000 
      0  
     000 
    00000
    --------------------
    000000
     0000 
      00  
     0000 
    000000
    --------------------
    0000000
     00000 
      000  
       0   
      000  
     00000 
    0000000
    --------------------
    
  4. # 4 楼答案

    使用中心调整字符串格式

    灵感:https://stackoverflow.com/a/44781576

    def render(size):
        char = "*"
        #build a center-justified format mask
        mask = '{:^%ds}' % (size)
    
        print("size:%s:\n" % (size))
    
        #count down your shrinking
        for i in range(size, 0, -2):
            print(mask.format(char * i))
    
        #trickier:  you've already printed the narrowest
        #and your next line is different depending on odd/even input 
        if size % 2:
            start = 3
        else:
            start = 4
    
        for i in range(start, size+1, 2):
            print(mask.format(char * i))
        print()
    
    
    render(3)
    render(5)
    render(12)
    

    输出:

    size:3:
    
    ***
     *
    ***
    
    size:5:
    
    *****
     ***
      *
     ***
    *****
    
    size:12:
    
    ************
     **********
      ********
       ******
        ****
         **
        ****
       ******
      ********
     **********
    ************