下面的代码应循环生成:
It should print 1: Lather and Rinse
2: Lather and Rinse
Done.
It produces:
1: Lather and Rinse
Done.
import java.util.Scanner;
public class ShampooMethod {
public static void printShampooInstructions(int numCycles) {
if (numCycles < 1) {
System.out.println("Too few.");
return;
}
else if (numCycles > 4) {
System.out.println ("Too many.");
return;
}
else {
for (int i = 1; i <= numCycles; i++) {
System.out.print(+ i);
System.out.println(": Lather and rinse.");
System.out.println("Done.");
return;
}
}
}
public static void main (String [] args) {
printShampooInstructions(2);
return;
}
}
答案 0 :(得分:2)
您必须删除new[] {
new { Id = 1, Tags = new[] { "tag1", "tag2" } },
new { Id = 2, Tags = new[] { "tag3", "tag7" } },
new { Id = 3, Tags = new[] { "tag7", "tag8" } },
new { Id = 4, Tags = new[] { "tag1", "tag4" } },
new { Id = 5, Tags = (string[])null }
}
.SelectMany(u => u.Tags ?? Enumerable.Empty<string>())
.GroupBy(t => t)
.Select(g => new { TagName = g.Key, Count = g.Count() } )
语句,否则您的循环将在第一遍中停止
return;
答案 1 :(得分:1)
你在循环中return
,所以方法第一次退出。删除return
。
此外,声明内联变量并不是一个好习惯。将您的声明放在方法的开头,并在for
语句中初始化它。
答案 2 :(得分:0)
return 语句将立即导致该方法返回,因此循环将在第一次传递时停止。从循环中删除return语句,它应该可以工作。