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

Thread: Loop Question - Very new beginner

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

    Default Loop Question - Very new beginner

    Hi,

    Im very new to Java and learning the basics in school. Were now on our very first assignment and I have a question about loops.

    I dont want to post the code I have as I fear I could get in trouble by the school but my question is this,

    If my current loop was printing a question that says "Person 1 do u like apples?" the user replies with "yes" and the program says "good"
    it then repeats the question and if you say "no" it says "bad". my question is how would I make the question change each loop. For example

    //
    Person 1 do u like apples?
    Yes
    Good
    Person 2 do u like apples?
    no
    Bad
    //

    I know what I said is probably extremely difficult to understand so i'm sorry for that.
    Thanks to anyone who can help


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Loop Question - Very new beginner

    how would I make the question change each loop
    Put the questions into an array.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Callcollect (November 22nd, 2011)

  4. #3
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Loop Question - Very new beginner

    Quote Originally Posted by Norm View Post
    Put the questions into an array.
    Really? I don't think if it would be any good idea. OP just wants to print Person 1,2, so on. Not the names. So, he/she can handle it through the loop variable.

    @CallCollect: Did you study if else and loops? If Yes,
    1. Define a loop.
    2. Write a simple string inside the loop.
    3. Get user input in some String object.
    4. Use if else.
    5. If answer is Yes, print what do you want.
    6. If answer is No, print what do you want.
    7. If answer is different, print what do you want.
    8. Repeat the loop.

    Note: Set the loop condition to the number of times you want to ask questions.

  5. The Following User Says Thank You to Mr.777 For This Useful Post:

    Callcollect (November 22nd, 2011)

  6. #4
    Junior Member
    Join Date
    Nov 2011
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Loop Question - Very new beginner

    Edit!! I got it! Thank u guys for the help!!!
    Last edited by Callcollect; November 22nd, 2011 at 08:40 AM.

  7. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Loop Question - Very new beginner

    @Mr777
    How would you change the question asked at statement #2 in your list?

  8. #6
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Loop Question - Very new beginner

    Quote Originally Posted by Norm View Post
    @Mr777
    How would you change the question asked at statement #2 in your list?
    What i get by OP's post is,
    Player 1 Do you like this?
    Player 2 Do you like ths?
    .
    .
    .
    So, this can be done. And if everytime a new question, yes you need an array or Collection.

  9. #7
    Member
    Join Date
    Dec 2011
    Location
    United States
    Posts
    94
    My Mood
    Amused
    Thanks
    5
    Thanked 8 Times in 8 Posts

    Default Re: Loop Question - Very new beginner

    The reason why it sounds a little too complicated for her to use arrays is because they just started the course and they are still working on loops and if statements. I think making her attempt arrays might make her too confused at the moment - although a good way to think big as a programmer.

  10. #8
    Junior Member
    Join Date
    Jan 2012
    Posts
    16
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: Loop Question - Very new beginner

    I'm also a begginer, in my opinion the best way at the start to make loops, is:
    1.use do{...}while();
    2.create a bollean ans use for(int i =0; boolean; i++); when u want to end the loop put boolean=false;

    But I'm still new at this and is what i've been using

  11. #9
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Loop Question - Very new beginner

    create a bollean ans use for(int i =0; boolean; i++); when u want to end the loop put boolean=false;
    you mean a boolean for a FOR-LOOP to control the iteration just like passing true or false?, well in my opinion(not being a super clean coder) but for-loop's operands where meant for an explicit coding, putting just a boolean 'flag' which will have true or false determining the run for a FOR-LOOP is not a good coding practice, in my experience(although not that much).

    for-loops iterative process is control by 3 parts, with these approach, a boolean flag for the condition, makes the for-loop's initialization and update part obsolete
    Last edited by chronoz13; January 6th, 2012 at 11:58 AM.

  12. #10
    Junior Member
    Join Date
    Jan 2012
    Location
    Mumbai
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Loop Question - Very new beginner

    Try this out may it help
    import java.io.*;
    Class Abc
    {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    for(int i=1;i<=i+1;i++)
    {
    System.out.println(“Person ”+i+” do u like apples?”);
    String s1=br.readLine();
    if(s1==”Yes” || s1==”Y”)
    {
    System.out.println(“Good”);
    }
    else
    {
    System.out.println(“Bad”);
    }
    }

    }
    Last edited by RaoPatek; January 18th, 2012 at 02:38 AM.

  13. #11
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Loop Question - Very new beginner

    That won't compile. But that's OK because the OP announced that they had figured it out ... last November.

  14. #12
    Junior Member
    Join Date
    Jan 2012
    Location
    Mumbai
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Loop Question - Very new beginner

    Ohhh... i forget to make use of try... catch block..

    import java.io.*;
    Class Abc
    {

    try
    {

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    for(int i=1;i<=i+1;i++)
    {
    System.out.println(“Person ”+i+” do u like apples?”);
    String s1=br.readLine();
    if(s1==”Yes” || s1==”Y”)
    {
    System.out.println(“Good”);
    }
    else
    {
    System.out.println(“Bad”);
    }
    }

    }
    catch(Exception e)
    {
    System.out.println("ERROR FOUND");
    }

    }
    This time sure it works...

  15. #13
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Loop Question - Very new beginner

    By "sure" do you mean you took the time to run it and verified that it isn't consistently 'Bad'?

Similar Threads

  1. Beginner question on where to put an array
    By Diplo in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 3rd, 2011, 03:56 PM
  2. Beginner Question
    By jrt224 in forum Loops & Control Statements
    Replies: 1
    Last Post: March 10th, 2011, 12:56 PM
  3. [SOLVED] Simple question from a beginner
    By jimmylee7706 in forum Java Theory & Questions
    Replies: 1
    Last Post: March 6th, 2011, 09:57 PM
  4. Loop Patterns (triangles) [Beginner]
    By me1010109 in forum Loops & Control Statements
    Replies: 2
    Last Post: October 24th, 2010, 05:13 PM
  5. [SOLVED] While Loop Help (beginner)
    By Perplexing in forum Loops & Control Statements
    Replies: 4
    Last Post: October 23rd, 2010, 02:00 PM