2017-10-10 95 views
-4
public static void checkWeight(int startWeight, Number weight){ 
     System.out.println("HERE?!"); 
     y=false; 
     System.out.println("HEHEHEH"); 
     mainLoop:{ 
      if((y==true)||(startWeight<=50)) { 
       weight=startWeight; 
       System.out.println("HERE"); 
       break mainLoop; 
      }else{ 
      subMainLoop:{ 
       while(z==false) { 
        int userWeightCheck=Integer.parseInt(JOptionPane.showInputDialog("Your cat's weight: ")); 
         if(userWeightCheck<=50) { 
          System.out.println("OR HERE"); 
          y=true; 
          z=true; 
          break subMainLoop; 
         }else { 
          System.out.println("Sorry, but that is not an acceptable input. Try again."); 
          continue; 
         } 
       } 
      } 
      } 
     } 
    } 

嘿,所有!我似乎無法解決這個問題。我已經添加了System.out.println來嘗試自己拍攝它。過去1個半月我一直在瀏覽互聯網,但仍無法找到解決方案。它打印"HEHEHEH",當它應該通過if語句時doe不打印「HERE」。使用數字的驗證失敗

在我的主類的調用是這樣的:

int userWeight=Integer.parseInt(JOptionPane.showInputDialog("Your cat's weight")); 
Animal.checkWeight(userWeight, 51); 

的問題是,當用戶輸入一個數字,它應該工作,當且僅當數量爲=或< 50.否則,它彈出一個窗口,要求用戶重新輸入他們以前的答案。然而,它似乎完全忽略了循環。

任何幫助大規模讚賞! :)

+1

問題是......? *「它打印」HEHEHEH「,即使它沒有滿足任何條件」* - 沒有條件打印該消息。 – Tom

+0

而不是使用'System.out.println()'你應該學會使用調試器。這對追蹤錯誤更有幫助。 – Thomas

+2

在檢查任何條件之前打印「HEHEHEH」。 –

回答

0

這是我可以做的最好的事情,同時試圖理解你的意思,並試圖保持你寫的方式相同,無論出於什麼原因使用。我擺脫了標籤,並用一段時間替換了它們。

public static void checkWeight(int startWeight, Number weight) { 
     System.out.println("HERE?!"); 
     boolean run = true; 
     System.out.println("HEHEHEH"); 
     while (true) { 

      if (startWeight <= 50) { 
       weight = startWeight; 
       System.out.println("HERE"); 
       break; // or "return weight" maybe? 
      } 

      while (run) { 
       int userWeightCheck = 
Integer.parseInt(JOptionPane.showInputDialog("Your cat's weight: ")); 
       if (userWeightCheck <= 50) { 
        System.out.println("OR HERE"); 
        run = false; 
        startWeight = userWeightCheck; 
        break; // or "weight" maybe? 
       } else { 
        System.out.println("Sorry, but that is not an acceptable 
input. Try again."); 
        continue; 
       } 
      } 
     } 

    }