KSHMR
KSHMR

Reputation: 809

My loop doesn't loop

This is a little strange, I think. My loop doesn't loop, and I have no idea why. There's no error message. The program just runs through once. This is a game I'm creating which makes you throw a 1-9 dice, and you get the choice of removing that number from a list of numbers 1-9, or removing the number itself. This is SUPPOSED to happen until there are no numbers left in the board. But... the loop doesn't loop!!!

public class gudmundur_skilaverkefni9 {
    public static void main (String[] args) {
        System.out.println("Godan dag. Thu ert ad spila Larus.");
        System.out.println("1 2 3 4 5 6 7 8 9");
        int N = 9;
        boolean[] Spilafylki = new boolean[10];

        for (int i = 1; i <= N; i++) {
            System.out.println("Sladu inn slembitolu til ad kasta teningunum.");

            int teningakast = StdIn.readInt();
            int teningatala = (int) (Math.random() * 10);

            System.out.println("Thu fekkst " + teningatala + ".");

            if (teningatala != 2) {
                System.out.println("Viltu taka ut " + teningatala 
                        + " eda velja tvaer tolur sem mynda summu af " 
                        + teningatala + "? (1/2)");

            int tala2 = StdIn.readInt();

            if (tala2 == 2) {
                System.out.println("Slaid inn fyrri tolu:");
                int fyrritala = StdIn.readInt();

                System.out.println("Slaid inn seinni tolu:");
                int seinnitala = StdIn.readInt();

                while (fyrritala+seinnitala != teningatala 
                                            || fyrritala == seinnitala){
                    System.out.println("Thetta gengur ekki! Valdirdu nokkud somu toluna tvisvar? Reyndu aftur:");

                    System.out.println("Slaid inn fyrri tolu:");
                    fyrritala = StdIn.readInt();

                    System.out.println("Slaid inn seinni tolu:");
                    seinnitala = StdIn.readInt();
                }

                System.out.println("Nu litur spilid svona ut:");

                Spilafylki[fyrritala] = true;
                Spilafylki[seinnitala] = true;

                for (i = 1; i <= N; i++) {
                    if (Spilafylki[i] == false)
                        System.out.print(i + " ");
                    else
                        System.out.print(" ");
                }

                System.out.println();
            }

            if (tala2 == 1) {
                Spilafylki[teningatala] = true;

                System.out.println("Nu litur spilid svona ut:");

                for (i = 1; i <= N; i++) {

                    if (Spilafylki[i] == false)
                        System.out.print(i + " ");
                    else
                        System.out.print(" ");
                }

                System.out.println();
            }
        }
        else {
            System.out.println("Thu fekkst tvo. Ekki er hægt ad velja tvaer olikar natturulegar tolur sem mynda 2.");

            System.out.println("Nu litur spilid svona ut:");

            Spilafylki[teningatala] = true;

            for (i = 1; i <= N; i++) {
                if (Spilafylki[i] == false)
                    System.out.print(i + " ");
                else
                    System.out.print(" ");
            }
            System.out.println();

            int count = 0;

            for (i = 1; i <= N; i++)
                if (Spilafylki[i] == true)
                    count++;

             if (count == 9) {
                System.out.println("Allar tolurnar eru farnar. Takk fyrir ad spila Larus.");
                    break;

             }     
         }
    }
}

Upvotes: 0

Views: 131

Answers (1)

Nicola Miotto
Nicola Miotto

Reputation: 3696

It's simple: in the nested loops you are not redeclaring the i variable, hence the same from the outer loop is used. This way, as soon as one of the nested loops is over, i is gonna be equal to N, creating the right condition to make the first loop finish immediately after the first cycle.

So, either you obscure the outer i by redeclaring the i variable in the nested loops

for (int i = 1; ...

Putting int in front of i obscures the one in the current lexical scope. Or, for more readability, use a different variable name for each loop.

Upvotes: 1

Related Questions