我的售票机未运行。如何使用布尔和while循环编写运行过程? [关闭]

问题描述 投票:-3回答:1
我需要在明天完成的自动售票机上完成一些帮助。

慢慢地,但是我肯定不知道该如何进一步。

机器必须具备以下功能:

[卖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);

}
java validation boolean do-while ticket-system
1个回答
0
投票
您无法读取布尔值。但是您可以根据给定字符控制布尔值。所以我所做的是:如果给定字符为'j',则布尔值moreTickets为true,如果字符不是'j',moreTickets为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);

© www.soinside.com 2019 - 2024. All rights reserved.