循环错误时该如何解决?

时间:2018-11-09 22:36:58

标签: java loops switch-statement project do-while

我正在由我的大学的计算机科学教授主持的一个项目上工作。我没有他,但是我想尝试一下会很好。该项目的目标是完成一个符合所有条件的成功的书籍和CD租赁“商店”。

现在,我的问题位于我应该为用户提供一种修改现有客户信息的方法的部分。在上面定义的ModifyCustomer方法中,我有很多嵌套循环。一切工作都进行得很顺利,直到我必须对整个方法实施do while循环,以便用户可以根据需要再次更改现有的客户信息。只有两个客户。每当我多次修改客户1时,我所有的switch语句都能正常工作。但是,每当我多次修改客户2次时,即使提示编译器修改客户信息的某个方面,编译器也会决定跳到最底层。我只能为客户2编辑地址2,但是我不能提示编译器编辑客户2的名称或电话号码,除非它跳到do while循环的底部,而又不允许我更改任何内容。这是很多代码,因此,如果有人想复制并粘贴到自己的IDE中并帮助我弄清楚我做错了什么,那将不胜感激!

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class ShopWithMethods {

public static void main (String[] Args) {
    Scanner scan1 = new Scanner(System.in);
    String name = "n/a";
    String date1 = "n/a";
    String address = "n/a";
    String phone = "n/a";
    String decide = "n/a";
    String rentalRecord = "No books or CDs rented yet.";
    System.out.println("Welcome to Eli's bookshop and CD rental shop!");
    Addcustomer(name, address, phone, date1, rentalRecord);

}

private static void Addcustomer(String name, String address, String phone, String date1, String rentalRecord) {
    Scanner scan1 = new Scanner(System.in);
    String decide;
    loop:
        while (true) {
        System.out.println("Would you like to create a customer profile with us? (Enter y or n):");
        decide = scan1.next();

        switch (decide) {

        case "y" : {
            System.out.println("Enter your name: ");
            scan1.nextLine();
            name = scan1.nextLine();
            System.out.println("Enter your address: ");
            address = scan1.nextLine();
            break loop;

        }


        case "n" : {
            System.out.print("Okay, please visit us another time.");
            break loop;
        }

        default : {
            System.out.print("(Try again)  ");
        }

    }
            }

        loop:
            while (true) {
                System.out.println("Enter your phone number(0000000000): ");
                phone = scan1.nextLine();
                if (phone.length() != 10) {
                    continue loop;
                }


                break loop;

            }

        loop:
            while(true) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter the Date(mm-dd-yyyy): ");

        date1 = scanner.next();

        SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");
        Date date2=null;
            try {

        date2 = dateFormat.parse(date1);
        } 
            catch (ParseException e) {

                e.printStackTrace();
                continue loop;
            }

            System.out.println("The date you've entered is: " + date2);
            break loop; 
            }



            String [][] CustomerProfile1 = {
                {"Name:", name},
                {"Address:", address},
                {"Phone Number:", phone},
                {"Enrollment Date:", date1},
                {"Rental Record:", rentalRecord}
        };

            System.out.println();
            loop:
                while(true) {
                    System.out.print("Adding a second customer, press c to continue: ");
                    decide = scan1.next();

            switch (decide.toLowerCase()) {

            case "c": {
                System.out.println("Enter your name: ");
                scan1.nextLine();
                name = scan1.nextLine();
                System.out.println("Enter your address: ");
                address = scan1.nextLine();
                break loop;

            }


            default : {
                System.out.print("(Try again)  ");
            }

        }
                }

            if (decide.equals("c")) {
            loop:
                while (true) {
                    System.out.println("Enter your phone number(0000000000): ");
                    phone = scan1.nextLine();
                    if (phone.length() != 10) {
                        continue loop;
                    }


                    break loop;

                }
            }

            if (decide.equals("c")) {
            loop:
                while(true) {
            Scanner scanner = new Scanner(System.in);
            System.out.println("Enter the Date(mm-dd-yyyy): ");

            date1 = scanner.next();

            SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");
            Date date2=null;
                try {

            date2 = dateFormat.parse(date1);
            } 
                catch (ParseException e) {

                    e.printStackTrace();
                    continue loop;
                }

                System.out.println("The date you've entered is: " + date2);
                break loop; 
                }
            }

                System.out.println();
                if (decide.equals("c")) {
                    String [][] CustomerProfile2 = {
                        {"Name:", name},
                            {"Address:", address},
                            {"Phone Number:", phone},
                            {"Enrollment Date:", date1},
                            {"Rental Record:", rentalRecord}
                    };

                    System.out.println("Customer profile 1: ");
                    for (String i []: CustomerProfile1) {
                        System.out.println();
                        for (String print1: i) {
                            System.out.print(print1 + " ");

                        }
                    }

                    System.out.println();
                    System.out.println();
                    System.out.println("Customer profile 2: ");
                    for (String j []: CustomerProfile2) {
                        System.out.println();
                        for (String print2: j) {
                            System.out.print(print2 + " ");

                        }
                    }

                }


                String [][] CustomerProfile2 = {
                        {"Name:", name},
                            {"Address:", address},
                            {"Phone Number:", phone},
                            {"Enrollment Date:", date1},
                            {"Rental Record:", rentalRecord}
                    };

                System.out.println();

                ModifyCustomer(CustomerProfile1, CustomerProfile2, name, address, phone, date1, rentalRecord);

}

private static void ModifyCustomer(String[][] customerProfile1, String[][] customerProfile2, String name,
        String address, String phone, String date1, String rentalRecord) {

    System.out.println();
    String decide;
    String change;
    String repeat;
    int modify;

    Scanner scan1 = new Scanner(System.in);

    do {
    loop:
        while(true) {
            System.out.println("Would you like to modify a customer? (Enter y or n): ");
            decide = scan1.next();

            switch (decide.toLowerCase()) {

            case "y" : {
                System.out.println();
                break loop;
            }
            case "n" : {
                System.out.println();
                break loop;
            }
            }
        }

    loop:
        while(true) {
    if (decide.equals("y")) {
        System.out.println("Would you like to modify customer 1 or customer 2? ");
        modify = scan1.nextInt();
        switch (modify) {
        case 1:{
            System.out.println();
            break loop;
        }

        case 2: {
            System.out.println();
            break loop;
        }


        }
    }
        }


            loop:
                while(true) {
                    System.out.print("Would you like to modify the name, address, or phone?");
                    change = scan1.next();
            switch(change) {

            case "name": {
                if (modify == 1) {
                    System.out.print("Modify customer 1's name: ");
                    scan1.nextLine();
                    customerProfile1[0][1] = scan1.nextLine();
                    System.out.println("Customer 1's updated profile: ");
                    for (String [] i: customerProfile1) {
                        System.out.println();
                        for (String printData1: i) {
                            System.out.print(printData1 + " ");
                        }
                    }
                if (modify == 2) {
                    System.out.print("Modify customer 2's name: ");
                    scan1.nextLine();
                    customerProfile2[0][1] = scan1.next();
                    System.out.println("Customer 2's updated profile: ");
                    for (String [] j: customerProfile2) {
                        System.out.println();
                        for (String printData2: j) {
                            System.out.print(printData2 + " ");
                        }
                    }
                }
            }
                break loop;
            }

            case "address": {
                if (modify == 1) {
                    System.out.print("Modify customer 1's address: ");
                    scan1.nextLine();
                    customerProfile1[1][1] = scan1.nextLine();
                    System.out.println("Customer 1's updated profile: ");
                    for (String [] i: customerProfile1) {
                        System.out.println();
                        for (String printData1: i) {
                            System.out.print(printData1 + " ");
                        }
                    }
                }

                if (modify == 2) {
                    System.out.print("Modify customer 2's addresss: ");
                    scan1.nextLine();
                    customerProfile2[1][1] = scan1.nextLine();
                    System.out.println("Customer 2's updated profile: ");
                    for (String [] j: customerProfile2) {
                        System.out.println();
                        for (String printData2: j) {
                            System.out.print(printData2 + " ");
                        }
                    }
                }
                break loop;
            }

            case "phone": {
                if (modify == 1) {
                    do {
                        System.out.print("Modify customer 1's phone number: ");
                        scan1.nextLine();
                        phone = scan1.nextLine();
                        customerProfile1[2][1] = phone;
                        System.out.println("Customer 1's updated profile: ");
                        for (String [] i: customerProfile1) {
                            System.out.println();
                            for (String printData1: i) {
                                System.out.print(printData1 + " ");
                            }
                        }
                    }
                    while (phone.length() != 10);
                    if (phone.length() == 10) {
                        break loop;
                    }

                    if (modify == 2) {
                        do {
                            System.out.print("Modify customer 2's phone number: ");
                            scan1.nextLine();
                            phone = scan1.nextLine();
                            customerProfile2[2][1] = phone;
                            System.out.println("Customer 2's updated profile: ");
                            for (String [] j: customerProfile2) {
                                System.out.println();
                                for (String printData2: j) {
                                    System.out.print(printData2 + " ");
                                }
                            }
                        }
                        while (phone.length() != 10);
                        if (phone.length() == 10) {
                            break loop;
                        }
                    }

                }
                break loop;
            }
}






}


        System.out.print("Would you like to modify anything else? (Enter y or n): ");
        repeat = scan1.next();
        if (repeat.equals("n")) {
            break;
        }
        else 
            continue;

    }
    while (repeat.equals("y"));



}
}

0 个答案:

没有答案