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

Thread: help with defining a changing variable within a loop

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

    Question help with defining a changing variable within a loop

    I'm having trouble with a homework assignment. I'll post the broad objective of it below so you understand what the end result should be. I understand how to use if loops to compare numeric values and tell the user if a number is greater lesser than or equal to another. I understand the basic concepts of while
    loops and how to set parameters on how many times they run. Yet I have spent hours trying to figure out how I can compare two user values within a loop. My only thought of how to perhaps accomplish this is to get user input twice during the while loop(meaning I would only loop5 times instead of 10) yet I don't know if this will work or if this is the best way to tackle the problem. It only needs to be written in pseudocode at the moment which helps. But i am just at a loss of how to define and compare user inputted values that are constantly changing within a loop. I've attached a picture of the assignment objective, if my question does not make sense out of context. I would appreciate any help with this aspect of my assignment. I am almost at my wits end! Once I understand this I can easily write the pseduocode and draw the flowchart by myself. Yet I am at a standstill without knowing how to define the variables in this particular situation.




    In this assignment, you will write an algorithm that asks the user for 10 different numbers. Afier
    each number, you should print whether this number is bigger than, smaller than, or the same as
    the one that came right before it. (Note that you won’t print anything after the first number, as
    nothing came before it.) Below is an example. I have put in BLACK the information that your
    algorithm should print, and in RED information that the user is providing. (The red is just the parts that says x is bigger/smaller than y)
    Enter numberi 14
    Enter numberi 20
    20 is bigger than 14
    Enter numberi 5
    5 is smaller than 20
    Enter numberi 5
    5 is the same as 5
    Enter numberi 10
    10 is bigger than 5
    Enter numberi -100
    -100 is smaller than 10
    Enter numberi 0
    0 is bigger than -100
    Enter numberi 0
    0 is the same as 0
    Enter numberi 57
    57 is bigger than 0
    Enter numberi 25
    25 is smaller than 57

    You should type your algorithm Your solution should include:
    0 The steps to solving the problem (your steps should look similar to the examples in the
    Designing Loops lecture notes). You should use a loop to get the 10 numbers from the
    students, and you should use appropriate variables for storing the current number entered
    by the user and the previous number entered by theuser

    Sorry for the weird spelling things I used an OCR translator to get the info in txt form

    THANKS FOR THE HELP!


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

    Default help with defining a changing variable within a loop

    I'm having trouble with a homework assignment. I'll post the broad objective of it below so you understand what the end result should be. I understand how to use if loops to compare numeric values and tell the user if a number is greater lesser than or equal to another. I understand the basic concepts of while
    loops and how to set parameters on how many times they run. Yet I have spent hours trying to figure out how I can compare two user values within a loop. My only thought of how to perhaps accomplish this is to get user input twice during the while loop(meaning I would only loop5 times instead of 10) yet I don't know if this will work or if this is the best way to tackle the problem. It only needs to be written in pseudocode at the moment which helps. But i am just at a loss of how to define and compare user inputted values that are constantly changing within a loop. I've attached a picture of the assignment objective, if my question does not make sense out of context. I would appreciate any help with this aspect of my assignment. I am almost at my wits end! Once I understand this I can easily write the pseduocode and draw the flowchart by myself. Yet I am at a standstill without knowing how to define the variables in this particular situation.




    In this assignment, you will write an algorithm that asks the user for 10 different numbers. Afier
    each number, you should print whether this number is bigger than, smaller than, or the same as
    the one that came right before it. (Note that you won’t print anything after the first number, as
    nothing came before it.) Below is an example. I have put in BLACK the information that your
    algorithm should print, and in RED information that the user is providing. (The red is just the parts that says x is bigger/smaller than y)
    Enter numberi 14
    Enter numberi 20
    20 is bigger than 14
    Enter numberi 5
    5 is smaller than 20
    Enter numberi 5
    5 is the same as 5
    Enter numberi 10
    10 is bigger than 5
    Enter numberi -100
    -100 is smaller than 10
    Enter numberi 0
    0 is bigger than -100
    Enter numberi 0
    0 is the same as 0
    Enter numberi 57
    57 is bigger than 0
    Enter numberi 25
    25 is smaller than 57

    You should type your algorithm Your solution should include:
    0 The steps to solving the problem (your steps should look similar to the examples in the
    Designing Loops lecture notes). You should use a loop to get the 10 numbers from the
    students, and you should use appropriate variables for storing the current number entered
    by the user and the previous number entered by theuser

    Sorry for the weird spelling things I used an OCR translator to get the info in txt form

  3. #3
    Member Zyrion's Avatar
    Join Date
    Feb 2013
    Location
    Iowa
    Posts
    106
    My Mood
    Angelic
    Thanks
    2
    Thanked 8 Times in 8 Posts

    Default Re: help with defining a changing variable within a loop

    Post your compilable code or your code with any associated compiler errors; it will be easier to see what you are having trouble with.

  4. #4
    Member Zyrion's Avatar
    Join Date
    Feb 2013
    Location
    Iowa
    Posts
    106
    My Mood
    Angelic
    Thanks
    2
    Thanked 8 Times in 8 Posts

    Default Re: help with defining a changing variable within a loop

    What exactly are you having issues with solving? What code have you done so far (even pseudo-code)? Please post it so we can tell exactly where you are struggling at.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: help with defining a changing variable within a loop

    how to define the variable within the while loop so that it stores the previous and current
    Maybe defining them outside the loop would work better.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: help with defining a changing variable within a loop

    Quote Originally Posted by Zyrion View Post
    Post your compilable code or your code with any associated compiler errors; it will be easier to see what you are having trouble with.
    Sorry Zyrion. I thought I could explain in words. I was a little fed up with code last night. Ha I was wrong though. Heres my current pseudocode I'm leaning towards in my head. I've tried to highlight the section I am unsure about.

    import.java.util.*;
    public static void main(String[] args){

    int count = 0
    set up new scaner

    while (count<10)

    count++

    ask user for value (print)
    get user input
    (this below is the part I'm not sure is translatable to java, if it is how would i accomplish this)
    int num1 = previous user entered value
    int num2 = current user entered value

    if num1< num2
    print num 1 "is less than" num 2
    end if
    if num1>num2
    print num1 "is greater than" num2
    end if
    if num1=num2
    print num1 "is equal to" num2
    end if
    end while




    }

    thanks for your help zyrion and Norm



    --- Update ---

    Quote Originally Posted by Norm View Post
    Maybe defining them outside the loop would work better.
    is it possible to do this when the loop gathers the user input? if so how would i tie the two together and have the int continuously update throughout the loop?

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: help with defining a changing variable within a loop

    You can define a variable outside the loop and assign it values inside the loop.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: help with defining a changing variable within a loop

    Quote Originally Posted by Norm View Post
    You can define a variable outside the loop and assign it values inside the loop.
    Thanks Norm! I will look into this and let you guys know if I have further questions.

Similar Threads

  1. Replies: 149
    Last Post: February 19th, 2013, 10:04 PM
  2. Replies: 1
    Last Post: February 14th, 2013, 07:30 PM
  3. For-loop: initialisation of variable, can't set variable to value zero?
    By Bitbot in forum Loops & Control Statements
    Replies: 4
    Last Post: July 15th, 2012, 02:32 PM
  4. calling a changing variable from another class
    By bondage in forum Collections and Generics
    Replies: 11
    Last Post: December 7th, 2011, 10:17 AM
  5. JButton Auto-changing Reference Variable
    By bgroenks96 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: June 19th, 2011, 10:57 PM