Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Need help with Sentinel Control Loop

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help with Sentinel Control Loop

    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.


  2. #2
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Need help with Sentinel Control Loop

    The problem is your "or" condition. This should be an "and". The way your condition reads now, in basic english, is "while base is not -1 or height is not -1" so it evaluates, base is -1, but height isn't, keep going! If this is an "and" it will work as you expect.

    Side note: the way your code is written, if the user puts in a -1 then it executes "theHypotenuseIs" and still puts output. Therefore your code is not stopping immediately after you get your stop condition. You should add in a mechanism to check for good input before doing the calculation and output.

Similar Threads

  1. timer control
    By amber_hell in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 26th, 2013, 07:16 AM
  2. Printer control
    By bardd in forum Java Theory & Questions
    Replies: 4
    Last Post: August 31st, 2012, 08:15 AM
  3. Setting variable correctly in sentinel-controlled loop
    By exchaoordo in forum Loops & Control Statements
    Replies: 1
    Last Post: June 1st, 2012, 03:40 PM
  4. How to control the time that a function takes to execute in a loop?
    By GodspeedPR in forum Loops & Control Statements
    Replies: 4
    Last Post: July 20th, 2011, 03:37 PM
  5. [SOLVED] DoublyLinkedList out of control!
    By javapenguin in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 13th, 2010, 07:06 AM