給定一個像「Code」這樣的非空字符串str,打印一個像「CCoCodCode」這樣的字符串。在字符串中的每個索引處,必須將字符串重新打印到該索引。如何重新打印從第一個字符開始到最後一個字符串?
我知道我寫這段代碼肯定有些問題,因爲答案應該是CCoCodCode,但是它會給我字母表!我不知道該如何改變它。
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
String str = scan.next();
int x = str.length();
for(char i = str.charAt(0); i <= str.charAt(x-1); i++)
{
System.out.print(i);
}
}
非常感謝你!你知道我可以怎樣更多地練習這些東西嗎?事實上它很簡單,我一直在複雜的問題。 – Kj45