简体   繁体   English

Java,使用多个条件语句,while循环,for循环,if语句的问题

[英]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. 我真的很难再添加一个条件-检查输入是否高于0。

I have put the entire for loop into another if statement as you can see: if(ticketsWanted > 0) 如您所见,我将整个for循环放入另一个if语句中:if(ticketsWanted> 0)

and it prints "Please enter a positive number" if something below 1 has been entered 如果输入的数字小于1,则会显示“请输入一个正数”

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"); 基本上在System.out.println(“请输入一个正数”)之后; 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? 我如何使用while循环,使其循环直到输入正数?

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 else loop.then代码看起来可读。 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. 这是一个非常简单的方法,可用于在提示输入大于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?");
          ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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