I have to do this problem where I use a Sentinel Control loop to repeatedly get the lenghts of the base and height of a triangle from a user, call the method, and then display the Values of the three sides of the triangle. This all has to take place inside a sentinel control loop where the user has to enter a -1 to terminate the loop. My problem is that after I get the base and height and do the calculations, the loop will not terminate when -1 is entered only one time. Even though I have an or conditional in my while loop, the program will only terminate if both are -1. What do I have to do to get the loop to terminate as soon as a -1 is entered, but still be able to calculate the hyp using my method theHypotenuseIs?
import java.util.*; public class Hypotenuse { public static double theHypotenuseIs(double base, double height) { double calculation, hyp; hyp= Math.sqrt((Math.pow(base,2) + Math.pow(base,2))); return hyp; } public static void main(String[]args) { final int FLAG = -1; double hypotenuse, base, height; Scanner input = new Scanner(System.in); while(base!=FLAG ||height!=FLAG) { base = input.nextDouble(); height = input.nextDouble(); hypotenuse = theHypotenuseIs(base, height); System.out.println("The Base is:" + base); System.out.println("The Height is:" + height); System.out.println("The Hypotenuse is:" + hypotenuse); } } }
--- Update ---
Never mind, I found a workaround. IDK if it's good style, but I added a conditional after each input that will break if the value is -1.