简体   繁体   English

如何分别使用 for 循环隐藏字符?

[英]How to hide characters using for loops, respectively?

I want to basically hide characters following three constant dots (...), the pattern goes like this:我想基本上隐藏三个常量点(...)之后的字符,模式如下:


Inputs a phrase from the user and outputs the phrase followed by three dots (...), then the phrase minus one character followed by three dots (...), then the phrase minus two characters followed by the dots, and so on until only one dot is left.输入用户的短语并输出短语后跟三个点 (...),然后是短语减去一个字符后跟三个点 (...),然后是短语减去两个字符后跟点,依此类推直到只剩下一个点。

Note: This has to be done using nested for loops only注意:这只能使用嵌套的 for 循环来完成

Sample input样本输入

 1
 disappear

Expected output:预期 output:

 disappear...
 disappea...
 disappe...
 disapp...
 disap...
 disa...
 dis...
 di...
 d...
 ...
 ..
 .

This is my attempt:这是我的尝试:

Problem : I am unable to make it so the phrase decreases each time (minus 1 each time)问题:我无法做到,所以短语每次都减少(每次减 1)

I tried using the charAt();我尝试使用 charAt(); method, but it wouldn't work, I am sure that you would need a for loop separate for each of the dots or a whole set of dots, in this case.方法,但它不起作用,我相信在这种情况下,您需要为每个点或整组点单独设置一个 for 循环。

import java.util.Scanner;
public class Dissappear{
public static void main(String[]args){
    Scanner keyboard = new Scanner(System.in);
    int option = keyboard.nextInt();
    String phrase = keyboard.next();
        if (option == 1){
            for (int x = 0; x <= phrase.length(); x++){
                    System.out.print(phrase + "...");
                for (int y = 0; y <= phrase.length(); y++){
                    char n = phrase.charAt(y);
                    System.out.print(n+"...");
                }
            }
        }
     }
 }

This is how I got it to work:这就是我让它工作的方式:

public class Disappear {
    public static void main(String... args) {
        String word = "disappear";
        int originalLength = word.length();

        for(int i = 0; i < originalLength; i++) {
            System.out.println(word.substring(0, originalLength - i) + "...");
        }

        for(int i = 0; i < 3; i++) {
            for(int j = 0; j < 3 - i; j++) {
                System.out.print(".");
            }
            System.out.println();
        }
    }
}

Without substring:没有 substring:

public class Disappear {
    public static void main(String... args) {
        String word = "disappear";
        int originalLength = word.length();

        for(int i = 0; i < originalLength; i++) {
            for(int j = 0; j < originalLength - i; j++) {
                System.out.print(word.charAt(j));
            }

            System.out.println("...");
        }

        for(int i = 0; i < 3; i++) {
            for(int j = 0; j < 3 - i; j++) {
                System.out.print(".");
            }
            System.out.println();
        }
    }
}

You can do it with StringBuilder:你可以用 StringBuilder 做到这一点:

    StringBuilder stringBuilder = new StringBuilder(str);

    System.out.println(str + "...");
    for (int i = 0; i < length; i++) {
        stringBuilder.deleteCharAt(stringBuilder.length() - 1);
        System.out.println(stringBuilder.toString() + "...");
        if (i == length - 1) {
            for (int j = 0; j < 2; j++) {
                for (int k = j; k < 2; k++) {
                    System.out.print(".");
                }
                System.out.println();
            }
        }

Ok!好的! Nested for loops.嵌套for循环。 But the outer one is only included to meet the requirement.但外层只是为了满足要求才包括在内。 Probably not in the spirit of the assignment though.可能不符合任务的精神。 Just keep decrementing k until it is zero and then latch it there until the StringBuilder length is 0 and the inner loop terminates.只需继续递减k直到它为零,然后将它锁在那里直到StringBuilder长度为0并且内部循环终止。

StringBuilder sb = new StringBuilder("disappear...");
for (;;) {
    for (int k = sb.length() - 4; sb.length() > 0;) {
        System.out.println(sb);
        sb.delete(k, k + 1);
        k = k > 0 ? --k : 0;
    }
    break;
}

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

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