简体   繁体   中英

Java, problems with using multiple conditional statements, while loop, for loop, if statement

I am working on a program that helps with signing up guests to parties.

the party has to exist in the data

and ofcourse the amount of tickets wanted must be less than the tickets available

I am really struggling with adding one more condition - to check whether the input is higher than 0.

I have put the entire for loop into another if statement as you can see: if(ticketsWanted > 0)

and it prints "Please enter a positive number" if something below 1 has been entered

However, it also prints "This party does not exist or is fully booked." again..

Basically after the System.out.println("Please enter a positive number"); I want the program to provide an option to enter the number again until it's positive

So here are my questions:

How could I use the while loop so that it loops until the positive number has been entered?

Is there a more efficient/better way of doing what I intend to do ?

Thank you

 private static void signUp2(Guest validGuest){

              input.nextLine();
              System.out.println("Enter the name of the party");
              String partyName = input.nextLine();
              System.out.println("How many tickets the guest wishes to purchase?");
              int ticketsWanted = input.nextInt();
              boolean check = false;

              if(ticketsWanted > 0) {
               for(Party p: parties){
                 if(p.getPartyName().equals(partyName)){
                     if(ticketsWanted > e.getTickets()){
                 }
                   else{
                     check = true;  
                     validGuest.AddPartiesAndTickets(p, ticketsWanted);
                    }

                 }
              }
            }
             else{
                System.out.println("Please enter a positive number");

               }

            if(check == false){
            System.out.println("This party does not exist or is fully booked.");

add the method:

int getNumberTickets() {
    System.out.println("How many tickets the guest wishes to purchase?");
    int ticketsWanted = input.nextInt();
    return(ticketsWanted);
}

and call it using:

int ticketsWanted = 0;
do {
    try {
       ticketsWanted = getNumberTickets();
       if (ticketsWanted < 1)
          throw new RuntimeException("Unused");
     catch(Throwable e) {
         ticketsWanted = -1;
         System.out.println("Invalid amount");
     }
while (ticketsWanted < 1);
 // now do your checks
 Using the continue keyword


   boolean check = false;
    boolean valid = false;

   while(valid == false){
   Sop( " how many tickets? ");
   ticketsWanted = input.nextInt ();
   if  (ticketsWanted < 0)  
       continue ;
   else   
    {
  valid = true;
// Rest of the code
 }

}

int ticketsWanted = input.nextInt();

while(ticketsWanted < 0) {
        ticketsWanted = input.nextInt();
    }

This works.

for(Party p: parties){
             if(p.getPartyName().equals(partyName)){
                 if(ticketsWanted > e.getTickets()){
             }
               else{
                 check = true;  
                 validGuest.AddPartiesAndTickets(p, ticketsWanted);
                }

             }

Instead of for each loop use streams and filters instead of if else loop.then code looks readable. If you include more nested loops ..it is not a best practice.

Here is a very simple method you can use to ensure the user inputs the correct value when prompted for a integer greater than 0.

  public static int getNumberGreaterThanZero(String prompt)
  {
      int number = 0;
      while(number == 0)
      {
          System.out.println(prompt);
          String inputFromUser = input.nextLine();
          //check if input matches any negative or positive number
          if(inputFromUser.matches("-\\d+|\\d+"))
          {
              number = Integer.parseInt(inputFromUser);
              if(number <= 0)
              {
                  System.out.println(number + " is not greater than 0");
                  number = 0; //have them try again
              }
          }
          else
              System.out.println("Error - " + inputFromUser + " is not a valid number");

      }
      return number;
  }

Then you can call it from your other method like so

          System.out.println("Enter the name of the party");
          String partyName = input.nextLine();
          int ticketsWanted = getNumberGreaterThanZero("How many tickets the guest wishes to purchase?");
          ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM