samedi 13 janvier 2018

Java If statement to check a user input value ensuring it's less than a set int variable

  int totalCount = total1 + total2 + total3 + total4;
        int MaxTotal = 32;
        int MaxTotalCal = MaxTotal - (totalCount);


       System.out.print("\nEnter First name: ");
       Scanner FN = new Scanner(System.in);
       String fn = FN.nextLine();

       System.out.print("Enter Last name: ");
       Scanner LN = new Scanner(System.in);
       String ln = LN.nextLine();

       System.out.print("Enter Phone number: ");
       Scanner Num = new Scanner(System.in);
       String num = Num.nextLine();


       System.out.print("Adults in party: ");
       Scanner adult = new Scanner(System.in);
       int Adult = adult.nextInt();

       System.out.print("Kids in party: ");
       Scanner kid = new Scanner(System.in);
       int Kid = kid.nextInt();

       int inputTotal = MaxTotalCal - Kid - Adult;

       if(inputTotal > MaxTotal){
           System.out.print("Error");
       }else{
                System.out.print("\n"+(uniqueID + " ||"));      
                System.out.print(fn+ " ||");
                System.out.print(ln+ " ||");
                System.out.print(num+ " ||");
                System.out.print(Adult+ " ||");
                System.out.print(Kid+ " ||");
                System.out.println("\nTotal seats left to book: " + inputTotal);
}

       System.in.read();

Essentially i'm trying to add the user input for adults and kids to the total, if the total is more then 32 then the class will restart, otherwise if it's less than 32 then execute the print statements. Where have I gone wrong?

Thank you beforehand for any help given, i'm rather new to java

Aucun commentaire:

Enregistrer un commentaire