public class Firework {
public static void main(String[] args) {
final int time = 120;//总时间
int totalCount = 0;//总次数
int firstCount = 0;//甲的放鞭炮次数
int secondCount = 0;//乙的放鞭炮次数
int thirdCount = 0;//丙的放鞭炮次数
//注意刚开始也可以放,所以时间从0开始,到120结束,切记
for(int i = 0; i <= time; i++){
if(i % 3 == 0){
firstCount++;
}
if(i % 2 == 0){
secondCount++;
}
if(i % 4 == 0){
thirdCount++;
}
}
totalCount = firstCount + secondCount + thirdCount;
System.out.println("2个小时内一共听到鞭炮次数: " + totalCount);
}
}
------------------程序结果
2个小时内一共听到鞭炮次数: 133