Reputation: 9
I'm relatively new to java. My inner while loops aren't functioning? What could be the reason?
I am trying to run the while loops within the while loop first and work my way out to the outermost while loop. Isn't that how you operate scenarios like this?
Scanner scan = new Scanner(System.in);
int count = 0;
while (count>=0)
{
int num = 0;
while(num<1 && num>4)
{
System.out.println("Which aircraft would you like to simulate?");
System.out.println("1. Blimp");
System.out.println("2. Helicopter");
System.out.println("3. Fighter Jet");
System.out.println("4. Space Shuttle");
num = scan.nextInt();
int num2 = 0;
while(num2 < 1 && num2>6)
{
System.out.println("Please select your programmable characterisitics for simulated flight.");
System.out.println("1. Position Trim ");
System.out.println("2. Force Breakout");
System.out.println("3. Force Gradient");
System.out.println("4. Force Friction");
System.out.println("5. Damping");
System.out.println("6. Hard Stop");
num2 = scan.nextInt();
if(num2 == 1)
{
System.out.println("The position to which a flight control returns");
}
else if(num2 == 2)
{
System.out.println("A force that returns a control to Trim. This is a constant force applied toward Trim which remains the same despite how far the control is moved (displacement) and how fast a control is moved (velocity).");
}
else if(num2 == 3)
{
System.out.println("A force that returns a control to Trim, but one that varies with displacement. The farther the control is moved, the stronger the force applied toward trim.");
}
else if (num2 == 4)
{
System.out.println("A constant force that is opposite to the direction of movement");
}
else if(num2 == 5)
{
System.out.println("A force that is oppisite to the direction of movement. Damping varies with velocity. The faster a control is moved the stronger the force.");
}
else if (num2 == 6)
{
System.out.println("A force that simulates a mechanical limit of travel. By varying the Hard Stops, the range of travel can be adjusted");
}
else
{
System.out.println("Invalid input");
}
}
}
}
Upvotes: 0
Views: 241
Reputation: 900
Looking over the code, it looks like you want to run the contents of the loop, and then test for the condition.
In that case, you want to use a do ... while
loop.
Also, as pointed out by ProgrammerDan, the condition you're testing for is impossible. num
cannot be both less than 1 and greater than 4, so the loop would not continue past the first run. If you want to re-run the loop while num is outside the range of 1-4, then test that num is less than 1 or (||
) greater than 4.
int num = 0;
do
{
//
// your loop contents here
//
} while(num<1 || num>4);
Upvotes: 1
Reputation: 1543
Programs flow from top to bottom, not inner to outer. It is true that inner loops are checked before outer, but that is because the bottom of the inner loop is always reached before the bottom of the outer. Anyway . . .
You need ||, not &&. You want the loop to continue if the input is lower than 1 or great than 4 (or 6). A do ... while() might be better here.
Secondly, your outer loop seems to do nothing.
Here is a working fix:
public class MyClass {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int num = 0;
while (num < 1 || num > 4) {
System.out.println("Which aircraft would you like to simulate?");
System.out.println("1. Blimp");
System.out.println("2. Helicopter");
System.out.println("3. Fighter Jet");
System.out.println("4. Space Shuttle");
num = scan.nextInt();
int num2 = 0;
while (num2 < 1 || num2 > 6) {
System.out.println("Please select your programmable characterisitics for simulated flight.");
System.out.println("1. Position Trim ");
System.out.println("2. Force Breakout");
System.out.println("3. Force Gradient");
System.out.println("4. Force Friction");
System.out.println("5. Damping");
System.out.println("6. Hard Stop");
num2 = scan.nextInt();
if (num2 == 1) {
System.out.println("The position to which a flight control returns");
} else if (num2 == 2) {
System.out.println("A force that returns a control to Trim. This is a constant force applied toward Trim which remains the same despite how far the control is moved (displacement) and how fast a control is moved (velocity).");
} else if (num2 == 3) {
System.out.println("A force that returns a control to Trim, but one that varies with displacement. The farther the control is moved, the stronger the force applied toward trim.");
} else if (num2 == 4) {
System.out.println("A constant force that is opposite to the direction of movement");
} else if (num2 == 5) {
System.out.println("A force that is oppisite to the direction of movement. Damping varies with velocity. The faster a control is moved the stronger the force.");
} else if (num2 == 6) {
System.out.println("A force that simulates a mechanical limit of travel. By varying the Hard Stops, the range of travel can be adjusted");
} else {
System.out.println("Invalid input");
}
}
}
}
}
Upvotes: 1
Reputation: 7922
while(num < 1 && num > 4)
will always evaluate to false
.
while(num2 < 1 && num2 > 6)
will also always evaluate to false
.
If you change the <
and >
signs around, your while loops should work better :)
Upvotes: 1