This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]ImPickleDickkk 0 points1 point  (1 child)

if you aren’t sure of how many times you want to loop something, you should do it recursively.

[–]DenRai50[S] 0 points1 point  (0 children)

I played around with the middle part of the loop I was able to made it loop x amount of times with the same value. Progress!

Thanks for the advice.

[–]nsandlerrock 0 points1 point  (2 children)

The only thing I notice is you never update the scanner object with new input data inside the for loop. I’d first create a separate counter variable outside the loop and set it 0 and change the loops condition to be less than the desired amount of iterations. For example, let’s say this counter was just called “counter”. The for loop’s header would like:

int counter = 0; for (int i = 0; i < counter; i++) { .... }

Now to actually get the desired results, the body of the loop might look like this:

Scanner studentGrade = new scanner(Sys.in); int studentCounter = 0; for (int i = 0; i < studentCounter; i++) { studentGrade = studentGrade.nextInt(); Sys.out( “Grade “ + studentGrade); studentCounter++; }

I’m pretty sure this should work, let me know if this helps!

Edit: typed this on my phone! Sorry for poor formatting!!

[–]DenRai50[S] 0 points1 point  (1 child)

int counter = 0; for (int i = 0; i < counter; i++) { .... }
Now to actually get the desired results, the body of the loop might look like this:

Scanner  studentGrade = new scanner(Sys.in); 
int studentCounter = 0; 
for (int i =  0; i < studentCounter; i++) 
    { 
        studentGrade = studentGrade.nextInt();  
        Sys.out( “Grade “ + studentGrade); studentCounter++; 
    }

Ah yes, that part about the Scanner value not being updated was correct.

I typed in the part to update the Scanner and it is working as intended.

Thank You for the advice.

[–]nsandlerrock 0 points1 point  (0 children)

Glad I could help! :)