代码不等待用户输入? [英] Code doesn't wait for user input?

查看:148
本文介绍了代码不等待用户输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须为我的计算机科学课做一个项目。问题是:

I have to do a project for my Computer Science class. The problem is:

图书馆的顾客最多可以借三本书。因此,赞助人有一个名字和最多三本书。一本书有作者和标题。设计并实现两个类,Patron和Book,来表示这些对象和以下行为:

Patrons of a library can borrow up to three books. A patron, therefore, has a name and up to three books. A book has an author and a title. Design and implement two classes, Patron and Book, to represent these objects and the following behavior:


  • 客户端可以实例化带有标题的书籍和作者

  • 客户可以检查但不能修改书籍的标题或作者

  • 客户可以向顾客询问是否借了一本书(通过标题确定。

  • 客户可以告诉顾客返回给定的书(由标题标识)。

  • 客户可以告诉顾客借用给定的书。

  • The client can instantiate a book with a title and author
  • The client can examine but not modify a book's title or author
  • The client can ask a patron wthere it has borrowed a given book (identified by title).
  • The client can tell a patron to return a given book (identified by title).
  • The client can tell a patron to borrow a given book.

Patron类应该为每本书使用一个单独的实例变量(总共三个)。
这些变量中的每一个最初都是null。当借书时,顾客会寻找一个非空的变量。如果未找到此类变量,则该方法返回false。如果找到null变量,则将其重置为新书,并且该方法返回true。类似的考虑适用于其他方法。使用方法aString.equals(aString)比较两个字符串是否相等。请确保为您的类包含适当的toString方法,并使用测试程序对其进行测试。

The Patron class should use a seperate instance variable for each book (a total of three). Each of these variables is initially null. When a book is borrowed, the patron looks for a variable that is not null. If no such variable is found, the method returns false. If a null variable is found, it is reset to the new book and the method returns true. Similar considerations apply to other methods. Use the method aString.equals(aString) to compare two strings for equality. Be sure to include appropriate toString methods for your classes and test them with a tester program.

这是我的客户端 class,包含 main 方法: http://pastebin.com/ JpxCT2F6

Here is my Client class, which contains the main method: http://pastebin.com/JpxCT2F6

现在我的问题是,当我运行程序时,程序不会等待用户输入。以下是Eclipse控制台中的内容:

Now my problem is that when I run the program, the program doesn't wait for user input. Here is what comes up in the console of Eclipse:

Please enter title of book 1: 
s
Please enter author of book 1: 
e
Please enter title of book 2: 
f
Please enter author of book 2:
t
Please enter title of book 3: 
g
Please enter author of book 3:
d
Which book would you like to check for?
s
The patron has taken out the book s
Would you like to return a book? (1 yes or 2 no)
1
Which book would you like to return?
Sorry, could not find the book 
Would you like to take out a book? (1 yes or 2 no)
2
Invalid option
Which book would you like to check for?
The patron does not have  taken out
Would you like to return a book? (1 yes or 2 no)

驴你可以看到,控制台不等待用户输入在你要回哪本书?之后相反,它需要一个空白值。后来在代码中,我输入2,这意味着不返回任何书,而是给我一个无效的输入输出。

Ass you can see, the console doesn't wait for user input after "Which book would you like return?" Instead, it takes a blank value. And later in the code, i put in "2", which means to return no book, but instead gives me an invalid input output.

推荐答案

您在代码的第71行使用 nextInt(),它获取用户提供的整数答案。然后你使用 nextLine()哪个使此扫描程序超过当前行并返回跳过的输入。。跳过的输入只是前一个 nextInt()调用中的换行符(它不会只读取int的整行)。

You use nextInt() on line 71 of your code, which gets the integer answer the user provides. Then you use nextLine() which Advances this scanner past the current line and returns the input that was skipped.. The input that is skipped is only the newline character from the previous nextInt() call (It doesn't read the whole line only the int).

您可以通过在输入之前调用 input.nextLine()跳过此操作,或者使用 nextLine()而不是 nextInt()并将字符串转换为整数值。

You can skip this by calling input.nextLine() once before you want the input, or by using nextLine() instead of nextInt() and converting the string to the integer value.

这篇关于代码不等待用户输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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