java - why does this code print the output it is supposed to print if a Boolean is true but the Boolean was declared to false?

时间:2015-05-12 23:03:52

标签: java boolean

I have the following code:

Unexpected Token :

The output is:

import java.util.*;
public class Test {
   public static void main(String[] args) {
   boolean b = false;
   if (b=true) 
      System.out.println("one. b = false");
   if (b)
      System.out.println("two. b = false");
   }
}

I set b equal to false so why does it print the statement for when b is true?

5 个答案:

答案 0 :(得分:5)

You are doing assignment, not comparison

>= 0

You mean to use

if (b=true)

答案 1 :(得分:3)

You need to use.

 apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

defaultConfig {
    applicationId "com.sistemasmas.escribiramanowhatsapp"
    minSdkVersion 15
    targetSdkVersion 22
    versionCode 1
    versionName "1.0"
}




buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'


    }
}
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.1.1'
    compile 'com.google.android.gms:play-services:7.0.0'
}

If you need to check equality then you need to use "==". But since you are using boolean you don't need to check equality instead you can use the value.

答案 2 :(得分:0)

Please check below:

 $file="test.txt";
    //maybe your server cannot find the root
    // if it does not solve the problem write root file command
// $file=dirname(__FILE__).DIRECTORY_SEPARATOR."test.txt";
    if (file_exists($file)) {
       //its ok try to continue
    } else {
        echo "the text file is not exits !";
    }

答案 3 :(得分:0)

The problem is that in the if statement "python C:\TestProj\TestApp\manage.py shell" you are not comparing. I meant you are not telling to java b=true, what you are really doing here is setting true to b. For that reason you can print two System.out.print statements. Because in both cases the value is true.

Take into account this:

  • Set value true to b: boolean b = true;
  • Use boolean in if statement

    if b is equal to true

Avoid this statament

if (b) {
}

答案 4 :(得分:-2)

Try not 8 but (b=true).