制作分析给定值的方法

时间:2015-09-08 09:54:07

标签: java

我想制作一个分析给定值的方法

void method(int value)
  • 如果此值等于示例7,我希望输出如下: 7 = 12 * 0 + 7
  • 如果值= 13输出:13 = 12 * 1 + 1
  • 如果值= 24输出:24 = 12 * 2 + 0
  • 如果值= 39输出:39 = 12 * 3 + 3
  • 如果值= 289输出:289 = 12 * 24 + 1

等等 12是常数

我该怎么做?

3 个答案:

答案 0 :(得分:4)

看起来你正在寻找

12 * x + y

这意味着:

x = value / 12;
y = value % 12;

答案 1 :(得分:0)

void method(int value) {
  int x = value / 12;
  int y = value % 12;
  System.out.print(value + " = 12 * " + x + " + " + y);
}

您正在寻找的方法可能类似于我上面写的代码段。

答案 2 :(得分:0)

以下是完整的代码:

public class SingleItemView {

    public static void main(String[] args) {
        SingleItemView.getNumber(14); // Pass the value for which you need the expression
    }

    public static void getNumber(int n) {
        int temp1 = 0;
        int temp2 = 0;
        if (n > 12) {
            temp1 = n / 12;
            temp2 = n % 12;
            System.out.println(n + " = 12 * " + temp1 + " + " + temp2);
        } else {
            System.out.println(n + " = 12 * 0 + " + n);
        }
    }
}

在main方法中,您将值传递给需要表达的方法。