Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to make loop with different values 1

Status
Not open for further replies.

mutual

Programmer
Jun 8, 2000
6
US
Bellow is my code that calculate area of triangle where<br>a,b,c are sides of triangle and s is semiperimeter.program works fine,but I need main function call method Calcarea 3 times with different values for a,b and c.I'm very new in java.can't figure out how. Any help will be greatly appreciated.<br><br><br>&nbsp;&nbsp;public class Heron {&nbsp;&nbsp;&nbsp;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;static int a=3,b=4,c=5,s=0,area=0;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;private static boolean IsValidTriangle(int a,int b,int c,int s) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (s&gt;a && s&gt;b && s&gt;c)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return true;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return false; <br>&nbsp;} <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;private static int SemiPerimeter(int a, int b, int c) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return s=(a+b+c)/2; <br>&nbsp;} <br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;private static int CalcArea(int a, int b, int c,int s) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return area=(int)Math.sqrt(s*(s-a)*(s-b)*(s-c));<br>&nbsp;} <br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public static void main(String args[] ) {<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SemiPerimeter(a,b,c);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (IsValidTriangle(a,b,c,s))<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CalcArea(a,b,c,s);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(
 
how about this:<br><br>public static void main(String args[])<br>{<br>int[] a = {1,2,3}; //can be any numbers or variables<br>int[] b = {4,5,6};<br>int[] c = {2,3,4};<br>int s = 10;<br>int area;<br><br>for(int i = 0; i &lt; 3; i++)<br>{<br>&nbsp;&nbsp;area = calcArea(a<i>,b<i>,c<i>,s); //method name should begin with lowercase and internal words should be capitalized<br>&nbsp;&nbsp;System.out.println(area);<br><br>}<br><br><br>} <p> fenris<br><a href=mailto:fenris@hotmail.com>fenris@hotmail.com</a><br><a href= > </a><br> I am interested in Mining Software, as well as Genetic Algorithms.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top