JAVA代码不执行,如果我有在试图找出一个很难言

问题描述:

为什么,如果在下面的代码语句不执行正确:JAVA代码不执行,如果我有在试图找出一个很难言

double yint = -157.42; 
if(copy_denominator.contains("x^"+nextBottom))yint = -1*constant/d2; 

Log.d(PERIOD,"copy den: "+copy_denominator +" yint "+yint); 
//the Log statement above prints out that yint = 3.0; 

if(yint != -157.42) 
Log.d(PERIOD,"oblique 5 "+slope+"x + "+yint);//I expect this to print to Log 

if(yint == -157.42)Log.d(PERIOD,"oblique 6 "+slope+"x ");//This prints out. 

我不知道为什么声明:if(yint!= -157.42)不执行。 我从来没有见过这个,它可能是Android Studio中的一个错误。

感谢您的任何建议

+0

你的代码格式是非常糟糕的。同样使用==和!=来与具有浮点的常量进行比较不是一个好主意。相比之下,允许有一些误差。此外,不要命名双“yint”类型的变量。你也确定它打印出“yint = 3.0;”?希望这可以帮助。祝你好运得到你的问题的答案。 – user643011

+0

我认为你可能是对的。让我看看我该如何尝试这个误差的计算。 @ user643011 –

+0

是的,它打印出“yint = 3.0”@ user643011。我尝试通过设置yint = 157.42并使用if(Math.abs(157.42 - yint)> 0.001)来进行更正,但它仍未执行! –

这是一个很奇怪的问题。您可以发布更多关于上下文的代码快照。以下是我的上下文快照。希望帮助别人,你:

的Android 2.3.3工作室

compileSdkVersion 25

buildToolsVersion “25.0.2”

的minSdkVersion 16

targetSdkVersion 25

public class MainActivity extends AppCompatActivity { 

    private final String PERIOD = "PERIOD"; 

    private ArrayList<String> copy_denominator = new ArrayList<>(); 
    private int nextBottom = 2; 
    private final double constant = 6.0D; 
    private double d2 = 2.0D; 
    private String slope = "2.3"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     copy_denominator.add("x^2"); 

     double yint = -157.42; 
     if (copy_denominator.contains("x^" + nextBottom)) yint = -1 * constant/d2; 

     Log.d(PERIOD, "copy den: " + copy_denominator + " yint " + yint); 
     //the Log statement above prints out that yint = 3.0; 

     if (yint != -157.42) 
      Log.d(PERIOD, "oblique 5 " + slope + "x + " + yint);//I expect this to print to Log 

     if (yint == -157.42) Log.d(PERIOD, "oblique 6 " + slope + "x ");//This prints out. 

    } 
} 

日志是好的:

[Logcat prints][1] 

日志:

08-09 19:49:21.792 12323-12323/io.github.dkbai.appdemo D/PERIOD: copy den: [x^2] yint -3.0 

08-09 19:49:21.792 12323-12323/io.github.dkbai.appdemo D/PERIOD: oblique 5 2.3x + -3.0