java很难启动随机浮点到整数
对于我的程序,我试图用我可以输入的变量获得一个随机范围,但当我试图运行它时,它似乎会导致程序崩溃。不过,我已经指出了主要问题所在:
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 错误是:
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)
现在,在我的头脑中,最小值和最大值应该是有效的,但它不是。它会导致抛出错误,很可能是因为它没有进行数学运算,从而导致两个不同。以下是关于它应该如何工作的几个变量:
如果攻击为10,通量为.20,则最大值应为(10*(1+.20)=12,最小值应为(10*(1-.20)=8。然而,如果我得到damageFlux调用抛出的错误,那么这意味着它们匹配相同的值。通过做一个系统。出来在最小值和最大值上打印,它们都返回为10,而不是8和12
共 (0) 个答案