簡體   English   中英

Java,使用多個條件語句,while循環,for循環,if語句的問題

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

我正在開發一個程序,該程序可以幫助注冊來賓。

當事人必須存在於數據中

當然,所需票數必須少於可用票數

我真的很難再添加一個條件-檢查輸入是否高於0。

如您所見,我將整個for循環放入另一個if語句中:if(ticketsWanted> 0)

如果輸入的數字小於1,則會顯示“請輸入一個正數”

但是,它還會打印“此聚會不存在或已被預訂滿”。 再次..

基本上在System.out.println(“請輸入一個正數”)之后; 我希望程序提供一個選項來再次輸入數字,直到輸入正數為止

所以這是我的問題:

我如何使用while循環,使其循環直到輸入正數?

有沒有更有效/更好的方式來做我打算做的事情?

謝謝

 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.");

添加方法:

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

並使用以下命令調用它:

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();
    }

這可行。

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

             }

而不是為每個循環使用流和過濾器,而不是if else loop.then代碼看起來可讀。 如果包含更多嵌套循環,則不是最佳實踐。

這是一個非常簡單的方法,可用於在提示輸入大於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;
  }

然后您可以像這樣從其他方法調用它

          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