我的代码不断跳过一行!请帮忙! T_T [英] My code keeps skipping over a line! Please please please help! T_T

查看:61
本文介绍了我的代码不断跳过一行!请帮忙! T_T的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以实际问题非常冗长,所以我只会提供你需要知道的信息:



1.我是在墙上发射弹丸。

2.每次发射后,用户必须能够选择是再次弹奏还是退出。





我使用的变量(你需要知道):

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):

boolean GameOn
int angle;
int speed;
String choice;



您也应该知道我的扫描仪名为`sc` < br $> b $ b



所以这就是发生的事情(阅读评论):


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


So this is what's happening (read the comments):

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

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!

推荐答案

Quote:

For某些原因,而不是程序等待用户输入他们的选择,我的程序创建了一个新的墙!

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.

angle = sc.nextInt();
speed = sc.nextInt();



当您的程序读取 angle speed 作为整数,扫描器只读取所需内容

因此,行尾或任何键入的内容都保留在缓冲区中,等待消耗。


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

choice = sc.nextLine();



添加 sc.nextLine()阅读速度后应解决问题。



如果您使用调试器,您将看到 choice 正在获得一些东西。


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.


这篇关于我的代码不断跳过一行!请帮忙! T_T的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆