请JAVA编写一个程序,while循环结构实现以下要求: 某个游乐园一张门票的售价为100元。请编写一个程序,模拟卖票过程。每次询问顾客是否购买门票,如果顾客确认购买,则计算顾客总消费,并将售出数量加1,否则结束程序。
时间: 2024-02-23 13:59:31 浏览: 71
好的,以下是Java代码实现:
```
import java.util.Scanner;
public class TicketSales {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int ticketPrice = 100; //门票售价
int ticketCount = 0; //售出数量
int totalRevenue = 0; //总收入
while (true) {
System.out.print("是否购买门票?(Y/N)");
String answer = input.next();
if (answer.equalsIgnoreCase("Y")) {
ticketCount++;
totalRevenue += ticketPrice;
System.out.println("已售出" + ticketCount + "张门票,总收入为" + totalRevenue + "元。");
} else if (answer.equalsIgnoreCase("N")) {
break;
} else {
System.out.println("输入有误,请重新输入!");
}
}
System.out.println("程序结束!");
}
}
```
程序中使用了一个 `while` 循环来模拟卖票过程。每次循环,程序会询问顾客是否购买门票,如果顾客确认购买,则售出数量加1,总收入加上门票售价,并输出已售出的门票数量和总收入;如果顾客不购买,则程序结束。当顾客输入了错误的选项时,程序会输出提示信息并要求重新输入。循环会一直执行,直到顾客选择不购买门票时结束程序。
阅读全文