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 4 of 4

Thread: Not sure whether I'm using a loop correctly and whether what I'm doing is possible

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Not sure whether I'm using a loop correctly and whether what I'm doing is possible

    Hi,

    I've currently written a program that reads in two values from the keyboard and stores them. The next part of my program is where its confusing me.

    I'm trying to repeatedly ask a further two more questions which then does some simple calculations with the data that is read in from the keyboard until a value of zero is met which kills the loop. Now I understand the concept behind what I'm trying to do requires a loop which I'm assuming would be a Do While loop.

    Its when I try to print every calculation, I want to also print a ratio between the previous and the present values except the first iteration which I've really know clue about doing.(I know how to work out the ratio)

    But I dont know how I would print the ratio missing out the first run? Because in a loop a message prints constantly regardless, so is there a way I can get a message from out side the loop or something?
    Last edited by monkeyhead; November 9th, 2010 at 12:37 PM.


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Not sure whether I'm using a loop correctly and whether what I'm doing is possibl

    Quote Originally Posted by monkeyhead View Post
    Hi,

    I've currently written a program that reads in two values from the keyboard and stores them. The next part of my program is where its confusing me.

    I'm trying to repeatedly ask a further two more questions which then does some simple calculations with the data that is read in from the keyboard until a value of zero is met which kills the loop. Now I understand the concept behind what I'm trying to do requires a loop which I'm assuming would be a Do While loop.

    Its when I try to print every calculation, I want to also print a ratio between the previous and the present values except the first iteration which I've really know clue about doing.(I know how to work out the ratio)

    But I dont know how I would print the ratio missing out the first run? Because in a loop a message prints constantly regardless, so is there a way I can get a message from out side the loop or something?
    It would be nice if you could show your code.

    How to repeatedly ask for a value until a value is reached is

    static Scanner console = new Scanner(System.in);
    int x = 0;

    while (x !=0)
    {
    int x = console.nextInt();

    // do calculations.
    }

    if you have the calculated result that's supposed to be 0, have

    while (calculationResultVariable != 0)

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Not sure whether I'm using a loop correctly and whether what I'm doing is possibl

    A do...while loop did come to my mind even before I noticed that part in your post. However, that may not be the best format now that I think of it.

    A do....while merely executes a condition at least once, even if the while value is going to return false or whatever from the first iteration.

    The reason you're skipping the first iteration is because there isn't, at least I don't think so, I don't have your code, a previous value.

    You'll either get 0 or an Arithmetic Exception

  4. #4
    Junior Member
    Join Date
    Nov 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Not sure whether I'm using a loop correctly and whether what I'm doing is possibl

    Worked it out, thanks.
    Last edited by monkeyhead; November 9th, 2010 at 04:29 PM.

Similar Threads

  1. for loop and while loop problems
    By Pulse_Irl in forum Loops & Control Statements
    Replies: 4
    Last Post: May 3rd, 2010, 02:09 AM
  2. hi. i want to rewrite this do loop into a while loop.
    By etidd in forum Loops & Control Statements
    Replies: 3
    Last Post: January 26th, 2010, 05:27 PM
  3. loop or what
    By silverspoon34 in forum Loops & Control Statements
    Replies: 5
    Last Post: November 19th, 2009, 02:10 PM
  4. Need help with loop
    By SwEeTAcTioN in forum Loops & Control Statements
    Replies: 8
    Last Post: October 25th, 2009, 05:59 PM
  5. can any one do this in a loop? (for loop)
    By chronoz13 in forum Loops & Control Statements
    Replies: 4
    Last Post: October 4th, 2009, 08:31 PM