-1

我有一些需要帮助的代码。我是一名 AP CS 学生(而且是入门),所以请不要评判我。

// name:  
    //
    // program:  CoinsTester
    //
    // purpose: 

    public class CoinsTester {



         public static void main(String[] args) {
     //create a new Coins object named money and pass it the amount of cents as a parameter
  //***** STUDENTS NEED TO COMPLETE ******
      Coins(); 
      Coins money = new Coins(); 

        // call the correct method in the Coins class to find your answer
  //***** STUDENTS NEED TO COMPLETE ******
        money.calculate(); 


    }

}



// name:  
//
// program:  CoinsTester
//
// purpose: This class accepts a certain number of monetary change.
//         The output is a list of the number of quarters, dimes, nickels,
//    and pennies that will make that amount of change with the least
//    number of coins possible.  This is a skeleton that will be finished 
//       by the students

    public class Coins {

 //private variable to store the only attribute of a "Coins" object - how many cents the 
 //    user wants to find change for. 
 private int myChange;

 //constructor that accepts an initial value for the private data member
 public Coins(int change) {
  myChange = change;
 } 

     // the method calculate will 
     // 1. use modular and regular division to determine the quantity of each type of coin
     // 2. prints the quantity of the coins needed to make the entered amount 
      public void calculate(){
      int quarters=25, dimes=10, nickels=5, pennies=1;
      int temp1, temp2, temp3, temp4; 
      int remainquar1, remaindime2, remainnick3, remainpenn4; 

        //variable declarations to hold the values needed for different coin types
        // make sure you use descriptive identifiers!
       //***** STUDENTS NEED TO COMPLETE ******

       // calculations for the various coin types
       //***** STUDENTS NEED TO COMPLETE ******


      // output statements, formatted as shown on specs  
       //***** STUDENTS NEED TO COMPLETE ******

      }   

     }

所以事情就是这样,我为我格式不正确的代码道歉。所以当我运行它时,它说 Coins money = new Coins() 找不到代码的构造函数。我需要帮助来创建合适的对象。这里的事情是我必须为“CoinsTester”创建一个对象,然后它告诉我我没有链接到该对象的构造函数。我现在真的找不到解决方案。有人能给我关于如何为 CoinsTester 类创建构造函数的提示吗?

4

2 回答 2

0

您有两行需要审查:

Coins(); 

这将调用名为 Coins 的方法,而不是构造函数。我建议您删除它,因为您很可能不会使用/不需要它。

Coins money = new Coins(); 

没有与此相关联的构造函数的原因是您已经有 Coins 的构造函数:

public Coins(int change) {
    myChange = change;
} 

创建此构造函数时,您删除了默认构造函数new Coins(); . 如果您希望能够使用不带参数的 Coins 构造函数,您可以再次声明它。这是一个例子:

public Coins() {
    myChange = 0;
} 
于 2013-09-04T02:18:18.530 回答
0

添加一个名为 CoinsTester 的“方法”(如果您需要 CoinsTester 类的构造函数,从 Q 中不清楚您需要什么构造函数)。如果要使用添加的显式构造函数,请确保参数对应于调用序列。

于 2013-09-04T02:00:53.493 回答