【问题标题】:My try catch function won't work as expected我的 try catch 功能无法按预期工作
【发布时间】:2019-05-26 19:32:33
【问题描述】:

我的程序应该取给定的值并用它找到另一个值。但是,我的 try catch 语句似乎不起作用。它从不执行前 2 个 if 语句,这是程序最重要的部分。当您输入两个值时,第三个确实有效。提前致谢。

        public void calculate(View view) {
    EditText length_find = findViewById(R.id.feet);
    EditText pounds_find = findViewById(R.id.pounds);

    try {
        //int length_int = Integer.parseInt(length_find.getText().toString());
        //int pounds_int = Integer.parseInt(pounds_find.getText().toString());

        double d = .29;
        //double length = length_int;
        //double pounds = pounds_int;
        double w = 24.5 / 12;
        double h = .002 / 12;

        //This Calculates if Pounds are given
        if ((length_find).getText().toString().trim().length() == 0){
            Toast.makeText(MainActivity.this, "Given Pounds", LENGTH_LONG).show();
            int pounds_int = Integer.parseInt(pounds_find.getText().toString());
            double pounds = pounds_int;
            double v = pounds / d;
            double length = v / w / h;
            final TextView mTextView = (TextView) findViewById(R.id.length_show);
            mTextView.setText((int) length);
        }


        //This Calculates if Length is given
        if ((pounds_find).getText().toString().trim().length() == 0){
          Toast.makeText(MainActivity.this, "Given Length", LENGTH_LONG).show();
          int lenght_int = Integer.parseInt(length_find.getText().toString());
          double length = lenght_int;
          double v = length * w * h;
          double answer_pounds = v * d;
          final TextView mTextView = (TextView) findViewById(R.id.pound_show);
          mTextView.setText((int) answer_pounds);
        }
        if((pounds_find).getText().toString().trim().length() > 0 && (length_find).getText().toString().trim().length() > 0){
            Toast.makeText(MainActivity.this, "Whata hell you need me for mate!", LENGTH_LONG).show();
        }

    }
    catch(Exception ex) {
            Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();
        }

    }

【问题讨论】:

  • 您是否尝试过调试?你有例外吗?
  • 好奇: length() <= 0 你预计length() 什么时候会是负面的?
  • string的负长度()???这永远不会发生,所以你没有问题,你的逻辑是不对的。
  • 是的,我的错误我明白你们在说什么,但是如果我将

标签: java android if-statement try-catch


【解决方案1】:

据我了解,

(pounds_find).getText().toString().trim().length() <= 0

(length_find).getText().toString().trim().length() <= 0

仅当用户没有输入任何内容时才为真。如果他有,那么第三个“如果”按预期执行。但是当用户没有输入任何内容时

int length_int = Integer.parseInt(length_find.getText().toString());
int pounds_int = Integer.parseInt(pounds_find.getText().toString());

无法完成。因为它是空的。我认为这里就是这种情况。如果不是,请告诉我。如果我错了对不起。这就是我从这里理解的。如果您可以调试并具体说明场景或异常(如果它抛出任何异常),那将更有帮助

【讨论】:

  • 好的,我已经按照你说的做了,但是我还是遇到了同样的问题。我已经更新了代码,所以你可以看看。我注释掉了旧代码。
  • 顺便感谢您的帮助
  • 另外,我对编程还很陌生,只上了一学期的高中课程。它使我的兴趣达到了顶峰,所以我度过了我的寒假。所以我不知道如何调试。
  • 别担心,1个学期你做得很好。你很快就会学会如何调试,但在那之前
  • 你要做的是写一个 if 条件并检查输入是否为空,如果它们是 toast,否则直接将它们解析为 double 并将其分配给全局 double 变量。然后在代码中处理该变量。不要使用磅查找和长度查找,这更像是一种传统。
猜你喜欢
  • 2017-10-22
  • 1970-01-01
  • 2016-01-23
  • 2020-02-26
  • 1970-01-01
  • 2015-06-02
  • 1970-01-01
  • 2016-11-12
相关资源
最近更新 更多