Java 如何中断并继续?
当Java 如何中断并继续?,java,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;
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 { }
}
可能您需要取消清除Case因为Break语句在Case下工作。如果您想在中把手用完时提供
continue
选项,则不需要Break
条件。使用条件
就足够了。修改hc
的if块的代码,您应该能够实现这一点
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();
}
您还需要在其他情况下应用相同的逻辑,例如普通卡车情况下使用
continue
,您将跳转到循环的下一次迭代。如果我正确理解你的问题,那么现在你实际上没有给出另一个停车位的选择。您可以在结尾使用continue
,但也许您应该享受其他功能,并在结尾使用continue
。例如:
if (mediumHandi == 0) {
//enter some function that suggests a different parking space and then puts that car into that different parking space
continue;
}
断路器
不限于开关箱。