[英]In Java, how do I make variables change with each method call?
I'm trying to create a program that does manual division for whole numbers and prints out the quotient and remainder. 我正在尝试创建一个程序来对整数进行手动除法,并打印出商和余数。 I know Java can't change primitives so I would have to use a return type, but I'm not entirely sure how to incorporate it. 我知道Java不能更改原语,因此我必须使用返回类型,但是我不确定如何合并它。 Here's what I have so far: 这是我到目前为止的内容:
public class Division
{
private int x;
private int y;
private int quo;
private int a;
public Division() {
x = 50; //these two are just temporary for testing
y = 5;
quo = 0;
}
public void divide() {
a = (x-y);
quo++;
}
public void checkComplete() {
if(a > y) {
this.divide();
}
else {
System.out.println("Quotient: " + quo);
}
}
public static void main (String[] arg) {
Division math = new Division();
math.divide();
math.checkComplete();
}
}
I'm trying to have the variable a change to the output in method divide for future method calls. 我正在尝试让变量对方法划分中的输出进行更改,以供将来进行方法调用。 Any help is appreciated. 任何帮助表示赞赏。
In order to do the division you have to call your divide
method several times until a < y
... 为了进行除法,您必须多次调用divide
,直到a < y
...
Looking at the code, I see you have this: 查看代码,我看到你有这个:
public void checkComplete() {
if(a > y) {
this.divide();
}
else {
System.out.println("Quotient: " + quo);
}
}
Shouldn't you have a while (a>=y) { ... }
somewhere there ? 您是否应该while (a>=y) { ... }
某处有一段while (a>=y) { ... }
? Maybe instead of that if
? 也许if
不是呢?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.