給定的字符串是String s1=new String()
設置字符串值使用for循環我想改變S1值 如果循環運行一次,那麼它應該是s1="?"
, 如果循環運行兩次,那麼它應該如果循環是s1="?,?"
, 運行三次,那麼它應該是s1="?,?,?"
等等。 怎麼可能?使用循環
Q
使用循環
-3
A
回答
-1
嘗試,
String s1 = "";
for(int i=0;i<3;i++){
s1+=",?";
}
s1 = s1.replaceFirst(",", "");
System.out.println(s1);
輸出
?,?,?
+0
爲什麼選擇Down ..? ?? –
+1
It works.Thanks for your answer ... – user3253905
1
嘗試使用StringBuilder
:
public static void main(String[] args)
{
int n = 10; // Number of iterations
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) {
if (i != n - 1) { // if it's not the last iteration
sb.append("?,");
} else {
sb.append("?");
}
}
System.out.println(sb.toString());
}
輸出:
?,?,?,?,?,?,?,?,?,?
注:
如果你想把它當作一個String
,只是在結尾處加上:
String s1 = sb.toString();
相關問題
- 1. 循環使用javascript循環使用javascript
- 2. $。每()循環+用的getJSON()使用循環
- 3. 使用循環
- 4. 使用循環
- 5. 使用循環
- 6. 使用循環
- 7. 使用循環
- 8. 使用循環
- 9. 使用循環
- 10. 使用循環
- 11. 使用循環
- 12. 使用循環
- 13. 使用循環
- 14. 使用循環
- 15. 使用循環
- 16. 使用循環
- 17. 使用循環
- 18. 使用循環
- 19. 使用循環
- 20. 使用循環
- 21. 使用循環
- 22. 使用循環
- 23. 使用數組for循環for循環
- 24. 使用循環編寫bash循環
- 25. 安全使用循環變量循環
- 26. 在while循環中使用for循環
- 27. 使用DateOffSet使用循環
- 28. 要使用循環
- 29. 使用jQuery.each循環
- 30. 使用while循環
你爲什麼不試試呢? –
我不明白你在說什麼? – user3253905
我正在談論你向我們展示了你所嘗試過的。我們並不真的在SO上發佈代碼。 –