慢慢地,但是我肯定不知道该如何进一步。
机器必须具备以下功能:
[卖20美分的票。
出售第一张票后,它应该询问用户是否要再购买张票。这由第j / n号决定显示。
如果决定为'j':检查余额,如果还不够,它必须要求用户插入钱,直到余额> = 20美分。
如果决定为'n',则必须将其余款项退还给用户并向他显示总额。
该决定必须使用布尔值。
这是我遇到的问题,我无法编写机器工作的过程。
我不知道了,将为您提供任何帮助。
import java.util.Scanner;
public class TicketMachine
{
Scanner sc = new Scanner(System.in);
// 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;
private int restbetrag;
/**
* Create a machine that issues tickets of the given price.
*/
public TicketMachine(int cost)
{
price = cost;
System.out.println("Bitte Muenze einwerfen: ");
total = 0;
}
/**
* @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()
{
// Simulate the printing of a ticket.
System.out.println("##################");
System.out.println("#Erstsemesterfeier#");
System.out.println("# Ticket #");
System.out.println("# " + price + " cent");
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;
}
public String resetAlreadyPaid () {
restbetrag = balance;
balance = 0;
return(restbetrag + "cent");
}
public void processTicket() {
if(balance >= price) {
printTicket();
resetAlreadyPaid();
}else{
System.out.println("You must insert at least: " +
(price - balance) + " more cents.");
balance = sc.nextInt();
}
}
private boolean MachineWorks;
do{
do{
System.out.println("Bitte Muenze einwerfen: ");
int amount = sc.nextInt();
insertMoney(amount);
price = balance;
} while (balance <= price);
do {
printTicket();
System.out.println("Wollen sie weitere Tickets? j/n ");
char decision = sc.next().charAt(0);
if (decision == 'j') {
MachineWorks = true;
}
if (decision == 'n') {
MachineWorks = false;
System.out.println("Restgeld: " + restbetrag);
System.out.println("Gesamtsumme: " + total);
}
} while (MachineWorks==true && balance >= price);
} while (MachineWorks = false);
}
do {
printTicket();
System.out.println("Wollen sie weitere Tickets? j/n ");
char decision = sc.next().charAt(0);
boolean moreTickets = (decision == 'j')? true : false;
if (moreTickets) {
MachineWorks = true;
}
else{
MachineWorks = false;
System.out.println("Restgeld: " + restbetrag);
System.out.println("Gesamtsumme: " + total);
}
} while (MachineWorks==true && balance >= price);