• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help with while-loop in constructor.

 
raul vargas
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have my constructor made but I can't seem to figure out how to set up this while-loop

The constructor code:



Instructions on how to make the loop:

The constructor should initialize all elements of the arrays to -1.0 to indicate there is no score in that element yet.


 
fred rosenberger
lowercase baba
Bartender
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does it have to be a while-loop?
Do you know the syntax for a while loop?
Do you know what they mean by "initialize all elements of the arrays to -1.0 "

Personally, I think a for loop would be better, but technically, you don't need any loop at all.

So...What SPECIFICALLY do you need help with?
 
raul vargas
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Yes, it has to be a while-loop. Those are the only loops I can work with.

I don't know how to initialize to -1.0

The example loop I have looks like this:


 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does it have to be a while loop? Typically, when the programmer can identify positively how many times the loop must execute, either a for or enhanced for loop would be used. Here are the Java tutorials on looping.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raul vargas wrote:
Yes, it has to be a while-loop. Those are the only loops I can work with.

I don't know how to initialize to -1.0

The example loop I have looks like this:



if it must, it must...
you are missing part of the while loop. Generally, they look like this:




Those braces are important. You need a closing one for each opening one, and your example is missing the closing one.

So you have the line "int i = 0;". That is just there to set up the starting conditions.

You then have "while (i < 10)". Each time through the loop, that condition is checked. So hopefully, i will be changing inside your loop, otherwise you will be stuck in there forever - what is called an "infinite loop".

So you need to put code inside the curly brackets - what is called the body of the loop. You have "i++" in the loop. Each time that line executes, i will be incremented by 1. so the first time, it will change from 0 to 1, then from 1 to 2, etc. That means that this loop will run ten times before quitting. That is convenient, since you have an array with ten elements. The common idiom is to use your variable i as an index (that's why i is often used as the variable name) of the array.

To access a specific element of the array, you say "arrayName[index]". In your example, you have one array called quizzes. you set a value the same way as any other double variable. If i had a double named "fred", i would say

fred = 1.0;

since i have an array, i can say

this.quizzes[3] = 1.0;

that would set the fourth element to 1.0 (remember, arrays are zero based indexed, so the first element is this.quizzes[0]).

So you could explicitly name all ten elements and set each to -1:

this.quizzes[0] = -1.0;
this.quizzes[1] = -1.0;
this.quizzes[2] = -1.0;
...etc

or you could use a variable...and index...that is changing. sounds like our variable 'i' from above.

Why don't you give it a try, and show us what you come back with.
 
raul vargas
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Is this correct?

 
Paul Clapham
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since it doesn't assign anything to any array entries, and the point of the code was to assign -1.0 to some array entries, then... No, it isn't correct.
 
raul vargas
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

How do I do this? I've tried but I obviously don't know what I'm doing
 
Junilu Lacar
Bartender
Posts: 7602
54
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What references have you read about the while-loop and what don't you understand?

You should know that this site is NotACodeMill (<--click). We will guide you around obstacles but we expect you to ShowSomeEffort (<--click-->) ItDoesntWorkIsUseless and won't cut it with folks around here.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic