首页 文章

无法弄清楚C中的“通过引用传递”的Java等价来解决这个问题,而且[重复]要解决的最佳方法

提问于
浏览
-1

这个问题在这里已有答案:

我在C学习CS-1课程,现在我在新大学,他们在这里使用Java . 我一直在学习新的语法和做旧的C实验室,而且我已经用一个传递参考的方式打了一堵墙 .

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

编写一个程序,告诉您从1美分到497美分的任何变化金额 . 由于我们不会使用便士,所以我们需要将任何美分四舍五入到接近5或10 . 例如,如果金额为368美分,则舍入数为370美分,如果金额为367美分,则舍入数为365美分 . 变化将是1 toonie(2美元硬币),1加元,2个季度,2美分硬币370美分 . 使用2美元(toonie),1美元(加元),25美分(季度),10美分(硬币)和5美分(镍币)的硬币面额 . 您的程序将使用以下函数:Void computeCoin(int coinValue,int&number,int&amountLeft);
请注意,此函数需要返回两个值,因此您必须使用引用变量 . 例如,假设变量amountLeft的值为370美分 . 然后,在接下来的调用之后,number的值将为1,amountLeft的值将为170(因为如果从370美分获得1 toonie,则剩下170美分):computeCoin(200,number,amountLeft);
在进行以下调用之前,使用硬币名称打印数字的值:computeCoin(100,number,amountLeft);
等等 . 包含一个循环,允许用户为新输入值重复此计算,直到用户输入sentinel值来停止程序 .

在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;
}

1 回答

  • 1

    您可以(并且可能应该)返回一个对象,因为Joop Eggen在评论中指出了这一结果 .

    有时可行的替代方法(技术上它总是有效,但不应该总是使用)是传入一个像这样的可变对象:

    class MutableInteger
    {
       private int value;
    
       public void setValue(final int val)
       {
          value = val;
       }
    
       public int getValue()
       {
          return value;
       }
    }
    

    然后将一个实例传递给您的方法而不是int参数 .

    基本上就像C中的结构一样 . 但是,我建议在这个具体情况下返回结果 .

相关问题