This site requires JavaScript, please enable it in your browser!
Greenfoot back
divinity
divinity wrote ...

2016/1/26

how do you break out of a loop

divinity divinity

2016/1/26

#
hi all I have a question, how do you break out of a loop. i am building game and it is doing what it supposed to do but the problems is when the user enter a wrong answer it supposed to go back to the same question and repeat the question. the thing it is repeating the question but at the wrong level and wrong question and also it is in a loop. here is my codes.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
i=1;
         
        for(i=1; i <=4; i++)
        {
            //get random numbers
            intvalue  = 1 + randobj.nextInt(25);
            intvalue2 = 1 + randobj.nextInt(8);
            correctans = intvalue * intvalue2;
            wrongans = 25 + randobj.nextInt(50);
             
            System.out.println(playername+" round 1 "+" level "+ i + " question "+ i + " you are answered each question that correspond with an answer");
            System.out.println(playername+ " please multiply " +intvalue+" * "+ intvalue2);
             
             
            System.out.println("A:"+correctans);
            System.out.println("B:"+wrongans);
            ans = userinput.next().charAt(0);
             
            if(ans == 'a')
            {
                System.out.println(playername+" that answer is correct, you are doing just fine, keep it up ");
                points += points + 450;
                System.out.println("round 1");
                System.out.println("level "+ i);
                System.out.println("question "+ i);
                totalpoints += points;
                System.out.println("total point are "+ totalpoints);
            }
            else if(ans == 'b')
            {
                System.out.println(playername+" yikess!!!!! that is so incorrect, come now,you can do better");
                points -= points- 225;
                System.out.println("round 1");
                System.out.println("level "+ i);
                totalpoints -= points;
                System.out.println("total points is "+ totalpoints);
                count++;
                 
                 
                i=1;
                while(count < 4)
                {
                    System.out.println(playername+" round 1 "+" level "+ i + " question "+ i + " you are answered each question that correspond with an answer");
                    System.out.println(playername+ " please multiply " +intvalue+" * "+ intvalue2);
             
 
                    System.out.println("A:"+correctans);
                    System.out.println("B:"+wrongans);
                    ans = userinput.next().charAt(0);
             
                    if(ans == 'a')
                    {
                        System.out.println(playername+" that answer is correct, you are doing just fine, keep it up ");
                        points += points + 450;
                        System.out.println("round 1");
                        System.out.println("level "+ i);
                        totalpoints += points;
                        System.out.println("total point are "+ totalpoints);
                    }
                    else if(ans != 'b')
                    {
                        System.out.println(playername+" what is wrong you,come on now, inno daiz wrong");
                        points -= points - 225;
                        System.out.println("round 1 ");
                        System.out.println("level "+ i);
                        totalpoints -= points;
                        System.out.println("total points is "+ totalpoints);
                    }
                  
                }
danpost danpost

2016/1/27

#
I believe that the loop you are getting caught inside of is the 'while' loop declared on line 41. Currently, its condition to continue looping is that the value of 'count' be less than four (4). Since the value of 'count' is not altered anywhere inside the 'while' block (lines 42 through 70), once inside, you will never get smoothly out of it. You either need to increase the value of 'count' somewhere inside the loop or change the condition used to keep the loop going to something that will eventually become 'false' within the loop. A good candidate to keep the loop going might be that the value of 'ans' is not 'a' (or, as long as the question is not answered correctly, keep the loop going). Or, maybe a combination of conditions can be used -- like if not answered correctly within four tries. Something you ought to consider is starting the 'while' loop before any attempt to answer a question (that should help to simplify the code a bit). Another thing that does not help is setting 'i' back to one (1) on a wrong answer. So, for example, with that, if the fourth question is answered wrong, three more question will be forthcoming.
divinity divinity

2016/1/27

#
okay thanks am going and try something. have to get this thing to work properly
divinity divinity

2016/1/27

#
hey danpost ah try doing this while(count<=4) and i still inside the loop, what do i have to do to break out of the loop and for it to repeat the correct question and level
danpost danpost

2016/1/27

#
divinity wrote...
hey danpost ah try doing this while(count<=4) and i still inside the loop, what do i have to do to break out of the loop and for it to repeat the correct question and level
I would set 'ans' to some unused character (maybe a blank -- ' '); then, use the condition:
1
while (ans != 'a')
to control the looping.
You need to login to post a reply.