java 设置字母在一定范围内的循环使用

标签 java arrays string char

public static void main(String[] args) {
    String name = "Netbeans 8.2";
    String output = "";
    char charData[] = {};

    for (int i = name.length(); i > 0; i--) {
        output = name.charAt(i-1) + output; 
        System.out.println(output);
    }

    for (int i = name.length(); i < 40; i++) {
           output = " " + output;
           System.out.println(output);
    }      

    charData = output.toCharArray(); 
    char last = charData[39]; // von char [39]
    int j=0;
    for (int i = 38; i >= 0; i--) {    //somewhere here must be redone to fix the problem, I know where I get wrong.
        charData[i + 1] = charData[i];  
        for (charData[j] = 0; j < 15; j--) {        
            charData[0] = last; 
            output = new String(charData);
        }
        System.out.println(output);
    }
}

/*
The expected outcome should be like:

2
.2
8.2
 8.2
s 8.2
ns 8.2
ans 8.2
beans 8.2
tbeans 8.2
etbeans 8.2
Netbeans 8.2
 Netbeans 8.2
  ......
   (till length 40)
                            Netbeans 8.2
2                            Netbeans 8.
.2                            Netbeans 8
8.2                             Netbeans 
       .....
etbeans 8.2                            N

*/

最终,一旦移动到右侧并达到数组范围的限制,最后一个字符将再次显示在左侧的开头,这意味着我应该使用 for 来使其余被吃掉的字符继续显示从每次回收开始。这个问题已经让我花了一整天的时间并且无法正确执行。我不熟悉通过“for”方法获取字符串值。

有人有想法吗?万分感激! 感谢您的支持!享受编码的乐趣!

最佳答案

这可能是进行“环回”打印的方法之一。您还没有确切解释为什么要这样做,所以这就是我能为您提供的帮助。我建议阅读代码中的注释,因为它们可能会帮助您理解一些事情。

public static void main(String[] args) {
    // String name = "Netbeans 8.2";
    String output = "Netbeans 8.2";
    char[] charData;

    // this is unnecessary: you're just copying name into output
    // for (int i = name.length(); i > 0; i--) 
    //    output = name.charAt(i - 1) + output; 

    // adding a whitespace character to the beginning of output
    for (int i = output.length(); i < 40; i++) 
        output = " " + output;

    charData = output.toCharArray();

    // how many times do you want it printed? in my case: 100 times
    for (int i = 0; i < 100; i++) { 
        char last = charData[charData.length - 1]; // save the rightmost character

        // move every character one place to the right, losing the rightmost
        for (int j = charData.length - 1; j > 0; j--) 
            charData[j] = charData[j - 1];

        charData[0] = last; // change first character to what we've saved
        // output= new String(charData); also unnecessary - println can print char arrays as well 
        System.out.println(charData);
    }
}

关于java 设置字母在一定范围内的循环使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58893547/

相关文章:

ruby - 使用字符串作为函数名称

字符串排列秩+数据结构

java - 无法从docker容器内的spring连接到redis

c - CodeChef 上 "Discrepancies in voter list"(VOTERS) 的错误答案

java - 无法在 Java AWS lambda 函数中创建与 DynamoDB 的连接

objective-c - FMDB-使用值数组的Select语句

arrays - 从数组中加权随机选择

java - 将 s 识别为元音后最后一个字符的正则表达式模式

java - 使用 Dagger 2 时,我应该将库初始化移动到哪里?

java - 从 XML 数组中读取共享首选项默认值