I have 14 variables that I have defined, but only want these to apply to a logarithm I have developed when explicitly called on via User Input. The logarithm I've made is for a tabletop RPG group of mine for character creation and uses "elemental alignments" in order to influence statistical data. The most a person can use are 5 of these at any time but no less than one.
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
How would I call these specific variables using user input and ensure that a minimum of one and a maximum of 5 can be used at any given time?
The logarithm for calculations is absolutely massive as is and I do not want to have to make 14 more copies of that massive block of code. The biggest ambition is to cause the 1-5 variables called on to add a cumulative effect to the core stats for easier display.
This is the logarithm.
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;
开发者_JS百科 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;
I want these 14 variables to affect this, but only when called on, and only a minimum of 1 and a maximum of 5. Code optimization help is optional but would be GREATLY appreciated.
==============EDIT FOR A COMMENT=============
now uhh, Randy? It doesn't like doing math. This is how the element variables are set up:
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
I need this thing to be able to do math. otherwise if i can wrap my head around your "add things to the map" instructions and the
for (String element : elements) {
yourHp += elementMap.get(element);
thingamajig it'll be perfect if i can inject this into my big-arsed class.
---------------EDIT 7/15 ---------------------
Rob, what.. exactly is the ultimate output variable in that code you wrote out, because either I'm not understanding which one it is or i can't find it.
I'm going to pastebin my code with what you wrote inside it. It is commented. It won't run, because i either do not know where the ultimate output variable is in your element selection array is, or it simply does not exist.
where I have TRIED to put it to make it work is clearly marked.
http://pastebin.com/SFVRbs08
apologies for tab lacking, due to pastebin.
Something like this? (runnable example)
// 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;
}
}
(P.S. It's "algorithm", not "logarithm"--they're anagrams, but very different things.)
精彩评论