【发布时间】:2020-08-19 19:29:00
【问题描述】:
在我的代码中,我能够使用扫描仪功能提示用户输入一段文本,但是,我只能让用户输入一次。要再次输入,我必须关闭终端,然后再次打开它,我怎样才能做到这一点,以便我可以无限输入,这样我就不必每次都关闭它并重新打开它才能再次输入? 对于上下文,这是我的代码,它是关于显示某些数据的售票机,例如人名、价格、总余额等。目前我正在做这些地方。这意味着我希望用户输入英格兰的任何城市,然后会出现一个价格。但正如我所说,用户一次只能输入一个东西,而他们应该可以无限制地输入。
import java.util.Scanner;
import java.lang.String;
public class TicketMachine
{
private int price;
private int balance;
private int total;
/**
* Create a machine that issues tickets of the given price.
*/
public TicketMachine(int cost)
{
price = cost;
balance = 0;
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()
{
if(balance >= price) {
// Simulate the printing of a ticket.
System.out.println("HERE IS YOUR TICKET");
System.out.println("# Ticket");
System.out.println("# " + price + " cents.");
System.out.println("You have" + balance + "left");
// Update the total collected with the price.
total = total + price;
// Reduce the balance by the price.
balance = balance - price;
}
else {
System.out.println("You must insert at least: " +
(price - balance) + " more cents.");
}
}
/**
* Return the money in the balance.
* The balance is cleared.
*/
public int refundBalance()
{
int amountToRefund;
amountToRefund = balance;
balance = 0;
return amountToRefund;
}
public static void main(String [] args)
{
Scanner myScanner = new Scanner(System.in);
String answer = myScanner.nextLine();
if( answer.equals("London") )
{
System.out.println("£15");
}
if( answer.equals("Manchester") )
{
System.out.println("£20");
}
if( answer.equals("Brighton") )
{
System.out.println("£25");
}
if( answer.equals("Cornwall") )
{
System.out.println("£30");
}
if( answer.equals("Crystal Palace") )
{
System.out.println("£35");
}
if( answer.equals("Chealsea") )
{
System.out.println("£40");
}
if( answer.equals("Birmingham") )
{
System.out.println("£45");
}
if( answer.equals("Liverpool") )
{
System.out.println("£50");
}
if( answer.equals("Bristol") )
{
System.out.println("£55");
}
if( answer.equals("Leister") )
{
System.out.println("£60");
}
if( answer.equals("Newcastle") )
{
System.out.println("£65");
}
if( answer.equals("Cambridge") )
{
System.out.println("£70");
}
if( answer.equals("Bradford") )
{
System.out.println("£75");
}
if( answer.equals("Leeds") )
{
System.out.println("£80");
}
if( answer.equals("Oxford") )
{
System.out.println("£85");
}
if( answer.equals("Nottingham") )
{
System.out.println("£90");
}
if( answer.equals("Peterborough") )
{
System.out.println("£95");
}
if( answer.equals("Sheffield") )
{
System.out.println("£100");
}
}
}
【问题讨论】:
-
您的代码无法编译,因此我们无法运行它并重现您的问题,这使得调试变得困难。为了得到正确的答案,将你的代码减少到minimal reproducible example(又名SSCCE),简而言之,minimal(没有不相关的部分)但complete(所以我们不需要写更多)示例,让我们重现您的问题。
-
但除此之外,如果我没记错的话,BlueJ 曾经(或现在)面临着正确处理输入流(由 System.in 表示)的问题,这可能会导致 Scanner 出现问题。也许在你做任何其他事情之前,通过控制台或其他一些 IDE 手动编译和运行它来检查你的真实代码是否运行良好。
-
Pshemo,去掉"for(",代码就可以编译了。
-
使用 edit 选项并在您的示例中更正它。
-
谢谢,还有一件事。我需要这样做,所以在我输入城市并显示价格后,会出现“请立即付款”文本,如果我输入确切的价格,它会显示“谢谢,祝你有美好的一天”,如果我输入比价格小的数字,它告诉我我缺多少钱,如果我输入一个比价格大的数字,它告诉我我有多少零钱。