0

I have an arrayList of objects called Gear. Each object has attributes of String Name, String Type, Int rateOfFire and Int Damage. I also have a function that calculates damage per second (DPS) for each weapon by multiplying damage by rateOfFire. The problem I'm having is that this function prints off the damage of each weapon in a list, but i would also like the total DPS of all weapons in the list.

This is my method:

public static void getDPS()

    {
    for (int i = 0; i < Gear.size(); i++) 
        {
        int damage =  Gear.get(i).damage;
        int fireRate = Gear.get(i).fireRate;
        int DPS =(damage*fireRate)/60;
        System.out.println("DPS for weapon " + i+1 + " is: " +DPS);
        System.out.println("Total DPS= " );
        }
    }

And this is my output:

DPS for weapon 01 is: 10

Total DPS= 


DPS for weapon 11 is: 12

Total DPS= 

Any advice would be greatly appreciated as I am only a novice to programming!

1
  • 2
    You will also want to change "..." + i+1 + "..." into "..." + (i+1) + "..." : without the parenthesis since the operators have the same order of precedence, "..."+i is applied first, and the 1 is concatenated as string after Commented Jul 13, 2017 at 15:07

3 Answers 3

1
int total = 0;//define a new variable

for (int i = 0; i < Gear.size(); i++) 
{
    int damage =  Gear.get(i).damage;
    int fireRate = Gear.get(i).fireRate;
    int DPS =(damage*fireRate)/60;
    total += DPS; // add to total var.
    System.out.println("DPS for weapon " + i+1 + " is: " +DPS);
}

System.out.println("Total DPS= " + total);//print the total outside the loop

Also as @Aaron mentioned in the comment wrap i+1 using parenthesis (i+1) to avoid String concatenation.

Sign up to request clarification or add additional context in comments.

Comments

0

Just declare a total DPS variable before the loop, add the DPS of the current weapon in the loop, then print the total .

int totalDPS = 0;

for (int i = 0; i < Gear.size(); i++) 
    {
    int damage =  Gear.get(i).damage;
    int fireRate = Gear.get(i).fireRate;
    int DPS =(damage*fireRate)/60;
    totalDPS += DPS; // add current DPS to total
    System.out.println("DPS for weapon " + i+1 + " is: " +DPS);
    System.out.println("Total DPS= " + totalDPS );

    }

Comments

0

Of course there is also a "streamy" solution ...

class Gear {
    private String name;
    private String type;
    private int rateOfFire;
    private int damage;

    // getters and setters ...
}

Create a test List:

List<Gear> gears = new ArrayList<>();
gears.add(new Gear("a", "a", 10, 10));
gears.add(new Gear("b", "b", 5, 5));

Print DPS per Gear and total DPS:

gears.forEach(x -> 
  System.out.println("dps = " + x.getDamage() * x.getRateOfFire()));
System.out.println("Total dps = " + 
  gears.stream().mapToInt(x -> x.getDamage() * x.getRateOfFire()).sum());

Which prints:

dps = 100
dps = 25
Total dps = 125

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.