【问题标题】:Logic in Java \ androidJava\android中的逻辑
【发布时间】:2015-03-26 19:12:10
【问题描述】:

大家好,这是我的检查方法,但我无法处理逻辑。 姓名工作正常,但年龄 \ 体重 \ 身高工作方式错误(当我期待为真时,它变为假)。

我已经用 more () 尝试了几次,但我仍然无法修复它。请帮助我解决这个逻辑,或者如果您有更好的想法来检查并显示有关文本视图中错误值的消息。 Atm 我正在尝试使用警报生成器。

当我输入:_Age 24 时,if 语句加 10(当它低于 9 且超过 70 或为空时,我想加 10) 其余变量也是如此,我找不到我的逻辑错误。

private String checkValue(){
    String x = "";
    int a = 0; // TRUE == 1 false == 0 NAME > AGE > HEIGHT > WEIGHT
    String name = textName.getText().toString();
    String _Age = textAge.getText().toString();
    String _Weight = textWeight.getText().toString();
    String _Height = textHeight.getText().toString();



    if ( TextUtils.isEmpty(name))                                                                                  a+=1;
    if ( TextUtils.isEmpty(_Age) || ((Integer.parseInt(_Age) > 9) && (Integer.parseInt(_Age) < 70 )))                  a+=10;
    if ( TextUtils.isEmpty(_Height) || ((Integer.parseInt(_Height) > 100) &&( Integer.parseInt(_Height) < 250)))       a+=100;
    if ( TextUtils.isEmpty(_Weight) || (( Double.parseDouble(_Weight) > 30 ) && (Double.parseDouble(_Weight) < 300)))   a+=1000;
    if (a==0) x="All right boss here's the answer for your awsome body";
    while (a>=1) {
        x += "Please fill the data: otherwise - I cannot help you out mate";
        if (a >= 1000){
            x += ("\n- Weight a= "+a+" parse int _Weight: " + Integer.parseInt(_Weight));
            a-=1000;
        }
        if (a >= 100){
            x += ("\n- Height a= "+a+" parse int _Height: " + Integer.parseInt(_Height));
            a-=100;
        }
        if (a >= 10){
            x += ("\n- Age a= "+a+" parse int _Age: " + Integer.parseInt(_Age));
            a-=10;
        }
        if (a >= 1) {
            x += "\n- Name";
            a -=1;
        }
    }

【问题讨论】:

  • 你能把具体问题说得更清楚一点,也许格式再多一点吗?您有一些变量悬而未决,看起来它们即将从悬崖上掉下来。

标签: java android logic


【解决方案1】:

简单的逻辑:

String errorString = "";
boolean validate = true;
if(!ageString.isEmpty() && !heightString.isEmpty() && !weightString.isEmpty()) {
    if(age < 9 || age > 70) {
        errorMessage += "Invalid age!";
        validate = false;
    }
    if(height < 100 || age > 250) {
        errorMessage += "Invalid height!";
        validate = false;
    }
    if(weight < 30 || weight > 300) {
        errorMessage += "Invalid weight!";
        validate = false;
    }
}
else {
    errorString = "No empty fields allowed!";
    validate = false;
}
if(validate) {
    //Do whatever you want
}
else return errorString;

【讨论】:

  • 但是这个从不检查文本视图是否为空?(我可能有点困惑,没有读对)。每当它们为空时,我的程序就会崩溃。
  • 您可以通过解释您所做的更改以及更改原因来改进您的答案。
  • 这只是可读代码格式的逻辑。此代码原样不起作用。您必须进行TextViewStringInteger 的转换,并在strings are not empty 之间进行检查。
  • 好的,我会尽快尝试,只是要清醒一下。感谢您的尝试和建议!
  • 我重新构建了代码。希望能帮助到你。第一个 if 条件检查它们是否为空
猜你喜欢
  • 2011-03-30
  • 2012-03-01
  • 2013-07-02
  • 2018-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多