gpt4 book ai didi

java - 如何打破和继续?

转载 作者:行者123 更新时间:2023-12-04 10:49:26 24 4
gpt4 key购买 nike

我想停止我的代码执行,当 mediumHandi 的 parking 位时已用完,并继续提供其他 parking 位的选择权。但是当我使用 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 的选项万一中等handi用完,你不需要break健康)状况。使用 condition应该足够了。修改 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();
}

您还需要在其他情况下应用相同的逻辑,例如普通卡车案例

关于java - 如何打破和继续?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59550263/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com