简体   繁体   English

打印数字1-100,但可以被5或12整除的数字

[英]Printing numbers 1-100, except when divisible by 5 or 12

The question is 问题是

Write a Java program that prints numbers from 1 to 100. If the number is divisible by 5, instead of printing the number, your program should print how many fives are in this number, if the number is divisible by 12 it should print how many twelves are in this number. 编写一个打印从1到100的数字的Java程序。如果该数字可被5整除,而不是打印该数字,则程序应打印该数字中的5数;​​如果该数字可被12整除,则应打印该数。这个数字是十二。

Here's my current attempt: 这是我目前的尝试:

import acm.program.*;

public class FiveTwelve extends ConsoleProgram {
    public void run() {
        int a = (1);
        int b = (5);
        int c = (12);
        for (a = 1; a <= 100; a++) {
            println(a);
            if ((a % 5) == 0) {
                println(a/b);
            } else if ((a % 12) == 0) {
                println(a / c);
            }
        }
    }
}

The problem is that my code is also printing the divisible number instead of just the outcome. 问题是我的代码也在打印可除数而不是结果。 Example output: 输出示例:

1
2
3
4
5
1
6
7
8
9
10
2
11
12
1

This goes on. 这样下去。 I want to remove numbers that are divisible by 5 and 12. Instead, I need to show the result or num/5 && num/12. 我想删除可被5和12整除的数字。相反,我需要显示结果或num / 5 && num / 12。

UPDATE!!! 更新!!!

import acm.program.*;
public class FiveTwelve Extends ConsoleProgram{
public void run() {
    for (int a = 1; a <= 100; a++) {
        if (a % 5 == 0 && a % 12 == 0) {
            println(a / 5);
            println(a / 12);
        } else if (a % 5 == 0) {
            println(a / 5);
        } else if (a % 12 == 0) {
            println(a / 12);

        } else {
            println(a);
        }
    }
}
}

guess we all missed something. 猜猜我们都错过了什么。

Instead of solving for the specific case, let's write an extensible solution. 让我们编写一个可扩展的解决方案,而不是解决特定的情况。 First, what variables are there in the question that could change? 首先,问题中哪些变量可能会改变? I'd say they are: 我会说他们是:

  1. The starting number 1 起始编号1
  2. The last number 100 最后号码100
  3. The divisors 5 and 12 除数512

Additionally, as @Anoml pointed out, the problem wording suggests that if your number is divisible by multiple divisors, we should print out the number of times it is divisible for every applicable divisor. 此外,正如@Anoml指出的那样,问题用语表明,如果您的数字可被多个除数整除,则我们应打印出每个适用除数可被整除的次数。 And, as you noted in your question, we only want to print the number itself when it is not divisible by any divisors. 而且,正如您在问题中所指出的那样,我们只希望在数字不能被任何除数整除的情况下打印数字本身。

So, our solution becomes: 因此,我们的解决方案变为:

import acm.program.*;

public class FiveTwelve extends ConsoleProgram {
    private final int FIRST = 1;
    private final int LAST = 100;
    private final int[] DIVISORS = { 5, 12 };

    public void run() {
        boolean hasDivisor;

        for (int i = FIRST; i <= LAST; i++) {
            hasDivisor = false;

            for (int divisor : DIVISORS) {
                if (i % divisor == 0) {
                    println(i / divisor);
                    hasDivisor = true;
                }
            }

            if (!hasDivisor) {
                println(i);
            }
        }
    }
}

This way, we can change our starting number, last number, or which divisors we support, and the program will still pass. 这样,我们可以更改起始编号,最后一个编号或我们支持的除数,程序仍会通过。

This is what you intended to do, println(a) in all other cases. 这就是您要执行的操作,在所有其他情况下,都是println(a)

for (int a = 1; a <= 100; a++) {
    if (a % 5 == 0) {
        println(a / 5);
    } else if (a % 12 == 0) {
        println(a / 12);
    } else {
        println(a);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM