Click here to Skip to main content
16,021,041 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Okay, so the actual question is really lengthy, so I'm only going to give the information that you need to know:

1. I'm launching a projectile over a wall.
2. After each launch, the user has to be able to choose whether to play again or quit.


The variables I use (that you need to know):
Java
boolean GameOn
int angle;
int speed;
String choice;

You should also probably know that my scanner is called `sc`


So this is what's happening (read the comments):
Java
while (GameOn = true) 

    //Computer sets height and distance
    Wall myWall = new Wall();
    System.out.println(myWall.toString());

    //User sets angle and speed
    System.out.print("Thus, enter this angle and speed.");
    angle = sc.nextInt();
    speed = sc.nextInt();

    //Once the user clicks enter, they have launched their projectile.
    //I call a method called "reach" to check if their projectile makes it over the wall.
    //I tell the user if they did good or not
    //Then ask them to choose whether to guess again, pass this round or quit completely.
    if ((myWall.reach(angle, speed) - myWall.getHeight())>= 4) {
        score+=2;
        System.out.println("You went a little too far over but good job!");
        System.out.println("Do you want to play again or quit?");
        choice = sc.nextLine();
        System.out.println("it passes the choice");
            if (choice == "play again") {
                myWall.setNewWall();
            }
            else if (choice == "quit") {
                GameOn = false;
                break;
            }
        }


And here is where my problem is! For some reason, rather than the program waiting for the user to type in their choice, my program creates a new wall! It just skips right over
Java
choice = sc.nextLine();
for some reason!

Do you know why? Can someone please help? And please tell me if you need me to include my full code. My original code isn't very long but this version is quite fragmented so you only get the main point.

Once again please please please help. I've been stuck on this for the past three days and my project is due soon. Thank you so much if you can help!

What I have tried:

what have I tried? See above for the code which I have tried!
Posted
Updated 2-Oct-18 13:38pm

1 solution

Quote:
For some reason, rather than the program waiting for the user to type in their choice, my program creates a new wall!

The reason is that the keyboard buffer is not empty.
Java
angle = sc.nextInt();
speed = sc.nextInt();

When your program read angle and speed as integers, the scanner reads only what is needed
Thus the end of line or whatever was typed stay in buffer, awaiting to be consumed. Which is done when
Java
choice = sc.nextLine();

Adding a sc.nextLine() after reading the speed should solve the problem.

If you use the debugger, you will see that choice is getting something.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900