【问题标题】:I get android runtime error when applying multiple if conditions, android应用多个if条件时出现android运行时错误,android
【发布时间】:2014-01-12 06:25:11
【问题描述】:

您好,我有以下代码用于计算从 editText 获取子网掩码输入,并且我在单个 if 行上执行多个条件。基本上我已经使用 .split 破坏了子网的 4 个部分,然后保存在数组中。无论如何,这是代码。

       @Override
        public void onClick(View arg0) {
            String strings = String.valueOf(inputIp.getText().toString());
            final String parts[] = strings.split("\\.");
                   String block1 = parts[0];
                   String block2 = parts[1];
                   String block3 = parts[2];
                   String block4 = parts[3];

                   Double firstblock = Double.parseDouble(block1);
                   Double secondblock = Double.parseDouble(block2);
                   Double thirdblock = Double.parseDouble(block3);
                   Double fourthblock = Double.parseDouble(block4);

                   String substring = String.valueOf(inputSubnet.getText().toString());
                    String subPart[] = substring.split("\\.");
                        String  subBlock1 = subPart[0];
                        String  subBlock2 = subPart[1];
                        String  subBlock3 = subPart[2];
                        String  subBlock4 = subPart[3];

                        Double firstSubBlock =  Double.parseDouble(subBlock1);
                        Double secondSubBlock = Double.parseDouble(subBlock2);
                        Double thirdSubBlock = Double.parseDouble(subBlock3);
                        Double fourthSubBlock = Double.parseDouble(subBlock4);

            if(firstblock <= 127){

        //problem       
if((firstSubBlock == 255 || firstSubBlock !=0) &&  (secondSubBlock ==0 && secondSubBlock !=null) &&(thirdSubBlock== 0 && thirdSubBlock !=null) &&(fourthSubBlock == 0 && fourthSubBlock !=null)){
                int rangedisplay = (int) (Math.pow(2, 24)-2);
                showClass.setText("A");
                rangeDisplay.setText(rangedisplay);

                }else{

                Toast.makeText(getApplicationContext(), "Please enter a valid subnet mask !!", Toast.LENGTH_SHORT).show();

                }


            }else if(firstblock <=191){

                if((firstSubBlock == 255) && (secondSubBlock==255) &&(thirdSubBlock== null) &&(fourthSubBlock == null)){
                int rangedisplay = (int) (Math.pow(2, 16)-2);
                showClass.setText("B");
                rangeDisplay.setText(rangedisplay);
                }else{

                Toast.makeText(getApplicationContext(), "Please enter a valid subnet mask !!", Toast.LENGTH_SHORT).show();

                }

            }else if(firstblock <=223){

                if((firstSubBlock == 255) && (secondSubBlock==255) &&(thirdSubBlock==255) &&(fourthSubBlock == null)){
                    int rangedisplay = (int) (Math.pow(2, 8)-2);
                    showClass.setText("C");
                    rangeDisplay.setText(rangedisplay);

                   }else
                    {

                    Toast.makeText(getApplicationContext(), "Please enter a valid subnet mask !!", Toast.LENGTH_SHORT).show();

                    }
            }else{

                Toast.makeText(getApplicationContext(), "Please enter a valid Ip Address !!", Toast.LENGTH_SHORT).show();

            }





        }

在下面的行中,我评论了“问题”,我通过了多个条件,即使我输入了确切的真实条件。我收到运行时错误或提示“输入有效的子网掩码!!”的消息这是不对的,因为我已经输入了所有应该执行 if 块的条件。非常感谢您的参与。任何建议都将不胜感激,因为这是我的大学作业,而且我已接近截止日期。再次感谢您。

【问题讨论】:

    标签: java android if-statement conditional-statements


    【解决方案1】:

    尝试使用 int 代替 Double。在 Java 中直接将 double 与 == 进行比较不是正确的方法。

    【讨论】:

      【解决方案2】:

      我发现了问题,int变量随机显示没有解析成String。这就是问题所在,但还是谢谢你。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-11-27
        • 2016-09-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多