简体   繁体   English

硬币:无效的方法声明,需要返回类型

[英]Coins : Invalid Method Declaration, return type required

I just started Computer Science last week, and we got a worksheet called Coins, in which I had to find out how many quarters, dimes, nickels and pennies there are in a set of coins. 我上周刚开始计算机科学,我们得到了一个名为Coins的工作表,在其中我必须找出一组硬币中有多少个四分之一,一角硬币,五分之一硬币和几分钱。 I am having a lot of trouble, and getting that error. 我遇到了很多麻烦,并且遇到了这个错误。 Here's my code 这是我的代码

package Coins;


public class Coins
{
    private int change;

    // two contructors
    Change()    //default constructor
    {
        change = 94;
    }

    Change( int c )
    {
        change = c;
    }

    // accessor method - change
    public int getChange()
    {
            return Change;
    }



    // mutator method - change
    public void setChange( int anotherChange)
    {
        change = anotherChange;
    }

    public void askUserForChange()
    {
        Scanner keyIn;
        keyIn = new Scanner(System.in);

        System.out.print("Please enter the amount of change: ");
        String input = keyIn.nextLine();

        int nChange = Integer.parseInt (input);

        setChange(nChange);
        // change = nChange

        printChangex();
    }

    // action method - take accessor figure out coins -> output
    // calculating the coins needed for the change
    public void printChangeRange(int start, int end)
    {
        for(int c = start; c <= end; c++
        {
            setChange(c);
            printChangex();
        }

    }
    public void printChangex()
    {

    int c = change;
    int quarter = c / 25;
    System.out.println("quarter = " + quarter);
    int a = c%25;
    int dime = a / 10;
    System.out.println("dime = " + dime);
    int b = a%10;
    int nickel = b / 5;
    System.out.println("nickel = " + nickel);
    int c = b%5;
    int penny = c / 1;
    System.out.println("penny = " + penny);

    }


    // instance variables - replace the example below with your own
    private int x;

    public Coins()
    {
        // initialise instance variables
        x = 0;
    }

    public int sampleMethod(int y)
    {
        // put your code here
        return x + y;
    }
}

You have a class named Coins and are trying to give it a constructor named Change . 您有一个名为Coins的类,并且正在尝试为其提供一个名为Change的构造函数。 The class and constructor must have the same name. 类和构造函数必须具有相同的名称。 Just pick one. 选一个。

To elaborate on the error in your title, I assume that "Invalid Method Declaration, return type required" refers to the line with Change() //default constructor . 为了详细说明标题中的错误,我假设“无效的方法声明,需要返回类型”是指带有Change() //default constructor Since this is in a class called Coins it is not a constructor as the comment claims. 由于这是在名为Coins的类中,因此它不是注释所声称的构造函数。 The Java compiler thinks that it is a method. Java编译器认为这是一种方法。 All methods must have a return type, so the compiler complains. 所有方法都必须具有返回类型,因此编译器会抱怨。

The actual constructors are at the bottom of your code. 实际的构造函数位于代码的底部。 It is standard practice to put constructors first, so I suggest that you put these poperly-named constructors at the beginning of your Coins class. 按照惯例,将构造函数放在首位,所以我建议您将这些以名字命名的构造函数放在Coins类的开头。 You probably just need to remove the Change() constructors completely. 您可能只需要完全删除Change()构造函数。

Also as a tip for asking questions here, it is extremel critical that you post the complete error message you are getting. 另外,作为在此处提问的提示,发布所收到的完整错误消息也至关重要。 My answer is based on some educated guesses and certainly don't solve all the problems in your code. 我的答案是基于一些有根据的猜测,当然不能解决代码中的所有问题。 Feel free to come back with more questions as you keep trying to fix your program. 在继续尝试修复程序时,请随时提出更多问题。

This 这个

// two contructors
Change()    //default constructor
{
    change = 94;
}

Change( int c )
{
    change = c;
}

is unusual. 不寻常。 You even have a constructor for the class Coins at the bottom of the file, so you would want to use that. 您甚至在文件底部都有一个Coins类的构造函数,因此您想使用它。 Keep in mind that all Java classes have a constructor that are named the same as the class itself - even if it's the default constructor. 请记住,所有Java类都具有与该类本身相同的名称的构造函数-即使它是默认构造函数。

It's even more unusual that it has the magical value of 94 on instantiation...but in all seriousness, pick a class name and stick with it. 它在实例化时具有94的神奇价值,这更是不寻常了……但实际上,要选择一个类名并坚持下去。

This 这个

// accessor method - change
    public int getChange()
    {
            return Change;
    }

...is also odd. ...也很奇怪 You may want to return the member variable change instead, so change that to a lower case C. 您可能想返回成员变量change ,因此将其更改为小写C。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM