Adding a loop
i am having trouble putting in a loop into this bit of code. I need iot to keep asking for the base and height, until -1 is entered (after which it exits). HELP!!!!!
import javax.swing.JOptionPane;
public class Methods
{
public static int areaOfRec (int Base, int Height)
{
return Base*Height;
}
public static int perOfRec (int Base, int Height)
{
return 2*(Base+Height);
}
public static void main (String args[])
{
String strBase, strHeight;
int Base, Height;
strBase=JOptionPane.showInputDialog("Enter Base");
Base=Integer.parseInt(strBase);
strHeight=JOptionPane.showInputDialog("Enter Height");
Height=Integer.parseInt(strHeight);
System.out.println("Area: "+areaOfRec(Base, Height));
System.out.println("Perimeter: "+perOfRec (Base, Height));
System.exit(0);
}
}
[918 byte] By [
SahsaBaz] at [2007-9-30 23:16:31]

Hi, Sasha
When you need to have something done repetitively, you generally need a loop.
There are 2 types of loops in Java:
1. a loop with a fixed iteration limit (only iterates x times)
2. a loop whose every iteration is only executed if a given condition evaluates to true.*
1. is a for loop:
for(int i=0; i<5; i++){
//...
}
2. is a while/do-while loop:
while(something is true){
//...
}
OR
do{
//...
}while(something is true);
I'm sure loops were covered in your class.
Go over your notes or ask your teacher to revise the material.
Or have a look at the Java Tutoria'sl [url=http://java.sun.com/docs/books/tutorial/java/nutsandbolts/flow.html]section[/url] on Control Flow Statements (incl. loops)
Kind regards, :)
- lutha
--
* the do-while loop always executes at least once.
For the second till nth time, the given condition is evaluated each time,
and only if it evaluates to true are tje loop's content statements (if any)
executed.
It's probably the best option in your case...
>I need iot to keep asking for the base and height, until -1 is entered (after which it exits).
Here's a linguistic stepwise refinement: so while not -1 is entered you keep on
asking for the base and height? This leads to something like:while (!minusOneEntered())
// do something ...
Or should something be done without the question being asked before? I don't
think so, so lets stick with the while loop. Now '-1 entered' for what? The width?
The height? Both? None one of them? Lets stick with the latter; here's a small
refinement step:while (askForWidth() != -1 && askForHeight() != -1)
// do something ...
Now what is that 'do something' supposed to be; lets see:System.out.println("Area: "+areaOfRec(Base, Height));
System.out.println("Perimeter: "+perOfRec (Base, Height));
A darn, you want two variables 'Base' and 'Height' to have some values. BTW,
don't capitalize variable names, but that's an aside.
So within that while condition those two variables have to receive a value; the
following will do for now:while ((width= askForWidth()) != -1 && (height= askForHeight()) != -1) {
// do something ...
System.out.println("Area: "+areaOfRec(Base, Height));
System.out.println("Perimeter: "+perOfRec (Base, Height));
}
All we have to do now is implement those askForWidht() and askForHeight()
methods; I'm sure you can handle that ...
kind regards,
Jos (thirsty ...)
JosAH at 2007-7-7 13:48:58 >
