开发者

Mutator returning a value

Mutator returning a value

Add a new method emptyMachine() to the TicketMachine class that is designed to simulate emptying the machine of money It should both return the value in total and reset the value of total to zero. Paste the whole method into the space below

public int emptyMachine()
    {
        System.out.println("# " + total );
        total = 0;

    }

i get this error:

TicketMachine.java:44: missing return statement
    }
    ^
1 error
The output should have been:
    emptyMachine() returns correct value
    emptyMachine() empties machine

This is what was actua开发者_JAVA技巧lly produced:
    Exception in thread "main" java.lang.NoClassDefFoundError: TicketMachine


Instructions: ""It should both return the value in total and reset the value of total to zero.""

That's easy. In this case the "and" can only be usefully read as "as well as".

public int emptyMachine()
{
    int prevTotal = total;
    total = 0;
    return prevTotal;
}

Win \o/ !


As the compiler says, you are missing a return statement in your method - which must return an int value

How to use the Java return statement


Your method signature declares that it returns an int, but you don't return anything at all.

public int emptyMachine()
{
    System.out.println("# " + total );
    total = 0;
    return total;
}

I have to assume that the variable 'total' is defined outside of the method because you do not show us that code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜