为什么我的while循环条件不能正常运行?

时间:2020-07-04 14:58:10

标签: java arrays data-structures

我的代码有一些问题。

在输入完整名称后,它立即进入性别循环。 我的while循环中的性别无效。它不是在阅读我设置的条件。 它不会在我的菜单中读取hasNext或hasNextLine,因为它会立即读取nextInt。 它不会在我的ob中读取hasNext或hasNextLine,因为它会立即读取nextDouble。

import java.util.Scanner;
import java.time.LocalDate;
import java.text.SimpleDateFormat;
import java.util.Date;

public class BankAccountSystem extends Bank
{
    public static void main(String[] args)
    {
        Scanner ba = new Scanner(System.in);
        Bank BankAccount = new Bank();
        int menu = 0;
        double ob = 0;
        String name, address;
        String gender = "";
        LocalDate dateToday = LocalDate.now();
        Date date = new Date();

        System.out.print("\f");
        do {
            System.out.println("\nWhat are you looking for?"
            +"\n1.) New Accounts"
            +"\n2.) Deposit"
            +"\n3.) Account Inquiry"
            +"\n4.) Account Inquiry by Date"
            +"\n5.) Inquiry by Status"
            +"\n6.) Show All Accounts"  
            +"\n[Enter a num 1-6]");
            
            menu = ba.nextInt();
            switch (menu)
            {
                case 1: System.out.print("Enter complete name: ");
                while (ba.hasNextInt())
                {
                    System.out.print("Enter a valid complete name: ");
                    ba.next();
                }
                name = ba.nextLine();
                
                System.out.print("Enter gender [M-Male or F-Female]: ");
                ba.nextLine();
                while (!gender.equals("M") && !gender.equals("F") || (ba.hasNextInt()))
                {
                    System.out.print("Wrong input. Try again."
                    + "\nEnter gender [M-Male or F-Female]: ");
                    ba.next();
                }
                gender = ba.next();
                
                System.out.print("Enter Address: ");
                ba.next();
                address = ba.nextLine();
                
                System.out.print("Enter opening balance [Min: 500]: ");
                while (ob < 500 && !ba.hasNextDouble())
                {
                    System.out.print("Opening balance must be at least 500: ");
                    ba.next();
                }
                ob = ba.nextDouble();
                
                SimpleDateFormat timeNow = new SimpleDateFormat("hh:mm:ss a");
                System.out.println("\nAccount created successfully!"
                                    +"\nBank Account Number: " + BankAccount.accountNum(111,999)
                                    +"\nDate Opened: " + dateToday + " " + timeNow.format(date));
                break;
            }
        }while(menu != 0);
    }
}

0 个答案:

没有答案