-1

为什么当我运行我的程序并输入 5 时,它允许我输入我的记录,但是当主菜单再次运行并输入 6 时,changePhoneNumber 方法没有运行并返回到主菜单。while(true) 循环是否以某种方式把事情搞砸了?我有一个名为 Record 的类,它看起来像:

public static void main(String[] args) {
    BankMethods method = new BankMethods();
    Scanner input = new Scanner(System.in);
    int optionSelected = 0;

    while(true){

    System.out.println("5. Add a New Record");
    System.out.println("6. Change the Phone Number in the Current Record");

    optionSelected = input.nextInt();

if (optionSelected == 5){
        Scanner getRecord = new Scanner(System.in);
        System.out.println("Enter First Name: ");
        String firstName = getRecord.nextLine();
        System.out.println("Enter Last Name: ");
        String lastName = getRecord.nextLine();
        System.out.println("Enter Phone Number: ");
        String phoneNumber = getRecord.nextLine();

        method.addNewRecord(firstName, lastName, phoneNumber);


    }
    if (optionSelected == 6){
        System.out.println("What would you like to change your phone "
                + "number to? ");
        String newNumber = input.nextLine();
        method.changePhoneNumber(newNumber);

    }

和其他类...BankMethods:

public class BankMethods {
LinkedList recordInformation = new LinkedList();
Bankdata mainMenu = new Bankdata();


public void addNewRecord(String firstName, String lastName, 
        String phoneNumber){
    recordInformation.add(firstName); recordInformation.add(lastName);
    recordInformation.add(phoneNumber);
}

public void changePhoneNumber(String newNumber){
    recordInformation.set(2, newNumber);
    System.out.println(recordInformation);
}
4

2 回答 2

4

问题是您使用 2Scanners来读取 1 InputStream。当您打开第二个时,Scanner您将无法使用原始的阅读,因为第二个将拥有对它的独占访问权限。

对于此应用程序,您可以轻松使用单个Scanner.

请参阅:不要在单个 InputStream 上创建多个缓冲包装器

于 2013-03-12T03:06:50.520 回答
1

正确的方法是对输入流使用一个读取(扫描器)。编辑了以前的答案以使用单读选项下面给出了完整的程序

package com.stackoverflow.framework;

import java.util.LinkedList;
import java.util.Scanner;

public class Record {

    static Scanner input = new Scanner(System.in);

    public static String readData() {
        return (input.nextLine());
    }

    public static void main(String[] args) {
        BankMethods method = new BankMethods();

        int optionSelected = 0;

        while (true) {

            System.out.println("5. Add a New Record");
            System.out
                    .println("6. Change the Phone Number in the Current Record");

            optionSelected = Integer.parseInt(readData());

            if (optionSelected == 5) {
                // Scanner getRecord = new Scanner(System.in);
                System.out.println("Enter First Name: ");
                String firstName = readData();
                System.out.println("Enter Last Name: ");
                String lastName = readData();
                System.out.println("Enter Phone Number: ");
                String phoneNumber = readData();
                method.addNewRecord(firstName, lastName, phoneNumber);

            }
            if (optionSelected == 6) {
                System.out.println("What would you like to change your phone "
                        + "number to? ");
                // Scanner getRecord = new Scanner(System.in);
                String newNumber = readData();
                method.changePhoneNumber(newNumber);

            }
        }
    }
}

class BankMethods {
    LinkedList recordInformation = new LinkedList();

    public void addNewRecord(String firstName, String lastName,
            String phoneNumber) {
        recordInformation.add(firstName);
        recordInformation.add(lastName);
        recordInformation.add(phoneNumber);
    }

    public void changePhoneNumber(String newNumber) {
        recordInformation.set(2, newNumber);
        System.out.println(recordInformation);
    }
}
于 2013-03-12T03:06:28.717 回答