java - 如何打破和继续?
问题描述
我想在 的停车位mediumHandi
用完时停止我的代码执行,并通过将选项提供给其他停车位继续。但是当我使用时continue
,它不起作用。
有人可以向我解释一下吗,非常感谢?
这是我的代码
import java.util.Scanner;
public class challenge3 {
private static int small = 2;
private static int medium = 2;
private static int mediumHandi = 2;
private static int large = 2;
private static int p = 8;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
handiCar hc = new handiCar();
rTruck rT = new rTruck();
rCar rC = new rCar();
handiTruck hT = new handiTruck();
bike b = new bike();
String parkagain = "";
do {
System.out.println("Type your vehicle type:" +
"\n" +
"Handicap Car = hc " +
"\n" +
"Regular Truck = rT " +
"\n" +
"Regular car = rC" +
"\n" +
"Handicap Truck hT" +
"\n" +
"Bike b" +
"\n" +
"or no ");
String s1 = scanner.nextLine();
if (s1.equalsIgnoreCase("hc")) {
mediumHandi--;
p--;
System.out.println("Thank you.");
System.out.println();
if (mediumHandi == 0) {
System.out.println("Sorry, the parking space has run out for handicap car.");
continue;
} else if (mediumHandi == -1) {
System.out.println("Sorry, the parking space has run out for handicap car.");
break;
}
}
else if (s1.equalsIgnoreCase("rT")) {
large--;
p--;
System.out.println("Thank you.");
System.out.println();
if (large == 0) {
System.out.println("Sorry the parking space has run out for regular truck.");
continue;
} else if (large == -1) {
System.out.println("Sorry the parking space has run out for regular truck.");
break;
}
}
else if (s1.equalsIgnoreCase("rC")) {
medium--;
p--;
System.out.println("Thank you.");
System.out.println();
if (medium == 0) {
System.out.println("Sorry the parking space has run out for regular cars.");
continue;
} else if (medium == -1) {
System.out.println("Sorry the parking space has run out for regular cars.");
break;
}
}
else if (s1.equalsIgnoreCase("hT")) {
large--;
p--;
System.out.println("Thank you.");
System.out.println();
if (large == 0) {
System.out.println("Sorry the parking space has run out for handicap truck.");
continue;
} else if (large == -1) {
System.out.println("Sorry the parking space has run out for handicap truck.");
break;
}
}
else if (s1.equalsIgnoreCase("b")) {
small--;
p--;
System.out.println("Thank you.");
System.out.println();
if (small == 0) {
System.out.println("Sorry the parking space has run out for bike.");
continue;
} else if (small == -1) {
System.out.println("Sorry the parking space has run out for bike.");
break;
}
}
System.out.println("Number of small parking " + small);
System.out.println();
System.out.println("Number of large parking " + large);
System.out.println();
System.out.println("Number of medium parking "+medium);
System.out.println();
System.out.println("Number of Handicap medium " + mediumHandi);
System.out.println();
System.out.println("Total number of parking space left: " + p);
System.out.println();
if (p == 0) {
System.out.println("Sorry all the parking space run out!");
break;
}
parkagain = s1;
}while (
(parkagain.equalsIgnoreCase("rT")||
parkagain.equalsIgnoreCase("hc")||
parkagain.equalsIgnoreCase("rC")||
parkagain.equalsIgnoreCase("hT")||
parkagain.equalsIgnoreCase("b")
)
);
System.out.println("Thank you!");
scanner.close();
}
public static class handiTruck { }
public static class rTruck { }
public static class rCar { }
public static class handiCar { }
public static class bike { }
}
解决方案
如果您想提供continue
中号手快用完的选项,则不需要break
条件。使用condition
应该足够了。修改 if 块的代码hc
,您应该能够实现这一点
if (s1.equalsIgnoreCase("hc")) {
if (mediumHandi == 0) {
System.out.println("Sorry, the parking space has run out for handicap car.");
continue;
}
mediumHandi--;
p--;
System.out.println("Thank you.");
System.out.println();
}
您需要在其他情况下应用相同的逻辑,例如普通卡车案例
推荐阅读
- javascript - jspdf 和 html2canvas:未正确显示选定的单选按钮
- amazon-web-services - 在 AWS 账户之间切换
- minecraft - CraftTweaker (Minecraft 1.16): 损坏和重用以及制作后的物品
- android - 为什么我不能在“BoxScope”中使用“AnimatedVisibility”?
- python - 在 os.system 之后 python 脚本不会继续
- spring - 如何将 webclient 响应转换为 ResponseEntity?
- django - 在 Django 中验证之前,是否可以从表单集中获取数据作为字典列表?
- python - 在我出于无知而将其卸载后尝试在centos 7上安装python 2.7.5
- javascript - Peerjs 流重复(发送两次或更多次),有时不显示任何流
- html - 图像标签和段落标签之间的固定距离