2017-08-09 17 views
0

爲什麼,如果在下面的代碼語句不執行正確: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)來進行更正,但它仍未執行! –

回答

0

這是一個很奇怪的問題。您可以發佈更多關於上下文的代碼快照。以下是我的上下文快照。希望幫助別人,你:

的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 
相關問題