2011-07-13 22 views
1

我有14个我定义的变量,但只希望将这些变量应用于通过用户输入显式调用时开发的对数。我制作的对数是用于角色创建的桌面RPG组,并使用“元素对齐”来影响统计数据。一个人可以使用的最多的是其中的5个,但不少于1个。挑选和选择要激活的变量以及用户输入方法

double elemFire 
double elemIce 
double elemWater 
double elemWind 
double elemEarth 
double elemPoison 
double elemGravity 
double elemShadow 
double elemLight 
double elemElec 
double elemHoly 
double elemAnti 
double elemVoid 
double elemTime 

使用用户输入和确保最少一个,最多5可以在任何给定时间使用我将如何把这些具体的变量?

用于计算的对数是绝对巨大的,因此我不想再为该大块代码创建14个副本。最大的目标是让所调用的1-5个变量为核心统计数据添加累积效应,以便于显示。

这是对数。

Scanner sin = new Scanner(System.in); 
     System.out.print("What year were you born? :"); 

     yearBorn = sin.nextDouble(); 
     logHPCalc = 2.0/16.0; 
     yourHP = yearBorn * logHPCalc; 
     yourMP = yourHP/2.0; 
     yourNP = yourMP/3.0; 
      yourPSI = yourNP + yourMP/32.0; 
      logATK = yearBorn/60.0; 
      logDEF = yearBorn/120.0; 
      logMAG = yearBorn/60.0; 
      logSPR = yearBorn/120.0; 
      logSPD = yearBorn/60.0; 
      logLCK = yearBorn/120.0; 
      logEATK = yearBorn/60.0; 
      logEDEF = yearBorn/120.0; 
       statATK = yourHP/2.0 + logATK; 
       statDEF = yourHP/3.0 + logDEF; 
       statSPR = yourMP/2.0 + logSPR; 
       statMAG = yourMP/3.0 + logMAG; 
       statSPD = yourNP/2.0 + logSPD; 
       statLCK = yourNP/3.0 + logLCK; 
       statEATK = yourPSI/2.0 + logEATK; 
       statEDEF = yourPSI/3.0 + logEDEF; 

     System.out.print("What year is it? :"); 

       yearNow = sin.nextDouble(); 
       logHPCalcNow = logHPCalc * yearNow + 2.0 * 32.0; 
       yourHPNow = yearBorn * logHPCalcNow; 
      yourMPNow = yourHPNow/2.0; 
       yourNPNow = yourMPNow/3.0; 
       yourPSINow = yourMPNow + yourNPNow/32.0; 
        logATKNow = yearBorn/60.0 * yearNow + 2.0 * 32.0; 
        logDEFNow = yearBorn/120.0 * yearNow + 2.0 * 32.0; 
        logMAGNow = yearBorn/60.0 * yearNow + 2.0 * 32.0; 
        logSPRNow = yearBorn/120.0 * yearNow + 2.0 * 32.0; 
        logSPDNow = yearBorn/60.0 * yearNow + 2.0 * 32.0; 
        logLCKNow = yearBorn/120.0 * yearNow + 2.0 * 32.0; 
        logEATKNow = yearBorn/60.0 * yearNow * 2.0 * 32.0; 
        logEDEFNow = yearBorn/120.0 * yearNow * 2.0 * 32.0; 
         statATKNow = yourHPNow/2.0 + logATKNow; 
         statDEFNow = yourHPNow/3.0 + logDEFNow; 
         statSPRNow = yourMPNow/2.0 + logSPRNow; 
         statMAGNow = yourMPNow/3.0 + logMAGNow; 
         statSPDNow = yourNPNow/2.0 + logSPDNow; 
         statLCKNow = yourNPNow/3.0 + logLCKNow; 
         statEATKNow = yourPSINow/200.0 + logEATKNow; 
         statEDEFNow = yourPSINow/300.0 + logEDEFNow; 

     nonHPBase = 200; 
     nonATKBase = 5; 
     nonDEFBase = 5; 
     nonSPDBase = 4; 
     nonLCKBase = 2; 
     nonHPGain = nonHPBase * nonDEFBase - 10 + yearNow; 
     nonATKGain = nonHPBase * nonATKBase/25 + yearNow/200; 
     nonDEFGain = nonHPBase * nonATKGain/110 + yearNow/200; 
     nonATKDEFSum = nonATKGain + nonDEFGain; 
     nonSPDGain = nonHPBase * nonATKDEFSum/1000 + yearNow/200; 
     nonSPDSum = nonHPBase * nonATKDEFSum/2000; 
     nonLCKGain = nonHPBase * nonSPDSum/400 + yearNow/200; 


usrTRANS = 4; 

     transHP = yearBorn * logHPCalc * usrTRANS; 
    transMP = yourHP/2.0 * usrTRANS; 
    transNP = yourMP/3.0 * usrTRANS; 
     transPSI = yourNP + yourMP/32.0 * usrTRANS; 
      translogATK = yearBorn/60.0 * usrTRANS; 
      translogDEF = yearBorn/120.0 * usrTRANS; 
      translogMAG = yearBorn/60.0 * usrTRANS; 
      translogSPR = yearBorn/120.0 * usrTRANS; 
      translogSPD = yearBorn/60.0 * usrTRANS; 
      translogLCK = yearBorn/120.0 * usrTRANS; 
      translogEATK = yearBorn/60.0 * usrTRANS; 
      translogEDEF = yearBorn/120.0 * usrTRANS; 
       transstatATK = yourHP/2.0 + logATK * usrTRANS; 
       transstatDEF = yourHP/3.0 + logDEF * usrTRANS; 
       transstatSPR = yourMP/2.0 + logSPR * usrTRANS; 
       transstatMAG = yourMP/3.0 + logMAG * usrTRANS; 
       transstatSPD = yourNP/2.0 + logSPD * usrTRANS; 
       transstatLCK = yourNP/3.0 + logLCK * usrTRANS; 
       transstatEATK = yourPSI/2.0 + logEATK * usrTRANS; 
       transstatEDEF = yourPSI/3.0 + logEDEF * usrTRANS; 
       translogHPCalcNow = logHPCalc * yearNow + 2.0 * 32.0 * usrTRANS; 
       transHPNow = yearBorn * logHPCalcNow * usrTRANS; 
      transMPNow = yourHPNow/2.0 * usrTRANS; 
       transNPNow = yourMPNow/3.0 * usrTRANS; 
       transPSINow = yourMPNow + yourNPNow/32.0 * usrTRANS; 
        translogATKNow = yearBorn/60.0 * yearNow + 2.0 * 32.0 * usrTRANS; 
        translogDEFNow = yearBorn/120.0 * yearNow + 2.0 * 32.0 * usrTRANS; 
        translogMAGNow = yearBorn/60.0 * yearNow + 2.0 * 32.0 * usrTRANS; 
        translogSPRNow = yearBorn/120.0 * yearNow + 2.0 * 32.0 * usrTRANS; 
        translogSPDNow = yearBorn/60.0 * yearNow + 2.0 * 32.0 * usrTRANS; 
        translogLCKNow = yearBorn/120.0 * yearNow + 2.0 * 32.0 * usrTRANS; 
        translogEATKNow = yearBorn/60.0 * yearNow * 2.0 * 32.0 * usrTRANS; 
        translogEDEFNow = yearBorn/120.0 * yearNow * 2.0 * 32.0 * usrTRANS; 
         transstatATKNow = yourHPNow/2.0 + logATKNow * usrTRANS; 
         transstatDEFNow = yourHPNow/3.0 + logDEFNow * usrTRANS; 
         transstatSPRNow = yourMPNow/2.0 + logSPRNow * usrTRANS; 
         transstatMAGNow = yourMPNow/3.0 + logMAGNow * usrTRANS; 
         transstatSPDNow = yourNPNow/2.0 + logSPDNow * usrTRANS; 
         transstatLCKNow = yourNPNow/3.0 + logLCKNow * usrTRANS; 
         transstatEATKNow = yourPSINow/200.0 + logEATKNow * usrTRANS; 
         transstatEDEFNow = yourPSINow/300.0 + logEDEFNow * usrTRANS; 

我想这14个变量影响到这一点,但只有当上调用,并且只需要最少的1,最大值为5.代码优化帮助是可选的,但将不胜感激。

==============编辑评论=============

现在呃,兰迪?它不喜欢做数学。 这是元素变量是如何设置:

elemFire  = 10 * 2 * 2/32 
elemIce  = 10 * 2 * 3/32 
elemWater = 10 * 2 * 4/32 
elemWind  = 10 * 2 * 5/32 
elemEarth = 10 * 2 * 6/32 
elemPoison = 10 * 2 * 7/32 
elemGravity = 10 * 2 * 8/32 
elemShadow = 10 * 2 * 9/32 
elemLight = 10 * 2 * 10/32 
elemElec  = 10 * 2 * 11/32 
elemHoly  = 10 * 2 * 12/32 
elemAnti  = 10 * 2 * 13/32 
elemVoid  = 10 * 2 * 14/32 
elemTime  = 10 * 2 * 15/32 

我需要这种东西能够做数学。否则,如果我可以换我的头在你的“添加的东西到地图”的指示和

for (String element : elements) { 
yourHp += elementMap.get(element); 

thingamajig这将是完美的,如果我可以注入我的大arsed类此。

---------------编辑7/15 ---------------------

Rob,什么..确切的是在你写的代码中的最终输出变量,因为要么我不知道它是哪一个,或者我找不到它。

我打算用你写在里面的东西来粘贴我的代码。它被评论。它不会运行,因为我要么不知道最终输出变量在元素选择数组中的位置,要么它根本不存在。

在那里我试图把它使其工作明确标记。

http://pastebin.com/SFVRbs08

道歉标签缺乏,由于引擎收录。

+1

这是“算法”,而不是“对数” –

+0

对不起,我确实看到了一些最丑陋的代码,而且很丑,我觉得有很多冗余的代码可以通过使用方法和可能的数组或集合大大简化。这样做会使调试和升级更容易。 –

+0

“新手”的哪部分你不知道? – lolkuro

回答

1

像这样? (可运行的例子)

// Import our collections (Maps and Lists) 
import java.util.*; 

public class Elements { 
    // Instead of having 14 separate variables, you can store your 
    // "element effects" in a Map, using the element name as a key. 
    private static Map<String, Integer> elementMap = new HashMap<String, Integer>(); 

    static { 
     elementMap.put("fire", 10 * 2 * 2/32); 
     elementMap.put("ice", 10 * 2 * 3/32); 
     elementMap.put("water", 10 * 2 * 4/32); 
     elementMap.put("wind", 10 * 2 * 5/32); 
     elementMap.put("earth", 10 * 2 * 6/32); 
     elementMap.put("poison", 10 * 2 * 7/32); 
     elementMap.put("gravity", 10 * 2 * 8/32); 
     elementMap.put("shadow", 10 * 2 * 9/32); 
     elementMap.put("light", 10 * 2 * 10/32); 
     elementMap.put("elec", 10 * 2 * 11/32); 
     elementMap.put("holy", 10 * 2 * 12/32); 
     elementMap.put("anti", 10 * 2 * 13/32); 
     elementMap.put("void", 10 * 2 * 14/32); 
     elementMap.put("time", 10 * 2 * 15/32); 
    } 

    public static void main(String[] args) { 
     List<Integer> elementValues = getElementValues(); 
     double yourHp = 1234.0; 
     System.out.println("Starting HP: " + yourHp); 
     for (Integer elementValue : elementValues) { 
      yourHp += elementValue; 
     } 
     System.out.println("Ending HP: " + yourHp); 
    } 

    private static List<Integer> getElementValues() { 
     // Your input scanner: 
     Scanner sin = new Scanner(System.in); 
     // This list will keep track of all the elements entered by the user 
     List<String> elements = new ArrayList<String>(); 
     // Ask for up to 5 elements 
     while (elements.size() < 5) { 
      System.out.println("Select up to 5 elements ('q' to finish selecting) [" + elements.size() + " selected so far]: "); 
      // Get what the user entered (they have to hit "Enter" before it gets read) 
      String element = sin.next(); 
      if (element.trim().equals("q")) { 
       if (elements.size() == 0) { 
        // If the user asked to quit but hasn't selected any elements, 
        // prompt them again 
        System.out.println("You must select at least one element:"); 
       } else { 
        // If they want to quit and have selected at least one, then 
        // break out of the while loop and keep going. 
        break; 
       } 
      } else { 
       // They didn't ask to quit, so maybe they entered an element 
       if (elementMap.get(element) == null) { 
        // If elementMap.get() returns null, then it's not a valid element 
        System.out.println("Not a valid element, try again:"); 
       } else { 
        // They entered a good element, so store it 
        elements.add(element.toLowerCase()); 
       } 
      } 
     } 
     // Now there are between 1 and 5 elements stored in the elements list, 
     // and we just have to get their values that are stored in the map. 
     List<Integer> elementValues = new ArrayList<Integer>(); 
     for (String element : elements) { 
      elementValues.add(elementMap.get(element)); 
     } 
     return elementValues; 
    } 
} 

(附注:这是 “algorithm”,而不是 “logarithm” --they're字谜,但非常不同的事情。)

+0

犯错,你可以评论,所以我知道什么是什么?此外,它似乎只计算火和水,并非全部都是14. D: 另外,我将如何将这个结合到我的主类中?我是java的相对新手,我正在做这个变量的练习,所以我得到了他们的舒适。我不知道如何导入东西。 – lolkuro

+0

已评论。再看一次。尝试运行它并在提示符处输入“fire”。然后“q”退出。 –

+0

好吧,这是非常漂亮的,但我想在这里做的人,是调用这14个变量,并将它们添加到计算,以抵消结果。 – lolkuro