如何更改代码以使程序在用户输入否之后跳过选项?
我希望该程序跳过“输入另一个?”?当提示“明确”后输入n时。我该怎么做? 现在,当输入n时,它仍然会提示“长距离?”
以下是当前一批代码。 上下文:这是代码的数据输入部分,其中用户输入被添加到arraylist。
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"));
I want the program to skip to "Enter another?" when N is entered after being prompted "Express?". How can I do that?
Now, when N is entered, it still prompts "Long distance?"
Below is the current batch of code. Context: this is the data entry part of the code where user input gets added to an arraylist.
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"));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在接受' express包裹'的输入后,使用' if-else '条件
,如果用户输入n或n,则会要求下一个迭代跳过包裹代码距离。
Use 'if-else' conditioning after accepting the input for 'Express Parcel'
This way if user enter N or n it'll ask for next iteration skipping the code for Parcel distance.
尝试以下代码:
如果检查用户已输入y,则需要添加额外的代码
Try the below code :
you need to add an extra if to check the user has entered Y or not