原英语版本
My enemies are many,my equals are none. In the shade of olive trees,they said Italy could never be conquered.In the land of pharoahs and kings, they said Egypt could never be humbled.In the realm of forest and snow,they said russia could never be tamed.Now they say nothing.They fear me ,like a force of nature,a dealer in thunder and death.I say I am Napoleon,I am emperor........Burn it.
强大滴JAVA版(不知道运行结果如何)
import java.util.HashSet;
public class Napoleon {
private HashSet enemies = new HashSet();
// cdps = Chrysanthemum Damage Per Second
public int cdpm = 100000;
Napoleon() {
enemies.add(new Enemy("Italy", 100));
enemies.add(new Enemy("Egypt", 100));
enemies.add(new Enemy("Russia ", 100));
}
private void speak()
{
System.out.println("My enemy number: " + enemies.size());
HashSet trueEnemies = new HashSet();
for (Enemy e : enemies) {
if (e.cdpm > cdpm)
trueEnemies.add(e);
}
System.out.println("The number of enemies who can beat me: " + trueEnemies.size());
for (Enemy e : enemies) {
do {
e.shout();
} while (e.canBeatNapoleon(this));
}
for (Enemy e : enemies) {
e.shout();
}
cdpm = 10000000;
System.out.println("I am Napoleon cdpm " + cdpm);
System.out.println("Dispose enemy list...");
enemies.clear();
System.out.println("Enemy list disposed!");
}
public static void main(String[] args) {
(new Napoleon()).speak();
}
}
class Enemy {
private String name;
public int cdpm;
Enemy(String name, int cdpm) {
this.name = name;
this.cdpm = cdpm;
}
public void shout() {
if (cdpm > 0)
System.out.println(name + " says: we are invincible!!!");
else System.out.println(name + " says: ......");
}
public boolean canBeatNapoleon(Napoleon n) {
if (cdpm < n.cdpm) {
cdpm = -1;
return false;
}
return true;
}
}
output:
My enemy number: 3
The number of enemies who can beat me: 0
Egypt says: we are invincible!!!
Italy says: we are invincible!!!
Russia says: we are invincible!!!
Egypt says: ......
Italy says: ......
Russia says: ......
I am Napoleon cdpm 10000000
Dispose enemy list...
Enemy list disposed!
模仿java版的c#版(老子玉树临风提供)来源
using System;
using System.Collections.Generic;
public abstract class Person
{
/// <summary>
/// name
/// </summary>
public string Name { get; set; }
/// <summary>
/// Chrysanthemum Damage Per Second
/// </summary>
public int CDPM { get; set; }
}
public class Enemy : Person
{
public Enemy(string name, int cdpm)
{
this.Name = name;
this.CDPM = cdpm;
}
public void Shout()
{
if (this.CDPM > 0)
{
Console.WriteLine(string.Format("{0} says: we are invincible!!!", this.Name));
}
else
{
Console.WriteLine(string.Format("{0} says: ......", this.Name));
}
}
public bool IsCanBeatNapoleon(Napoleon n)
{
if (n == null)
{
throw new ArgumentNullException("Napoleon was not initialized...");
}
return this.CDPM > n.CDPM;
}
}
public class Napoleon : Person
{
private IList<Enemy> enemies = new List<Enemy>();
private void Speak()
{
Console.WriteLine("My enemy number: " + enemies.Count);
IList<Enemy> trueEnemies = new List<Enemy>();
foreach (Enemy item in enemies)
{
if (item.CDPM > CDPM)
{
trueEnemies.Add(item);
}
}
Console.WriteLine("The number of enemies who can beat me: " + trueEnemies.Count);
foreach (Enemy item in enemies)
{
do
{
item.Shout();
}
while (item.IsCanBeatNapoleon(this));
}
Console.WriteLine("I am {0},my cdpm is {1}", this.Name, this.CDPM);
Console.WriteLine("Dispose enemy list...");
enemies.Clear();
Console.WriteLine("Enemy list disposed!");
}
static void Main(string[] args)
{
(new Napoleon()).Speak();
Console.Read();
}
}