0

我收到一条显示“预期”的错误。我在这部分代码中得到它(* ****)。

为什么我会得到这个?我是初学者,我被困在这个问题上,想知道我该如何解决这个问题。为什么在我有*的地方出现标识符预期错误。这是什么意思广告我该如何解决这个问题?谢谢

/**
* TicketMachine models a ticket machine that issues
* flat-fare tickets.
* The price of a ticket is specified via the constructor.
* Instances will check to ensure that a user only enters
* sensible amounts of money, and will only print a ticket
* if enough money has been input.
*/
public class TicketMachine
{
// The price of a ticket from this machine.
private int price;
// The amount of money entered by a customer so far.
private int balance;
// The total amount of money collected by this machine.
private int total;

/**
* Create a machine that issues tickets of the given price.
*/
public TicketMachine(int cost)
{
if (cost > 0) 
{
price = cost;
balance = 0;
total = 0;
}
else
{ 
System.out.println("can not be minus");
}
}

/**
* @Return The price of a ticket.
*/
public int getPrice()
{
return price;
}

/**
* Return The amount of money already inserted for the
* next ticket.
*/
public int getBalance()
{
return balance;
}

/**
* Receive an amount of money from a customer.
* Check that the amount is sensible.
*/
public void insertMoney(int amount)
{
if(amount > 0) {
balance = balance + amount;
}
else {
System.out.println("Use a positive amount rather than: " +
amount);
}
}

/**
* Print a ticket if enough money has been inserted, and
* reduce the current balance by the ticket price. Print
* an error message if more money is required.
*/
public void printTicket()
{
if(balance >= price) {
// Simulate the printing of a ticket.
System.out.println("##################")…
System.out.println("# The BlueJ Line");
System.out.println("# Ticket");
System.out.println("# " + price + " cents.");
System.out.println("##################")…
System.out.println();

// Update the total collected with the price.
total = total + price;
// Reduce the balance by the prince.
balance = balance - price;
}
else {
System.out.println("You must insert at least: " +
(price - balance) + " more cents.");

}
}

/**
* Exercise printtickettwo
*/
public void printTicketTwo();
int amountLeftToPay;
amountLeftToPay (***********) = price - balance;

if (amountLeftToPay > 0) 
{
// Simulate the printing of a ticket.
System.out.println("##################")…
System.out.println("# The BlueJ Line");
System.out.println("# Ticket ");
System.out.println("# " + price + " cents.");
System.out.println("##################")…
System.out.println();
}
else
{
System.out.println.("Amount Still Required);
} 
}

/**
* Return the money in the balance.
* The balance is cleared.
*/
public int refundBalance()
{
int amountToRefund;
amountToRefund=balance;
balance = 0;
return amountToRefund;
}

/**
* Exercise empty machine money
*/ 
public int emptyMachine()
{
int emptyMachine;
emptyMachine = total;
total = 0;
return total;
}

}
4

2 回答 2

1

这条线public void printTicketTwo();

而是需要

public void printTicketTwo() { *method body* }

编辑 - 一般调试建议;当您收到这样的错误时,通常会有一个行号,如果您有错误或错误,请找到最小的行号并查看那里的代码。通常语法错误在上面提到的一行中,例如,如果你有int myInt = 5,并且int mySecondInt = 7;在下一行,错误可能会引用第二行,即使它是由于在前一行省略分号引起的。

于 2012-10-31T23:34:25.380 回答
0

评论已添加到您犯的每个错误旁边的类中,现在可以正常工作

        public void printTicket()
    {
    if(balance >= price) {
    // Simulate the printing of a ticket.
    System.out.println("##################");  // you for got ;
    System.out.println("# The BlueJ Line");
    System.out.println("# Ticket");
    System.out.println("# " + price + " cents.");
    System.out.println("##################");    // you for got ;
    System.out.println();

    // Update the total collected with the price.
    total = total + price;
    // Reduce the balance by the prince.
    balance = balance - price;
    }
    else {
    System.out.println("You must insert at least: " +
    (price - balance) + " more cents.");

    }
    }




public void printTicketTwo() // ; means an extarnl call of another existing method in the class 
{
int amountLeftToPay;
amountLeftToPay = price - balance;   // any method must have { method body  } 

if (amountLeftToPay > 0) 
{
// Simulate the printing of a ticket.
System.out.println("##################");
System.out.println("# The BlueJ Line");
System.out.println("# Ticket ");
System.out.println("# " + price + " cents.");
System.out.println("##################");
System.out.println();
}
else
{
System.out.println("Amount Still Required");   // you have added an extra . which is not needed and you for got the " and the end of Required
} 
}
于 2013-03-06T20:05:25.350 回答