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
Re: Loop Question - Very new beginner
Quote:
how would I make the question change each loop
Put the questions into an array.
Re: Loop Question - Very new beginner
Quote:
Originally Posted by
Norm
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.
Re: Loop Question - Very new beginner
Edit!! I got it! Thank u guys for the help!!!
Re: Loop Question - Very new beginner
@Mr777
How would you change the question asked at statement #2 in your list?
Re: Loop Question - Very new beginner
Quote:
Originally Posted by
Norm
@Mr777
How would you change the question asked at statement #2 in your list?
What i get by OP's post is,
Quote:
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.
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.
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
Re: Loop Question - Very new beginner
Quote:
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
Re: Loop Question - Very new beginner
Try this out may it help
import java.io.*;
Quote:
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”);
}
}
}
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.
Re: Loop Question - Very new beginner
Ohhh... i forget to make use of try... catch block..
Quote:
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...
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'?