**如果参数乘以3打印“JU”
如果参数乘以5打印“MP”
如果参数乘以3和5打印“JUMP”**
ex:
> java myclass 16
输出:
1
2
JU
4
MP
JU
7
8
JU
MP
11
JU
13
14
JUMP
16
答案 0 :(得分:3)
// for each number in the sequence 1..N execute the following
for (int i = 1; i <= N; ++i) {
// print JU if divides by 3
if (i % 3 == 0) {
System.out.print("JU");
}
// print MP if divides by 5
if (i % 5 == 0) {
System.out.print("MP");
}
// note that if it divides by 3 and by 5 (by 15 actually)
// it print JU and MP in a row and it becomes JUMP
// if not dividable - print it itself
if (i % 3 != 0 && i % 5 != 0) {
System.out.print(i);
}
System.out.println();
}
答案 1 :(得分:2)
这是FizzBuzz的一个变体,是新程序员或采访中的常见挑战。为什么不查看Rosetta Code's Java section on FizzBuzz以查看各种方法。你显然需要为'JU'改变Fizz,为'MP'改变Buzz。
答案 2 :(得分:0)
试试这个
public static void getNumber(int upperLimit){
boolean status=true;
int i=1;
while (status) {
if(i%3==0){
System.out.println("JU");
}else if(i%5==0){
System.out.println("MP");
}else if(i%5==0&&i%3==0){
System.out.println("JUMP");
}else {
System.out.println(i);
}
if(i==upperLimit){
status=false;
}
i++;
}
}