写一个普通JAVA程序
时间: 2023-05-25 21:03:32 浏览: 36
这是一个简单的JAVA程序,它打印出“Hello World”:
```
public class Main {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
```
这个程序定义了一个类叫做Main,它有一个公开的静态方法叫做main。Java应用程序的入口点就是这个main方法。在main方法中,我们使用了System.out.println()来打印输出一条简单的消息。当我们运行这个程序时,它会在控制台输出“Hello World”。
相关问题
写一个植物大战僵尸的Java程序
很抱歉,作为AI语言模型,我无法提供完整的Java程序,但我可以为您提供一个参考的程序框架。
```
public class PlantVsZombie {
public static void main(String[] args) {
// 初始化游戏场景
GameScene scene = new GameScene();
// 初始化植物和僵尸
Plant[] plants = {
new Peashooter(),
new Sunflower(),
// ...
};
Zombie[] zombies = {
new NormalZombie(),
new ConeheadZombie(),
// ...
};
// 玩家选择植物
Plant selectedPlant = selectPlant(plants);
// 植物进入场景
scene.addPlant(selectedPlant);
// 开始游戏循环
while (true) {
// 植物与僵尸自动攻击
for (Plant plant : scene.getPlants()) {
for (Zombie zombie : scene.getZombies()) {
plant.attack(zombie);
zombie.attack(plant);
}
}
// 检查场景胜利或失败条件
if (scene.isWin() || scene.isLose()) {
break;
}
// 玩家采取行动
Action action = selectAction(scene, plants);
// 根据玩家行动进行操作
if (action instanceof PlantAction) {
PlantAction plantAction = (PlantAction) action;
if (plantAction.getActionType() == PlantActionType.PLANT) {
Plant newPlant = plantAction.getPlant().clone();
scene.addPlant(newPlant);
} else if (plantAction.getActionType() == PlantActionType.REMOVE) {
scene.removePlant(plantAction.getPlant());
}
} else if (action instanceof ZombieAction) {
ZombieAction zombieAction = (ZombieAction) action;
if (zombieAction.getActionType() == ZombieActionType.SUMMON) {
Zombie newZombie = zombieAction.getZombie().clone();
scene.addZombie(newZombie);
} else if (zombieAction.getActionType() == ZombieActionType.REMOVE) {
scene.removeZombie(zombieAction.getZombie());
}
}
// 检查场景胜利或失败条件
if (scene.isWin() || scene.isLose()) {
break;
}
// 僵尸前进
for (Zombie zombie : scene.getZombies()) {
zombie.move();
}
// 场景产生阳光
int sunCount = scene.produceSun();
// 玩家收集阳光
collectSun(sunCount);
}
// 游戏结束,显示结果
if (scene.isWin()) {
System.out.println("You win!");
} else {
System.out.println("You lose!");
}
}
// 玩家选择植物
private static Plant selectPlant(Plant[] plants) {
// TODO: 实现玩家选择植物的逻辑
return plants[0];
}
// 玩家采取行动
private static Action selectAction(GameScene scene, Plant[] plants) {
// TODO: 实现玩家采取行动的逻辑
return new PlantAction(PlantActionType.PLANT, plants[0]);
}
// 玩家收集阳光
private static void collectSun(int count) {
// TODO: 实现玩家收集阳光的逻辑
}
}
class GameScene {
// TODO: 实现游戏场景的逻辑
}
class Plant {
// TODO: 实现植物的逻辑
}
class Zombie {
// TODO: 实现僵尸的逻辑
}
class Peashooter extends Plant {
// TODO: 实现豌豆射手的逻辑
}
class Sunflower extends Plant {
// TODO: 实现向日葵的逻辑
}
class NormalZombie extends Zombie {
// TODO: 实现普通僵尸的逻辑
}
class ConeheadZombie extends Zombie {
// TODO: 实现路障僵尸的逻辑
}
// 玩家行动
abstract class Action {
// TODO: 实现玩家行动的逻辑
}
// 植物行动
class PlantAction extends Action {
// TODO: 实现植物行动的逻辑
}
// 植物行动类型
enum PlantActionType {
PLANT,
REMOVE
}
// 僵尸行动
class ZombieAction extends Action {
// TODO: 实现僵尸行动的逻辑
}
// 僵尸行动类型
enum ZombieActionType {
SUMMON,
REMOVE
}
```
写个两个求和的Java程序
1. 普通版
```java
public class SumCalculator {
public static void main(String[] args) {
int a = 5;
int b = 10;
int sum = a + b;
System.out.println("The sum of " + a + " and " + b + " is " + sum);
}
}
```
2. 用户输入版
```java
import java.util.Scanner;
public class SumCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter two numbers to add:");
int a = scanner.nextInt();
int b = scanner.nextInt();
int sum = a + b;
System.out.println("The sum of " + a + " and " + b + " is " + sum);
}
}
```