【发布时间】:2015-04-18 01:14:17
【问题描述】:
我一直在努力使用我为作业创建的这种方法,它让我发疯。我的 if/else 语句有问题,但我似乎无法找出它是什么。这是我收到的输出:
1: Lather and Rise.
2: Lather and rinse.
Done
2: Lather and Rise.
2: Lather and rinse.
Done
3: Lather and Rise.
2: Lather and rinse.
Done
我希望看到这个:
1: Lather and rinse.
2: Lather and rinse.
Done.
我做错了什么来阻止它正确输出?任何建议/帮助表示赞赏!请参阅下面的代码:
public class ShampooMethod {
public static void printShampooInstructions(int numCycles) {
for (int i = 1; i < 4; i++) {
System.out.println(i + ": Lather and Rise.");
if (numCycles < 1) {
System.out.println("Too few.");
} else if (numCycles >= 4) {
System.out.println("Too many");
} else {
System.out.println(numCycles + ": Lather and rinse.");
}
System.out.println("Done");
}
}
public static void main(String[] args) {
printShampooInstructions(2);
return;
}
}
【问题讨论】:
-
请缩进您的代码以便阅读。
-
会不会是循环 3 次的 for 循环包裹了你的所有代码?
-
此行最先打印
System.out.println(i + ": Lather and Rise.");,所以Rise 总是第一个被打印 -
循环 3 次怎么能显示一次呢?
-
在 for 循环中放 i