我想让程序跳转到"输入另一个?"提示"快递?"后输入N。我该怎么做呢?现在,当输入N时,仍然提示&;Long distance?&;
下面是当前批次的代码。上下文:这是代码的数据输入部分,用户输入被添加到数组列表中。
String input=null;
//data entry (user input + adding to arraylist)
do {
System.out.println("Enter parcel code: ");
String pCode=kboard.next();
System.out.println("Enter parcel length: ");
double pLength=kboard.nextDouble();
System.out.println("Enter parcel width: ");
double pWidth=kboard.nextDouble();
System.out.println("Enter parcel height: ");
double pHeight=kboard.nextDouble();
System.out.println("Enter parcel weight: ");
double pWeight=kboard.nextDouble();
Parcel parcel=new Parcel(pCode, pLength, pWidth, pHeight, pWeight);
List.add(parcel);
System.out.println("Express parcel? Y/N: ");
String expressP=kboard.nextLine();
input=kboard.next();
System.out.println("Long distance parcel? Y/N: ");
String longDist=kboard.nextLine();
input=kboard.next();
System.out.println("Do you want to enter another parcel record? Y/N: ");
input=kboard.next();
}
while (input.toLowerCase().equals("y"));
使用'if-else快递包裹
System.out.println("Express parcel? Y/N: ");
String expressP=kboard.nextLine();
input=kboard.next();
if(ip.equalsIgnoreCase("Y"). //This will execute only if previous resp is Yes
{
System.out.println("Long distance parcel? Y/N: ");
String longDist=kboard.nextLine();
input=kboard.next();
}
System.out.println("Do you want to enter another parcel record? Y/N: ");
input=kboard.next();
这样,如果用户输入N或N,它将要求下一次迭代跳过包裹距离的代码。
试试下面的代码:
String input=null;
//data entry (user input + adding to arraylist)
do {
System.out.println("Enter parcel code: ");
String pCode=kboard.next();
System.out.println("Enter parcel length: ");
double pLength=kboard.nextDouble();
System.out.println("Enter parcel width: ");
double pWidth=kboard.nextDouble();
System.out.println("Enter parcel height: ");
double pHeight=kboard.nextDouble();
System.out.println("Enter parcel weight: ");
double pWeight=kboard.nextDouble();
/*Parcel parcel=new Parcel(pCode, pLength, pWidth, pHeight, pWeight);
List.add(parcel);*/
System.out.println("Express parcel? Y/N: ");
String expressP=kboard.next();
if (expressP.equalsIgnoreCase("Y")) {
System.out.println("Long distance parcel? Y/N: ");
String longDist = kboard.nextLine();
input = kboard.next();
}
System.out.println("Do you want to enter another parcel record? Y/N: ");
input=kboard.next();
}
while (input.toLowerCase().equals("y"));
您需要添加一个额外的if来检查用户是否输入了Y