0

For my program, I am trying to get a random range with variables I can input, but it seems to cause the program to crash when I try to work it. I have pinpointed where the main problem is, though:

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import java.util.Random;

public class main {
    private static Text attackBox;
    private static Text cooldownBox;
    private static Text ammoBox;
    private static Text stabilityBox;
    static int attack = 0;
    static int ammo = 0;
    static int stability = 0;
    static int damage = 0;
    static int damageRaw = 0;
    static int minuteDamage = 0;
    static int minuteDamageRaw = 0;
    static float attacksPerMinute = 0;
    static float cooldown = 0;
    static Random rand;

    /**
     * Launch the application.
     * @param args
     */
    public static void main(String[] args) {

        Display display = Display.getDefault();
        Shell shell = new Shell();
        shell.setSize(450, 269);
        shell.setText("Xenolbade X DPS Calculation");
        shell.setLayout(null);

        Label lblAttack = new Label(shell, SWT.NONE);
        lblAttack.setBounds(10, 10, 69, 15);
        lblAttack.setText("Attack:");

        Label lblCooldown = new Label(shell, SWT.NONE);
        lblCooldown.setBounds(10, 31, 69, 15);
        lblCooldown.setText("Cooldown: ");

        Label lblAmmo = new Label(shell, SWT.NONE);
        lblAmmo.setBounds(10, 52, 69, 15);
        lblAmmo.setText("Ammo:");

        Label lblStability = new Label(shell, SWT.NONE);
        lblStability.setBounds(10, 73, 69, 15);
        lblStability.setText("Stability:      \u00B1");

        attackBox = new Text(shell, SWT.BORDER);
        attackBox.setText("0");
        attackBox.setBounds(85, 10, 76, 21);

        cooldownBox = new Text(shell, SWT.BORDER);
        cooldownBox.setText("0");
        cooldownBox.setBounds(85, 31, 76, 21);

        ammoBox = new Text(shell, SWT.BORDER);
        ammoBox.setText("0");
        ammoBox.setBounds(85, 52, 76, 21);

        stabilityBox = new Text(shell, SWT.BORDER);
        stabilityBox.setText("0");
        stabilityBox.setBounds(85, 73, 76, 21);

        Label label = new Label(shell, SWT.NONE);
        label.setBounds(10, 94, 414, 15);
        label.setText("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");

        Label lblWithoutStability = new Label(shell, SWT.NONE);
        lblWithoutStability.setBounds(10, 115, 414, 15);
        lblWithoutStability.setText("Within a minute, without stability, your weapon would do:");

        Label rawDamageLabel = new Label(shell, SWT.NONE);
        rawDamageLabel.setBounds(10, 136, 414, 30);
        rawDamageLabel.setText(damageRaw + " damage " + (int)attacksPerMinute + " times for a total of " 
        + minuteDamageRaw + " damage per minute.");

        Label lblNewLabel_1 = new Label(shell, SWT.NONE);
        lblNewLabel_1.setBounds(10, 172, 414, 15);
        lblNewLabel_1.setText("If we include the stability, we can estimate a minute of combat to do:");

        Label stabilityDamageLabel = new Label(shell, SWT.NONE);
        stabilityDamageLabel.setBounds(10, 193, 414, 30);
        stabilityDamageLabel.setText(damage + " damage " + (int)attacksPerMinute + " times for a total of " 
        + minuteDamage + " damage per minute.");

        Button runButton = new Button(shell, SWT.NONE);
        runButton.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                damage = 0;
                damageRaw = 0;
                minuteDamage = 0;
                minuteDamageRaw = 0;
                attack = Integer.valueOf(attackBox.getText());
                cooldown = Float.valueOf(cooldownBox.getText());
                ammo = Integer.valueOf(ammoBox.getText());
                stability = Integer.valueOf(stabilityBox.getText());
                attacksPerMinute = 60 / cooldown;

                damageRaw = attack * ammo;
                minuteDamageRaw = damageRaw * (int)attacksPerMinute;
                rawDamageLabel.setText(damageRaw + " damage " + (int)attacksPerMinute + " times for a total of " 
                + minuteDamageRaw + " damage per minute.");

                float flux = stability/100;
                int max = (int)(attack * (1 + flux));
                int min = (int)(attack * (1 - flux));
                System.out.println(min);
                System.out.println(max);
                for (int i = 0; i < attacksPerMinute; ++i) {
                    damage = 0;
                    for (int j = 0; j < ammo; ++j) {
                        damage += damageFlux(min, max);
                        //
                    }
                    minuteDamage += damage;
                }
                stabilityDamageLabel.setText(damage + " damage " + (int)attacksPerMinute + " times for a total of " 
                + minuteDamage + " damage per minute.");
            }
        });
        runButton.setBounds(349, 10, 75, 25);
        runButton.setText("Open Fire!");

        shell.open();
        shell.layout();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }

    private static int damageFlux(int min, int max) {

        if (min >= max) {
            throw new IllegalArgumentException("max must be greater than min");
        }
        Random r = new Random();
        return r.nextInt((max - min) + 1) + min;
    }
}

T The error:

Exception in thread "main" java.lang.IllegalArgumentException: max must be greater than min
    at main.damageFlux(main.java:144)
    at main.access$4(main.java:141)
    at main$1.widgetSelected(main.java:120)
    at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:249)
    at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:86)
    at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4428)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1079)
    at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4238)
    at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3817)
    at main.main(main.java:135)

Now, in my head, the min and max should work, but its not. It causes the error to be thrown, most likely because its not doing the math to cause the pair to be different. Here are a couple variables on how it should work:

If attack is 10 and the flux is .20, then max should be (10 * (1 + .20) = 12 and min should be (10 * (1 - .20) = 8. However, if I am getting the error thrown by the damageFlux call, then it means they are matching the same value. by doing a System.out.println on the min and max, they are both returning as 10 instead of 8 and 12.

2 Answers2

0

It seems that variable attack is negative. You get it from the UI.

BTW: Add to error message values of min and max. It makes easier debugging

LunaVulpo
  • 3,043
  • 5
  • 30
  • 58
0

You should change float flux = stability/100; to float flux = stability/100.0f;.

That way, by having a float in the operation, you promote the division's result to a float. Your actual code makes it so that stability, an int, divided by 100 an other int, results in a rounded answer. Judging by your examples, the result of that is closer to 0 so when that's the case, your next operations with flux become:

int max = (int)(attack * (1 + 0));
int min = (int)(attack * (1 - 0));

Which is why you see 10 in both variables.

For more info, please see: Java implicit conversion

Mat
  • 1,440
  • 1
  • 18
  • 38