简体   繁体   English

在Java中,如何使变量随每个方法调用而变化?

[英]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.

相关问题 如何更改方法的多个传入变量,然后在Java中返回所说的变量? - How do I change multiple passed in variables of a method, and then RETURN said variables in Java? 在一种方法中,如何更改现有数组的值并将每个数组变量设置为1到100之间的随机数? - In a method, how do I change the values of an existing array and set each of the array variables to a random number between 1 - 100? java - 如何在一个类中的方法内进行循环以调用另一个类中的另一种方法n Java? - How do I make a loop inside a method in one class to call on another method in another class n Java? 如何使用增强的 for 循环更改方法,该方法将方法调用返回到 Java 中的 Stream? - How do i change a method with enhanced for-loop that returns a method call in to a Stream in Java? 如何在带注释的方法的每个方法调用上执行某些操作? - How do I perform something on each method call of an annotated method? 如何在此Java程序中调用该方法? - How do I call the method in this java program? 如何将我创建的以下 Java 代码写入驱动程序并调用方法/类? - How do I make the following Java code I created to a driver and call method/class? 我如何使用Java用一个方法从这些元素的平均值计算出数组中每个元素的方差 - How do I make a method that calculates the variance of each element of an array from the mean of those elements using Java 如何制作和更新Java中的方法? - How do I make and update method in java? 如何使Java客户端使用参数调用soap WebService方法? - How do I make a java client to call a soap WebService method with parameters?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM