How to use this boolean in an if statement?

前端 未结 8 1539
一个人的身影
一个人的身影 2020-11-28 15:35
private String getWhoozitYs(){
    StringBuffer sb = new StringBuffer();
    boolean stop = generator.nextBoolean();
    if(stop = true)
    {
        sb.append(\"y\         


        
相关标签:
8条回答
  • 2020-11-28 16:32

    Actually, the entire approach would be cleaner if you only had to use one instance of StringBuffer, instead of creating one in every recursive call... I would go for:

    private String getWhoozitYs(){
         StringBuffer sb = new StringBuffer();
         while (generator.nextBoolean()) {
             sb.append("y");
         }
    
         return sb.toString();
    }
    
    0 讨论(0)
  • 2020-11-28 16:37
    if(stop == true)
    

    or

    if(stop)
    

    = is for assignment.

    == is for checking condition.

    if(stop = true) 
    

    It will assign true to stop and evaluates if(true). So it will always execute the code inside if because stop will always being assigned with true.

    0 讨论(0)
提交回复
热议问题