我被提示编写一个程序,每隔一个字母删除一次,例如 schooled = shoe

I have been prompted to write a program that removes every second letter, for example schooled = shoe

我被提示编写一个程序,每隔一个字母删除一个我遇到的一个问题是我需要使用模数运算符和 charAt 方法。我可以想出不同的方法来做到这一点,但我不确定在这种情况下如何使用模数运算符来做到这一点。谁能赐教一下?

您可以按照以下方式进行:

public class Main {
    public static void main(String[] args){
        String str="Schooled";
        StringBuffer sb=new StringBuffer();
        for(int i=0;i<str.length();i++)
            if(i%2==0)
                sb.append(str.charAt(i));
        System.out.println(sb);
    }
}