简体   繁体   English

制作一个带有单词的空心钻石

[英]Making a hollow diamond with a word in it

What I need is a little modification to my code so that every part of my hollow diamond prints a letter of the word "HURRICANE"我需要的是对我的代码稍作修改,以便我的空心钻石的每个部分都打印出一个字母“飓风”

My code is:我的代码是:

String st1 = "HURRICANE";
int a = 0;
for (int i = 5; i >= 1; i--) {
    for (int j = 1; j <= 9; j++) {
        if (j == i || (10 - i) == j) {
            System.out.print(st1.charAt(a)); //needs change
        } else {
            System.out.print(' ');
        }
    }
    System.out.println();
}
for (int i = 2; i <= 5; i++) {
    for (int j = 1; j <= 9; j++) {
        if (j == i || (10 - i) == j) {
            System.out.print(st1.charAt(a)); //needs change
        } else {
            System.out.print(' ');
        }
    }
    System.out.println();
}

The output comes out as:输出结果如下:

    H    
   H H   
  H   H  
 H     H 
H       H
 H     H 
  H   H  
   H H   
    H    

I need to modify my "charAt" statement a little so it comes out to be:我需要稍微修改一下我的“charAt”语句,结果是:

    H    
   U U   
  R   R  
 R     R 
I       I
 C     C 
  A   A  
   N N   
    E

How should I make my print statement?我应该如何制作我的打印声明?

It's worth noting that the example provided only works for Strings the same length as "HURRICANE".值得注意的是,提供的示例仅适用于与“HURRICANE”长度相同的字符串。 A superior solution would work for all strings.一个卓越的解决方案适用于所有字符串。

Partial solution for you to complete, since I guess it's your coursework and I don't want you to copy / paste / fail exams :P部分解决方案供您完成,因为我猜这是您的课程作业,我不希望您复制/粘贴/考试失败:P

public static void main(String[] args) {
    String st1 = "HURRICANE";
    char[] st1CharArray = st1.toCharArray();
    int maxSpaces = st1CharArray.length / 2 + 1;
    for (int i = 0; i <= st1CharArray.length / 2; i++) {
        if (i == 0) {
            System.out.println(getSpacesString(maxSpaces) + st1CharArray[i]);
        } else {
            System.out.println(getSpacesString(maxSpaces - i)
                    + st1CharArray[i] + getSpacesString(i * 2 - 1)
                    + st1CharArray[i]);
        }
    }
    // Loop from st1CharArray.length / 2 + 1 and get the second half done.
}

private static String getSpacesString(int numberOfSpaces) {
    StringBuilder strBuilder = new StringBuilder();
    for (int i = 0; i < numberOfSpaces; i++) {
        strBuilder.append(" ");
    }
    return strBuilder.toString();
}
String st1 = "HURRICANE"; int a = 0; for (int i = 5; i >= 1; i--) { for (int j = 1; j <= 9; j++) { if (j == i || (10 - i) == j) { System.out.print(st1.charAt(5 - i)); } else { System.out.print(' '); } } System.out.println(); } for (int i = 2; i <= 5; i++) { for (int j = 1; j <= 9; j++) { if (j == i || (10 - i) == j) { System.out.print(st1.charAt(3 + i)); } else { System.out.print(' '); } } System.out.println(); }
//: Playground - noun: a place where people can play

import UIKit

var name : String = "HURRICANE"
var dimensions : Int = name.count - 1
var k : Int = 0

for rows in 0...dimensions{
    for columns in 0...dimensions{
        k = abs( (dimensions/2) - rows )

        if columns == k || columns == dimensions - k{
            print(Array(name)[rows], terminator: "")
        }
        else{
            print(" ", terminator: "" )
        }
    }

    print("")
}

Let's assume that a word has an odd number of characters, otherwise we get a crooked diamond.让我们假设一个单词有奇数个字符,否则我们会得到一个弯曲的菱形。

Try it online! 在线试试吧!

public static void main(String[] args) {
    String str = "abrahadabra";
    int n = str.length() / 2;
    for (int i = -n, ch = 0; i <= n && ch < str.length(); i++, ch++) {
        for (int j = -n; j <= n; j++)
            if (Math.abs(i) + Math.abs(j) == n)
                System.out.print(str.charAt(ch));
            else
                System.out.print(" ");
        System.out.println();
    }
}

Output:输出:

     a     
    b b    
   r   r   
  a     a  
 h       h 
a         a
 d       d 
  a     a  
   b   b   
    r r    
     a     

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM