我想制作一个分析给定值的方法
void method(int value)
等等 12是常数
我该怎么做?
答案 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方法中,您将值传递给需要表达的方法。