Java 我的try-catch函数赢了';I don’我没有按预期工作
我的程序应该取给定的值,然后用它找到另一个值。然而,我的try-catch语句似乎不起作用。它从不执行前两个if语句,这是程序最重要的部分。当您输入这两个值时,第三个值确实起作用。提前谢谢Java 我的try-catch函数赢了';I don’我没有按预期工作,java,android,if-statement,try-catch,Java,Android,If Statement,Try Catch,我的程序应该取给定的值,然后用它找到另一个值。然而,我的try-catch语句似乎不起作用。它从不执行前两个if语句,这是程序最重要的部分。当您输入这两个值时,第三个值确实起作用。提前谢谢 public void calculate(View view) { EditText length_find = findViewById(R.id.feet); EditText pounds_find = findViewById(R.id.pounds); t
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();
}
}
据我所知
(pounds_find).getText().toString().trim().length() <= 0
做不到。因为它是空的。我想这里就是这样。如果不是,请告诉我。如果我错了,对不起。这就是我从这里了解到的。如果您可以进行调试,并且如果它抛出任何场景或异常,您可以对其进行详细说明,这将更有帮助您尝试过调试吗?您是否遇到异常?奇怪:
string
的length()负长度()???这是不可能发生的,所以你没有问题,你的逻辑是不正确的。是的,我错了,我知道你们在说什么,但是如果我换了,它仍然有同样的问题。好的,我做了你说的,但是,我仍然遇到同样的问题。我已经更新了代码,你可以看一下。我把旧代码注释掉了。顺便说一句,谢谢你的帮助。另外,我对编程还相当陌生,高中只上了一个学期的课。它使我的兴趣达到顶峰,所以我把寒假都花在了这上面。所以我不知道如何调试。别担心,你一学期都做得很好。您将很快学会如何调试,但在此之前,您必须编写一个if条件并检查输入是否为空,如果为make toast,否则直接将其解析为double并将其分配给全局double变量。然后在代码中处理该变量。不要使用磅数和长度,这更像是一种传统。
(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());