我做了这个程序,它打印出一件事 [英] I made this program and it prints one extra thing

查看:78
本文介绍了我做了这个程序,它打印出一件事的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

程序使用主菜单中的while循环菜单来请求用户命令:

Program uses a while loop menu in the main to request for the user command:

public static void main(String[] args)throws Exception
{
    Boolean meow = true;

    while(meow)
    {
        System.out.println("\n  1. Show all records.\n"
            + "  2. Delete the current record.\n"
            + "  3. Change the first name in the current record.\n"
            + "  4. Change the last name in the current record.\n"
            + "  5. Add a new record.\n"
            + "  6. Change the phone number in the current record.\n"
            + "  7. Add a deposit to the current balance in the current record.\n"
            + "  8. Make a withdrawal from the current record if sufficient funds are available.\n"
            + "  9. Select a record from the record list to become the current record.\n"
            + " 10. Quit.\n");
        System.out.println("Enter a command from the list above (q to quit): ");
        answer = scan.nextLine();
        cmd.command(answer);
        if(answer.equalsIgnoreCase("10") || answer.equalsIgnoreCase("q"))
        {
            meow = false;
        }

    }
}

如果您选择的命令实际上都不是菜单上的命令,则会发生这种情况:

If none of the commands you pick are actually commands on the menu then this happens:

else
        {
            System.out.println("Illegal command");
            System.out.println("Enter a command from the list above (q to quit): ");
            answer = scan.nextLine();
            command(answer);
        }

每当我添加一个新人或使用任何需要我按回车键以完成输入值的命令时,我都会得到else语句和常规命令请求.

Whenever I add a new person or use any command that requires me to press return to finish entering a value I get the else statement and then the regular command request.

它看起来像:

Enter a command from the list above (q to quit): 
Illegal command
Enter a command from the list above (q to quit): 

发生这种情况时.

不想在这里发布我的完整代码,我担心它会因为太多了.取而代之的是它们的糊盒.

Not gonna post my full code on here, I'm afraid of it cause it's so much. Have the pastebins of them instead.

  • My full Main Class: http://pastebin.com/rUuKtpXb
  • My full not Main Class: http://pastebin.com/UE4H76Cd

有人知道为什么会这样吗?

Anyone know why this is happening?

推荐答案

问题在于Scanner::nextDouble之类的内容不会读取换行符,因此下一个Scanner::nextLine返回空行.

The problem is that something like Scanner::nextDouble doesn't read the new-line character, so the next Scanner::nextLine returns an empty line.

将所有出现的Scanner::nextLine替换为Scanner::next应该可以解决此问题.

Replacing all occurrences of Scanner::nextLine with Scanner::next should fix it.

您也可以在最后一个非nextLine next方法之后执行Scanner::nextLine,但这有点混乱.

You can also do a Scanner::nextLine after your last non-nextLine next method, but this is a bit messy.

我还建议其他一些事情:

Some other things I'd recommend:

  1. 在程序的开头添加scan.useDelimiter("\n");,在行中添加空格进行测试,然后您将了解为什么需要这样做.

  1. Add scan.useDelimiter("\n"); at the beginning of your program, test with adding spaces to a line and you'll see why this is needed.

println更改为print,因此可以在同一行上输入命令.即:

Change println to print, so the command can be entered on the same line. i.e.:

更改

System.out.println("Enter a command from the list above (q to quit): ");`

System.out.print("Enter a command from the list above (q to quit): ");

  • 更改此:

  • Change this:

    else
    {
        System.out.println("Illegal command");
        System.out.println("Enter a command from the list above (q to quit): ");
        answer = scan.nextLine();
        command(answer);
    }
    

    收件人:

    else System.out.println("Illegal command");
    

    您将再次打印菜单,但可以避免不必要的递归.这样很容易避免再次打印菜单.

    You would print the menu again, but you would avoid unneeded recursion. It would be easy enough to avoid printing the menu again.

    最好在运行command之前先检查退出 (然后您可以在command中删除该检查).

    It would be better to check for exit before running command (and then you can remove that check in command).

    System.out.println("Enter a command from the list above (q to quit): ");
    answer = scan.nextLine();
    if (answer.equalsIgnoreCase("10") || answer.equalsIgnoreCase("q"))
        meow = false;
    else
        cmd.command(answer);
    

  • Boolean更改为boolean. Booleanboolean的包装器类,在这种情况下是不需要的.

  • Change Boolean to boolean. Boolean is the wrapper class for boolean, which is unneeded in this case.

    这篇关于我做了这个程序,它打印出一件事的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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