【问题标题】:Why is my output not as expected? Java ACM为什么我的输出不符合预期? Java ACM
【发布时间】:2016-12-08 16:01:25
【问题描述】:

对于以下程序,我期望输出为:

5 * 2^3 = 40

但是输出是:

40 * 2^0 = 40

首先我建立一个类“HandyInt”:

/**
* An integer that provides arithmetic operations for great glory.
*/
public class HandyInt {

/** The integer represented by an instance of this class. */
private int theInt;

/**
 * Constructs a new handy integer representing the given int.
 * 
 * @param theInt
 *            the integer to be represented by this instance.
 */
public HandyInt(int theInt) {
    this.theInt = theInt;
}

/**
 * Constructs a new handy integer representing the integer represented by
 * the given handy integer.
 * 
 * @param handyInt
 *            the handy integer to intialize the new object with.
 */
public HandyInt(HandyInt handyInt) {
    this.theInt = handyInt.theInt;
}

/**
 * Returns the integer represented by this instance.
 * 
 * @return the represented integer.
 */
public int getInt() {
    return theInt;
}

/**
 * Returns a handy integer that represents the sum of this and the other
 * handy integer.
 * 
 * @param other
 *            the handy integer to add.
 * @return sum of the two handy integers.
 */
public HandyInt add(HandyInt other) {
    theInt += other.theInt;
    return this;
}

/**
 * Returns a handy integer that represents the result of subtracting the
 * other integer from this one.
 * 
 * @param other
 *            the handy integer to subtract from this one.
 * @return difference of the two handy integers.
 */
public HandyInt sub(HandyInt other) {
    theInt -= other.theInt;
    return this;
}

@Override
public String toString() {
    return Integer.toString(theInt);
 }
}

当我构建一个公共运行代码时,它建立在我的类“HandyInt”之上:

import acm.program.ConsoleProgram;

public class ComplexMathSolver extends ConsoleProgram {
@Override
public void run() {
    HandyInt factor = new HandyInt(5);
    HandyInt exp = new HandyInt(3);

    HandyInt result = multiplyWithPowerOfTwo(factor, exp);

    println(factor + " * 2^" + exp + " = " + result);
}

/**
 * Returns the result of multiplying {@code factor} with 2 to the power of
 * {@code exp}.
 * 
 * @param factor
 *            the handy integer to multiply with the power.
 * @param exp
 *            the exponent of the power.
 * @return the result of the multiplication.
 */
private HandyInt multiplyWithPowerOfTwo(HandyInt factor, HandyInt exp) {
    HandyInt one = new HandyInt(1);

    while (exp.getInt() > 0) {
        factor = factor.add(factor);
        exp = exp.sub(one);
    }

    return factor;
 }   
}

我的班级“HandyInt”修复输出有什么问题? 谢谢!

【问题讨论】:

  • 你的对象正在变异

标签: java class-hierarchy acm-java-libraries


【解决方案1】:

对象正在变异,因为当您调用multiplyWithPowerOfTwo() 方法时,您更改了对象factorexp 的内部结构。此外,您的 result 指向与 factor 相同的对象。

【讨论】:

  • 谢谢,我在“HandyInt”类中修复了它。在add()sub() 方法中,我创建了一个新的HandyInt:HandyInt sub = new HandyInt(theInt); sub.theInt -= other.theInt; return sub;
猜你喜欢
  • 1970-01-01
  • 2021-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-16
  • 1970-01-01
  • 2022-01-16
  • 2017-02-11
相关资源
最近更新 更多