#Array Mehthod isnt working

33 messages · Page 1 of 1 (latest)

balmy hull
#

final int n;
final int m;
//false = ausgeschieden, true = ist noch im kreis
private boolean[] Kkreis;

public Aufgabe3() {
    this.n = 6;
    this.m = 5;
    Kkreis = new boolean[n];
    for(int i = 0; i < Kkreis.length; i++) {
        Kkreis[i] = true;
    }
}

public void durchlauf() {
    int counter = 0;
    while(true) {
        for(int i = 0; i < this.Kkreis.length; i++) {
            counter ++;
            if(counter == m) {
                Kkreis[i] = false;
                counter = 0;
            }
        }
    }    

The Kkreis array should be after the durchlauf method false

nocturne jacinthBOT
#

This post has been reserved for your question.

Hey @balmy hull! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

ocean wagon
#

why do you have a while (true) in your method

#

there's no break or return there, so it's an infinite loop

#

also maybe try do describe the actual problem please...

balmy hull
#

I just wanted a algorthm in the durchlauf method, that can change the index to true

#

from the final m varibale

ocean wagon
#

not the task, the problem you're encountering

balmy hull
#

i tried to fix the loop problem, but i am still failing. The while true loop should be broken, if the array is completly false: `this.n = 6;
this.m = 5;
Kkreis = new boolean[n];
for(int i = 0; i < Kkreis.length; i++) {
Kkreis[i] = true;
}
}

public void durchlauf() {
    int counter = 0;
    int breaker = 0;
    while(true) {
        for(int i = 0; i < this.Kkreis.length; i++) {
            counter ++;
            if(counter == m) {
                Kkreis[i] = false;
                counter = 0;
            }
            for (int j = 0; j < Kkreis.length; j++) {
                if(this.Kkreis[j] == false) {
                    breaker++;
                }
            }
            if(breaker == this.Kkreis.length) {
                break;
            }
        }
    }`
ocean wagon
#

you aren't really giving enough info to help...

balmy hull
#

this part should break my while loop for (int j = 0; j < Kkreis.length; j++) { if(this.Kkreis[j] == false) { breaker++; } } if(breaker == this.Kkreis.length) { break;

ocean wagon
#

you aren't resetting breaker? is that the issue? it should be inside the while loop

#

dude you aren't being very helpful here

#

we need info about the problem in order to solve it

balmy hull
#

i have now a return and not a break, so the loop the not infinite

#

the ouput is

#

true true true true false true

#

so on the index of m is now a false and thats correct

#

but i want the algorithm

#

the right side is not useful

#

the left side is what i want

#

in the duchlauf method

#

but after the first pass it returns

#

so i need the other passes

#

i changed the method:

#

`public void durchlauf() {
int counter = 0;
int breaker = 0;
while(true) {
for(int i = 0; i < this.Kkreis.length; i++) {
counter ++;
if(counter == m) {
Kkreis[i] = false;
counter = 0;
}
for (int j = 0; j < Kkreis.length; j++) {
if(this.Kkreis[j] == false) {
breaker++;
}
}
if(breaker == this.Kkreis.length-1) {
return;
}
breaker = 0;
}
}

}`
#

now the output is: false false false false false true

#

the last true must also be false

#

ok the method works now

#

`public void durchlauf() {
int counter = 0;
int breaker = 0;
while(true) {
for(int i = 0; i < this.Kkreis.length; i++) {
counter ++;
if(counter == m) {
Kkreis[i] = false;
counter = 0;
}
for (int j = 0; j < Kkreis.length; j++) {
if(this.Kkreis[j] == false) {
breaker++;
}
}
if(breaker == this.Kkreis.length) {
return;
}
breaker = 0;
}
}

}`
#

thank u for ur help @ocean wagon

nocturne jacinthBOT
# balmy hull thank u for ur help <@628241756235890708>

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.