Advertisement
ssunlimited

Fighter Rankings Extended

Jun 27th, 2022
1,126
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.55 KB | None
  1. import java.util.*;
  2.  
  3. public class Fighter implements Comparator<Fighter> {
  4.    
  5.    public String name;
  6.    public int atk, def, powerLevel;
  7.    public static List<Fighter> fighters = new ArrayList<Fighter>();
  8.  
  9.    public Fighter(String name, int atk, int def){
  10.       this.name = name;
  11.       this.atk = atk;
  12.       this.def = def;
  13.       this.powerLevel = CalculatePowerLevel(atk, def);
  14.       }
  15.    
  16.    public int CalculatePowerLevel(int atk, int def){
  17.       return atk * def;
  18.       }
  19.    
  20.    public static void main(String[] args){
  21.       fighters = new ArrayList<Fighter>();
  22.       Scanner console = new Scanner(System.in);
  23.       boolean inLoop = true;
  24.       do{
  25.       System.out.println("What is the name of the fighter?");
  26.       String name = console.next();
  27.       System.out.println("What is the Attack stat of " + name + "?");
  28.       int atk = console.nextInt();
  29.       System.out.println("What is the Defense stat of " + name + "?");
  30.       int def = console.nextInt();
  31.       Fighter currentFighter = new Fighter(name, atk, def);
  32.       fighters.add(currentFighter);
  33.       System.out.println("Add 1 more fighter? Type in 'true' to confirm or 'false' to cancel.");
  34.       inLoop = console.nextBoolean();
  35.       } while(inLoop);
  36.       Collections.sort(fighters, new Fighter("None", 0, 0));
  37.       System.out.println("The strongest fighters by power level are: ");
  38.       for(Fighter fighter : fighters) System.out.println(fighter.name);
  39.       }
  40.    
  41.    @Override
  42.    public int compare(Fighter a, Fighter b){
  43.       return b.powerLevel - a.powerLevel;
  44.    }
  45.  
  46. }
Advertisement
RAW Paste Data Copied
Advertisement