Lowest of Given numbers.
Hi
I am trying to develop a program which can type the lowest of 3 given integers. I have tried using the "IF-else" statement, but I still think there is a simpler way to do it. I also heard of something called the "Switch Case" Statement, which I am not very sure of. Someone also told me to use the "MIN VALUE" statement. How do I use this. Some help from all of you will be greatly appreciated
Abhay
Show how you are currently doing it please and then we will tell you if theres a better way :-)
You could use the java.util.Collections.min-function.-Puce
Pucea at 2007-7-12 21:29:00 >

public class MathHack {
public static int min(int i, int j, int k) {
return i < j && i < k ? i : min(j, k, i);
}
public static void main(String[] args) {
System.out.println("min = "+min(3,1,2));
}
}
> min(3,3,3);
You have to go and break it, eh?
; )
public class MathHack {
public static int min(int i, int j, int k) {
return i <= j && i <= k ? i : min(j, k, i);
}
public static void main(String[] args) {
System.out.println("min = "+min(3,1,2));
}
}
What I am actually trying to do is store this calculation inside an ActionListener
My code for this particular area as as follows:
if (e.getSource()==calcGrat) {
actGrat = Integer.parseInt(actualGratField.getText());
servicePeriod = Integer.parseInt(numServiceField.getText());
govtGratLimit = 350000;
lastDrawSalMain = Integer.parseInt(salField.getText());
lastDrawSal = lastDrawSalMain/12;
formulaTaxCalc = 15/26 * (lastDrawSal * servicePeriod);
if (actGrat < govtGratLimit) {
noGrat = actGrat;
} else if (actGrat < formulaTaxCalc) {
actGrat = noGrat;
} else if (govtGratLimit < actGrat) {
noGrat = govtGratLimit;
} else if (govtGratLimit < formulaTaxCalc) {
noGrat = govtGratLimit;
} else if (formulaTaxCalc < actGrat) {
noGrat = formulaTaxCalc;
} else if (formulaTaxCalc < govtGratLimit) {
noGrat = formulaTaxCalc;
}
taxableGrat = actGrat - noGrat;
I have been using If-else statements, but I am not getting the desired results. There seems to be a major flaw in this design.
Please have a look and let me know
Thank you
Abhay
Message was edited by:
abhaybhargav
please use the Math.min(int a, int b) to get the lowest.
> please use the Math.min(int a, int b) to get the> lowest.That's only two numbers.
huh,int n = Math.min(a,b);n = Math.min(n,c);Message was edited by: Stone.li
> min(3,3,3);Wouldn't min(6,6,6) be a better example?
Thanks...Will try the Math.min thing....Abhay
But isnt it better If i used Double instead of int for numbers having decimals, etc....These are Financial Figures and they definitely will have Decimal calculations. Any views on this?
> But isnt it better If i used Double instead of int
> for numbers having decimals, etc....These are
> Financial Figures and they definitely will have
> Decimal calculations. Any views on this?
Yeah don't do that. Doubles are not good for financial figures.
But will int values allow decimals and the like... Arent they whole numbers?
If you want to compare the two double to a crumb, you would transfer the double to the long and compare the long value.
> But will int values allow decimals and the like...> Arent they whole numbers?You really need to read this. http://docs.sun.com/source/806-3568/ncg_goldberg.html
Alright I will read it and check it out. Thanks