Java 作业 - 使用 next() 和 nextLine() 方法的 Scanner 对象

标签 java java.util.scanner

我正在编写一个主要方法,为我为正在使用的 java 类编写的 Contact 类和 ContactBook 类提供菜单。我的问题是,我期望当用户输入 A、F、P 或 Q 时,我的 Scanner 对象 (kbd) 将捕获输入、使用它,并在输入下一个输入后继续前进。显然有一些关键我不理解,因为插入返回并不总是像我预期的那样推进我的计划。我已经包含了我的代码和输出。任何提示将不胜感激。

  import java.util.Scanner;
  public class run{
  public static void main(String[]args){
      Scanner kbd = new Scanner(System.in);
      boolean quit = false;
      System.out.println("How many contacts would you like in your Contact Book?: ");
      int size = kbd.nextInt();
      kbd.nextLine();
      ContactBook kevin = new ContactBook(size);
      while(!quit){
         System.out.println("A - Add a contact \n"+
                            "F - Find a contact \n"+
                            "P - Prints the list \n"+
                            "Q - Quits");


         if(kbd.next().equals("A")){
            if(ContactBook.full(kevin))
               System.out.println("Contact book full!");
            else{
               Contact temp = new Contact();
               System.out.println("Enter a First Name: ");
               temp.setFirstName(kbd.nextLine());
               System.out.println("Enter a Last Name: ");
               temp.setLastName(kbd.nextLine());
               System.out.println("Enter a Phone Number: ");
               temp.setPhoneNumber(kbd.nextLine());
               System.out.println("Enter an email: ");
               temp.setEmail(kbd.nextLine());
               kevin.addContact(temp);
            }
         }
         if(kbd.next().equals("F")){
            kevin.search();
         }
         if(kbd.next().equals("P")){
            System.out.print(kevin.produce());
         }
         if(kbd.next().equals("Q")){
            quit = true;
         }
      }
   }
}

这是我得到的输出。

 ----jGRASP exec: java run
How many contacts would you like in your Contact Book?: 
3
A - Add a contact 
F - Find a contact 
P - Prints the list 
Q - Quits
A
Enter a First Name: 
Kevin
Enter a Last Name: 
Smith
Enter a Phone Number: 
312-4567
Enter an email: 
<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bad1dfccd3d4faddd7dbd3d694d9d5d7" rel="noreferrer noopener nofollow">[email protected]</a>



 //here I keep pushing enter and am not sure why it doesn't continue back to
 //the beginning of my while loop



a
a
a
A - Add a contact 
F - Find a contact 
P - Prints the list 
Q - Quits

a

 ----jGRASP: process ended by user.

 ----jGRASP exec: java run
How many contacts would you like in your Contact Book?: 

 ----jGRASP: process ended by user.

 ----jGRASP exec: java run
How many contacts would you like in your Contact Book?: 
4
A - Add a contact 
F - Find a contact 
P - Prints the list 
Q - Quits
A
Enter a First Name: 
Enter a Last Name: 
Smith
Enter a Phone Number: 
312-4567
Enter an email: 
<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="432826352a2d03242e222a2f6d202c2e" rel="noreferrer noopener nofollow">[email protected]</a>

a
s
d
A - Add a contact 
F - Find a contact 
P - Prints the list 
Q - Quits

再说一遍,我是一名学生,这是我的第二堂 Java 课。我检查了许多资源,试图了解我做错了什么,但我无法将其拼凑起来。希望有人能为我阐明这一点。谢谢。

最佳答案

我将删除对 kbd.next() 的所有调用并替换 kbd.nextLine()。这里没有必要使用 next() ,并且由于它不处理行尾标记,因此它可能会让您感到困惑。如果您绝对需要使用 kbd.next(),请务必在调用 next() 之后调用 kbd.nextLine()让您的程序以合理的方式处理行尾标记。

关于Java 作业 - 使用 next() 和 nextLine() 方法的 Scanner 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13410401/

相关文章:

java - 无法访问 Android 的 Java Keystore

java - 从 Map 中找出给定值的键的更快方法?

java - 如何在 Java 中创建 JSON 数组

java - 读取 CSV 文件时出现的问题让我困惑不已

java - 更新了 util.scanner 重复方法

java - 扫描仪类输入不以分隔符结尾

Java 线程与 Scanner 第一次工作但第二次就不行了?

java - 在 spring boot 端点上使用自定义 jackson 映射器

java - 加快矩阵查找速度

java - Else 语句仅在奇数输入上给出输出