2017-12-29 53 views
0

내 프로그램의 경우 입력 할 수있는 변수가있는 임의의 범위를 얻으려고 시도하지만 프로그램을 작동하려고 할 때 프로그램이 중단되는 것으로 보입니다.난이도 시작 Float to Integer가있는 임의 번호

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이고 유선이 0.20이면 최대 값은 (10 * (1 + .20) = 12이고 최소값은 10 * (1)이어야합니다. - .20) = 8. 그러나 damageFlux 호출에 의해 던져진 오류를 얻는다면, 그들은 같은 값을 매치한다는 것을 의미합니다 .min과 max에 System.out.println을 수행하면 둘 다 . 10 대신 8과 12 변수 공격이 부정적인 것 같다

+1

https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – shmosel

+1

어떤 오류 당신이 언급하는? –

+0

당신이 얻고있는 에러를 게시하고 모든 코드가 너무 길지 않은 경우 게시하십시오. 안정성이 double 또는 float 유형이 아닌 경우 플럭스가 단순히 float로 변환 된 int를 포함합니다. – prsvr

답변

0

float flux = stability/100;float flux = stability/100.0f;으로 변경해야합니다.

그런 식으로 작업에서 float을 사용하면 사업부의 결과를 float로 승격 할 수 있습니다. 실제 코드는 stabilityint100으로 나눈 값이 int이되도록 반올림합니다. 당신이 두 변수에 10를 볼 이유

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

입니다 : 그런 경우 그렇게 할 때 예에 의해 판단, 그 결과는 flux로 다음 작업이 될, 0에 가깝다.

더 많은 정보를 참조하시기 바랍니다 : Java implicit conversion

0

당신은 UI에서 그것을 얻을

이 가

가 BTW :.. 최소 및 최대의 메시지 값을 오류에 추가 쉽게 디버깅을하게