vendredi 4 septembre 2020

How do i skip a specific part of my code in Java [closed]

How do i skip a certain part of my code? my code has a bunch of if cases and if a certain combination is inputted by the user, i want it to skip 3 questions containing if statements to the last question of the code and do like an end to questions and go straight to asking the user to restart or try again or just end it

Goes like this:

public static void main(String[] args) {
    Scanner in = new Scanner (System.in);
    float cost=0,discount = 0.1f;
    int room,floor,stay;
    String roomtype,redo;
    boolean a=true;
    try{
    while (true){
    while (true){
    System.out.print("Room Type [1=Single, 2=Double]: ");
    room=in.nextInt();
    if ((room<1)||(room>2)){
        System.out.println("Invalid, choose only from 1 or 2");
        }else if (room==1){
            roomtype = "Single Room";
            break;
        }else if (room==2){
            roomtype = "Double Room";
            break;
        }
    }
    while (true){
    System.out.print("Floor [1-12]: ");
    floor=in.nextInt();
    if (floor<1||floor>12){
        System.out.println("Invalid, choose only from 1 to 12");
        }else if (floor<6&&room==1){
            cost = 450; 
            break;
        }else if (floor<6&&room==2){
            cost = 600;
            break;
        }else if (floor<12&&room==1){
            cost = 550;
            break;
        }else if (floor<12&&room==2){
            cost = 720;
            break;
        }else if (floor==12&&room==2){
            cost = 120;
            break;
        }else if (floor==12&&room==1){
        System.out.println("Sorry room is not available.");
            break;
        }}
    while (true){
    System.out.print("Number of Nights: ");
    stay=in.nextInt();
    if (stay<1){
        System.out.println("Invalid, choose number greater than 0");
    }else{
        break;
    }}
    System.out.print("\nNightly Rate: ₱"+cost);
    cost = cost*stay;
    if (stay>3){
        discount = cost*discount;
        cost = cost-discount;
    }if (stay==1){
        System.out.print("\n"+stay+" night ");
    }else if (stay>1){
        System.out.print("\n"+stay+" nights ");
    }
    System.out.println("of stay in a "+roomtype+" at floor "+floor+".\nTotal price for your stay is ₱"+cost);
    while(true){
    System.out.print("Do you want to do another transaction [Y/N]? ");
    redo = in.next();
    if (redo.equalsIgnoreCase("Y")){
        System.out.println("… redo the process\n\n\n");
        break;
    }else if (redo.equalsIgnoreCase("N")){
        System.out.print("Thank you for staying with us");
        System.exit(0);
    }}}
    }catch (Exception e){
        System.out.print("Invalid Input!");
    }
    }
}

Aucun commentaire:

Enregistrer un commentaire