hi all

just a question.

if you are making a if statement in a game and the compile is not reading the condition in the if statement and it jumping out and reading the rest of the codes

what is the cause of that. I am trying to build my game and I have implemented an if statement. this is my if statement.

              if(diff == 'e' || diff == 'E')
         {

               System.out.println("enter diff level");
               diff = keyin.next().charAt(0);
         }

can u tell me if I am doing something wrong here, it only read it if i prompt for it. the game is supposed to if the student want to play either easy, medium or hard.

Recommended Answers

All 8 Replies

Please show the rest of the code in the function that this conditional statement resides.

okay so here is the whole code, what I have so far

 System.out.println("Welcome Students!!!\n " + "To math 1, 2 3!!! " + "Come let us have some fun with the multiplication table and give your brains a workout!!!\n " + "This is an interactive, inexpensive, effective and educational game that is specifically design for primary school studts\n " + "The game will give the students the basic knowledge of the multiplication times math table ad provid them with a solid foundation for future development");
         welcome_msg = keyin.nextLine();

         System.out.println("This game is made up of six levels and each level it is also made up of a number of rouns\n " + "Level easy is mde up of three rouds, while level medium and hard are made up of four rouds each\n " + "In each rounds and in each level, every correct answers your points will increase but will decrease with any incorrect answers\n " + "Each level consist of 12 questions which will be answered correctly in order to advance to the second rounds\n " + "Each students are given a maximum of three chances\n " + "Level 5 states that the students only win the games by answering all 12 questions correctly but fails if the students runs out of chances or tries\n " + "Level six states that each students should win the game if the students that choose either easy, medium or hard and that they should be presented with the option to proceed to the next advanced level");
         game_rules = keyin.nextLine();


              if(diff == 'e' || diff == 'E')
         {

               System.out.println("enter diff level");
               diff = keyin.next().charAt(0);
         }

              System.out.println("please enter the student name");
              stuname = keyin.next();

             System.out.println("please enter the student age");
             stu_age = keyin.nextInt();

             if(numoftries == 1)
             {
                 System.out.println("round 1");
                 rounds = keyin.nextInt();

             }
        }
     }

       it is supposed to say if the student want the easy level medium or hard          

as far as we know, diff doesn't have a value yet.

i have initialize diff to int = diff = 0; and it is still not reading the condition, i believe it jump out of the loop and went to student name and age what do i do to get it to read the diff level. do i prompt for it instead because that is the only way I am seeing that it will read the diff level. what to do

So, if diff is 0 it won't be equal to 'e' or 'E', so the if test (line 8) fails, and you don't issue the prompt.
If you always want to prompt the user, why is that if test there at all?

hi james what would it be equal to then. do i have to use a char, say like the letter e or capital E in order for it to read it. I am thinking about that but i am not sure

char in Java is a 16 bit integer value, used to encode characters in UniCode. So 0 is a prefcetly good value, as is 99, or 'e' because 'e' is a UniCode character whose numeric value is 101.
What you do when you prompt for and read the user input (lines 11,12) is perfectly OK. The mystery is what that if test on line 8 is intended to do.

What James said is 100% correct, but to reiterate, you are not setting the variable diff to some value, assuming input from the user. Hence, it does not match the conditional in your if statement, so it jumps over that block. Fix that and it will probably work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.