使用For-Loop帮我编写一个程序,打印用户输入字符串的每三个字符。未显示的字符将打印为下划线。该程序的示例运行可能如下所示:
输入字符串:君士坦丁堡
C _ _ s _ _ n _ _ n _ _ l _
mycode的:
public class Ex02ForLoop {
public static void main(String[] args) {
//name of the scanner
Scanner scanner = new Scanner(System.in);
//initialize variable
String userInput = "";
//asking to enter a string
System.out.print("Enter a string: ");
//read and store user input
userInput = scanner.next();
//using For-Loop displaying in console every third character
for (int i = 0; i <= userInput.length(); i+=3)
{
System.out.print(userInput.charAt(i) + " _ _ ");
}
scanner.close();
}}
但我的输出是: C _ _ s _ _ n _ _ n _ _ l _ _ 需要做一些事情才能把正确的下划线数量 谢谢
答案 0 :(得分:6)
使用此:
foreach($rows as $row) {
echo $row['shortlink'];
}
答案 1 :(得分:-1)
尝试替换它:
for (int i = 0; i <= userInput.length(); i+=3)
使用:
for (int i = 0; i < userInput.length(); i++)
{
if(i % 3 == 0)
System.out.print(userInput.charAt(i));
else
System.out.print("_");
}