首页 > 解决方案 > 如何打破和继续?

问题描述

我想在 的停车位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 { }

}

标签: java

解决方案


如果您想提供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();
        }

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


推荐阅读