【问题标题】:Can't figure out the Java equivalent of "passing by reference" in C++ to solve this, and the best method to solve by [duplicate]无法弄清楚 C++ 中“通过引用传递”的 Java 等价物来解决这个问题,以及通过 [重复] 解决的最佳方法
【发布时间】:2016-05-28 16:59:45
【问题描述】:

我参加了 C++ 的 CS-1 课程,现在我在一所新大学学习,他们在这里使用 Java。我一直在学习新语法并做我以前的 C++ 实验室,但我遇到了通过引用传递的方法。

到目前为止,我的理解是 Java 是按值传递的,但我怎样才能实现这个问题的目标?

编写一个程序,告诉从 1 美分到 497 美分的任何数量的零钱应该派出什么硬币。由于我们不会使用便士,因此我们需要将任何美分四舍五入到接近 5 或 10 的位置。例如,如果金额为 368 美分,则四舍五入为 370 美分,如果金额为 367 美分,则四舍五入为 365 美分。找零是 1 硬币(2 美元硬币)、1 加元、2 美分硬币、2 美分 370 美分。使用 2 美元(toonie)、1 美元(loonie)、25 美分(25 美分)、10 美分(角钱)和 5 美分(镍)的硬币面额。您的程序将使用以下函数:

Void computeCoin(int coinValue, int &number, int &amountLeft);

注意这个函数需要返回两个值,所以你必须使用引用变量。

例如,假设变量 amountLeft 的值为 370 美分。然后,在下面的调用之后,number 的值为 1,amountLeft 的值为 170(因为如果你从 370 美分中取出 1 个 tonie,则剩下 170 美分):

computeCoin(200, number, amountLeft);

在进行以下调用之前,打印带有硬币名称的数字值:

computeCoin(100, number, amountLeft);

等等。包括一个循环,让用户对新的输入值重复此计算,直到用户输入标记值以停止程序。

在 C++ 中,我可以只使用引用变量,并且可以更改硬币的值。到目前为止,我学到的是对象是一种使用引用的方法,我不知道如何真正做到这一点,或者是否有更好的方法。我在这里包含了 C++ 代码:

#include <iostream>
using namespace std; 

void computeCoin(int coinValue, int & number, int & amountLeft);

int main()
{

   int number, change, amountLeft, remainder;

   do
   {
       cout << "Enter the change (-1 to end): ";
       cin >> change;

  while (change>497)
  {
      cout << "The change should be less than 497. Try again.\n\n";
      cout << "Enter the change (-1 to end): ";
      cin >> change;
  }

  if (change != -1)
  {
     // round to near 5 or 10's
     remainder = change % 5;
     if (remainder <= 2)
        amountLeft = change - remainder; 
     else
        amountLeft = change + 5 - remainder;

     if (amountLeft == 0)
        cout << "No change.";
     else
     {
         cout << amountLeft << " cents can be given as\n";

         // compute the number of toonies
         computeCoin(200, number, amountLeft);
         if (number>0)
            cout << number << " toonie(s)  ";

         // compute the number of loonies
         computeCoin(100, number, amountLeft);
         if (number>0)
            cout << number << " loonie(s)  ";

         // compute the number of quarters
         computeCoin(25, number, amountLeft);
         if (number>0)
            cout << number << " quarter(s)  ";

         // compute the number of dimes
         computeCoin(10, number, amountLeft);
         if (number>0)
            cout << number << " dime(s)  ";

         // compute the number of nickels
         computeCoin(5, number, amountLeft);
         if (number>0)
            cout << number << " nickel(s)  ";

     }
         cout << endl << endl;
      }
   } while (change != -1);



 cout << "\nGoodbye\n";
   return 0;

}

void computeCoin(int coinValue, int &number, int &amountLeft)
{
   number = amountLeft / coinValue;
   amountLeft %= coinValue;
}

【问题讨论】:

  • 返回class Result { int number; int amountLeft; }。 Java 在设计时考虑到了不易出错的问题。 f(x); 永远不会给 x 赋值。
  • 请阅读这个优秀回答您的问题。 Is Java "pass-by-reference" or "pass-by-value"?
  • 你可以做类似 Void computeCoin(int coinValue, Integer number[], Integer amountLeft[]);并使用 number[0] = ... 但这通常不是一个好习惯。
  • 你有一个对状态进行操作和修改的函数。一种方法是让状态为类类型对象的状态,而函数为成员函数。

标签: java c++ pass-by-reference


【解决方案1】:

您可以(并且可能应该)返回一个对象作为 Joop Eggen 在 cmets 中指出的结果。

有时有效的替代方法(从技术上讲它总是有效,但不应该总是使用)是传入一个可变对象,如下所示:

class MutableInteger
{
   private int value;

   public void setValue(final int val)
   {
      value = val;
   }

   public int getValue()
   {
      return value;
   }
}

然后将它的实例而不是 int 参数传递到您的方法中。

基本上这就像 C 中的结构。不过,我还是建议在这种特定情况下返回一个结果。

【讨论】:

    猜你喜欢
    • 2022-07-24
    • 2014-02-09
    • 1970-01-01
    • 1970-01-01
    • 2011-11-01
    • 2020-06-12
    • 1970-01-01
    • 1970-01-01
    • 2011-04-11
    相关资源
    最近更新 更多