java - 循环错误时如何解决此问题?
问题描述
我正在从事一个由我大学的计算机科学教授提供的项目。我没有他,但我认为尝试一下会很好。该项目的目标是成功完成符合所有标准的书籍和 CD 租赁“商店”。
现在我的问题位于我应该为用户提供修改现有客户信息的方法的部分。在 ModifyCustomer 方法中,我已经定义了很多嵌套循环。一切都很顺利,直到我不得不在整个方法上实现一个 do while 循环,以便用户可以在需要时再次更改现有的客户信息。只有两个顾客。每当我多次修改客户 1 时,我的所有 switch 语句都可以正常工作。但是,每当我多次修改客户 2 时,即使在提示编译器修改客户信息的某个方面时,编译器也会决定跳到底部。我可以为客户 2 编辑的唯一内容是它的地址,但我无法提示编译器编辑客户 2' s 的姓名或电话号码,而不会跳到 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"));
}
}
解决方案
推荐阅读
- sql - 从 2 个表的总和计算的 SQL
- laravel - Carbon\Carbon 类的对象无法转换为数字
- c - 我可以在不知道它的大小的情况下迭代动态数组吗
- singularity-container - 运行加密奇点容器的问题
- git - 来自 dev.azure.com 的 git clone(使用 ssh)突然要求输入密码
- ajax - 对 Laravel 后端的 AJAX 请求返回 419 CSRF 令牌不匹配
- php - 我们如何在 shopify 中的单个 API 调用中获取多个集合的产品数据?
- javascript - 响应子组件状态的变化
- python - 导入numpy时如何修复RuntimeError?
- javascript - 意外的标记 。在 Chrome 控制台上