写一份游乐场的Java项目
时间: 2024-05-03 08:18:53 浏览: 11
好的,以下是一份简单的游乐场Java项目:
```java
import java.util.Scanner;
public class AmusementPark {
private static int balance = 0;
private static int tickets = 0;
private static int rides = 0;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int choice;
do {
System.out.println("\nWelcome to the Amusement Park!");
System.out.println("1. Buy tickets");
System.out.println("2. Check balance");
System.out.println("3. Ride a ride");
System.out.println("4. Exit");
System.out.print("Enter your choice: ");
choice = input.nextInt();
switch(choice) {
case 1:
buyTickets();
break;
case 2:
checkBalance();
break;
case 3:
rideRide();
break;
case 4:
System.out.println("\nThank you for visiting the Amusement Park!");
break;
default:
System.out.println("\nInvalid choice! Please try again.");
break;
}
} while(choice != 4);
input.close();
}
public static void buyTickets() {
Scanner input = new Scanner(System.in);
int amount;
System.out.print("\nEnter the amount of tickets you want to buy: ");
amount = input.nextInt();
balance += (amount * 10); // Each ticket costs $10
tickets += amount;
System.out.println("You bought " + amount + " tickets, and your balance is now $" + balance + ".");
input.close();
}
public static void checkBalance() {
System.out.println("\nYour balance is $" + balance + " and you have " + tickets + " tickets.");
}
public static void rideRide() {
Scanner input = new Scanner(System.in);
if(tickets > 0) {
System.out.println("\nYou have " + tickets + " tickets. Which ride would you like to go on?");
System.out.println("1. Ferris wheel");
System.out.println("2. Roller coaster");
System.out.print("Enter your choice: ");
int choice = input.nextInt();
switch(choice) {
case 1:
System.out.println("\nEnjoy your ride on the Ferris wheel!");
break;
case 2:
System.out.println("\nEnjoy your ride on the Roller coaster!");
break;
default:
System.out.println("\nInvalid choice! Please try again.");
break;
}
tickets--;
rides++;
} else {
System.out.println("You don't have enough tickets to ride any rides. Please buy more tickets.");
}
input.close();
}
}
```
这个游乐场项目有以下功能:
1. 购买门票:每张门票10美元,购买门票后,余额会相应增加。
2. 检查余额:显示当前余额和拥有的门票数量。
3. 玩游乐设施:如果有门票,可以选择想要坐的游乐设施,并扣除一张门票。如果没有门票,需要购买更多门票。
4. 退出游戏。
这只是一个简单的示例项目,您可以根据自己的需求进行修改和扩展。