2017-10-10 89 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; 
       } 
      } 
     } 

    }