-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