0

I am making a guessing game for fun. For some reason, the code in the for loop never processes. I didvided it into three parts. Please let me knowCan someone help me? I have checked and the code doesnt proceed int the for loop. I am certain there is nothing wrong with the for loop. Thanks for your hwlp Thanks

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;

public class Guess {
    public static Random r = new Random();
    public static BufferedReader in = new BufferedReader(
            new InputStreamReader(System.in));

    public static final String YES_S = "y";
    public static final String NO_S = "n";

    public static void main(String [] args) throws IOException { 
        boolean menu = true;
        boolean start = false;
        boolean end = false;
        boolean ans = true;
        boolean rand = true;

        int num = -1;

        int guessNum = -1;

        while(menu) {
             System.out.println( "Start game [ y ]:");
             String input = in.readLine();

             if(input.equals(YES_S)) {
                 menu = false;
                 start = true;
                 end = false;
             }
        }

        while(start) {
            while(ans) {
                while(rand) {
                    num = r.nextInt(11);
                    rand = false;
                }

                for (int i = 0; i > 3; i++) {
                    System.out.println( " Guess a number from 0 to 10 :");
                    String input1 = in.readLine();
                    guessNum = Integer.parseInt(input1);
                    if (guessNum == num) {
                        System.out.println( " Congratulations !");
                        ans = false;
                        rand = true;
                    } else {
                        System.out.println( " Try again");

                    }


                }
                if(ans = true) {
                   end = true;   
                }
            }

        }
    }
}
1
  • Side note: you should try do less things in one method. A loop in a loop in a loop in a loop ... such code turns unmaintainable the second you close your editor. If you are programming for fun; I really recommend to read "Clean code" by Robert Martin; as programming is even more fun when one still understands his code after not touching it for a few days.
    – GhostCat
    Commented Mar 19, 2016 at 21:43

5 Answers 5

7

Well, let's decompose your for loop:

for (int i = 0; i > 3; i++) 
  1. Start with i = 0
  2. Perform the body while i > 3 .... whops, do you see the problem?
2
  • Thanks. wow I didnt notic that lol
    – ugh
    Commented Mar 19, 2016 at 21:36
  • 1
    Just in case you were wondering why I recommended you to focus on the quality of your code ... exactly because that helps with such kind of mistakes.
    – GhostCat
    Commented Mar 19, 2016 at 21:46
1
for (int i = 0; i > 3; i++) {

The guard condition is never true - 0 > 3 is false immediately, so the loop never runs.

Use i < 3 as the guard instead.

1

You have i>3 and i=0... 0 is not greater than 3. So, this is wrong! Change to this:

               for (int i = 0; i < 3; i++) {
                    System.out.println( " Guess a number from 0 to 10 :");
                    String input1 = in.readLine();
                    guessNum = Integer.parseInt(input1);
                    if (guessNum == num) {
                        System.out.println( " Congratulations !");
                        ans = false;
                        rand = true;
                    } else {
                        System.out.println( " Try again");

                    }


                }
0

Check that if(ans = true) { is an assigment and not a check condition

if you want to check the value of ans do

if(ans == true) {

OR EVEN BETTER

if(ans) {

the other reason why is not working is because this:

for (int i = 0; i > 3; i++) {

the condition is never met

do instead

for (int i = 0; i < 3; i++) {
3
  • I am sorry but I dont get what you mean? I am a noob :/
    – ugh
    Commented Mar 19, 2016 at 21:34
  • 1
    This isn't the only problem Commented Mar 19, 2016 at 21:35
  • 1
    If you write this: ans == true you are asking "if ans value is true", but when you do ans = true you are setting the var ans to true.... Commented Mar 19, 2016 at 21:35
0

Flip your comparison operator:

for (int i = 0; i < 3; i++) {
1
  • Thanks. wow I didnt notic that lol
    – ugh
    Commented Mar 19, 2016 at 21:36

Not the answer you're looking for? Browse other questions tagged or ask your own question.