Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我做了这个程序,它打印了一个额外的东西_Java_Loops_Printing_Linked List - Fatal编程技术网

Java 我做了这个程序,它打印了一个额外的东西

Java 我做了这个程序,它打印了一个额外的东西,java,loops,printing,linked-list,Java,Loops,Printing,Linked List,程序使用主菜单中的while循环菜单来请求用户命令: 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"

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

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;
        }

    }
}
如果您拾取的命令实际上都不是菜单上的命令,则会发生以下情况:

else
        {
            System.out.println("Illegal command");
            System.out.println("Enter a command from the list above (q to quit): ");
            answer = scan.nextLine();
            command(answer);
        }
每当我添加一个新人或使用任何需要我按return键完成输入值的命令时,我都会得到else语句,然后是常规命令请求

所以它看起来像:

Enter a command from the list above (q to quit): 
Illegal command
Enter a command from the list above (q to quit): 
当这种情况发生时

我不想在这里发布我的全部代码,我很害怕,因为太多了。取而代之的是他们的粘贴箱

  • 我的全班主要课程:
  • 我的全班不是主班:

有人知道为什么会发生这种情况吗?

可能它在while循环结束时以及再次获取输入之前将
\n
留在缓冲区中。 也许吧


这有助于删除它。

问题是类似于
Scanner::nextDouble
的内容无法读取新行字符,因此下一个
Scanner::nextLine
返回一个空行

将所有出现的
Scanner::nextLine
替换为
Scanner::next
应该可以修复它

您还可以在上一个非
nextLine
nextLinenextLine方法之后执行
Scanner::nextLine
,但这有点混乱

我还推荐其他一些东西:

  • 添加
    scan.useDelimiter(“\n”)
    在程序开始时,通过在一行中添加空格进行测试,您将了解为什么需要这样做

  • println
    更改为
    print
    ,以便可以在同一行中输入命令。i、 e:

    改变

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

  • 更改此项:

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

    您可以再次打印菜单,但可以避免不必要的递归。避免再次打印菜单是很容易的

  • 最好在运行
    命令之前检查退出(然后您可以删除该签入
    命令)

  • Boolean
    更改为
    Boolean
    Boolean
    Boolean
    的包装类,在本例中不需要它


  • 尤达问:为什么不调试?我试过了,但我有点累了,急于完成。我希望能有第二双眼睛,让我觉得自己是在谷仓里长大的,谢谢XD@RyanTibbetts没有人一开始是(或者从来没有人是)一个完美的程序员,只是想给你指出我认为正确的方向。
    System.out.print("Enter a command from the list above (q to quit): ");
    
    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");
    
    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);