zer0-day
zer0-day

Reputation: 7

Why is Math.abs() returning 25 and saying no explicit return value?

I'm a bit confused about the math.abs() function. I need to debug this code but I somehow can't find the problem. It always says "no explicit return value" when I try to println() the result.

public class Test {

    public static void main(String[] args) {

        for (int i = 0; i < 50; i++) {
            for (int j = 0; j < 50; j++) {
                if (Math.abs(i - 25) + Math.abs(j - 25) > 25) {
                    System.out.println(" ");

                } else {
                    System.out.println("+");
                }
            }
            System.out.println();
        }

    }
}

I expect the program to output " " when the result of math.abs() is higher than 25 and "+" when it's lower than 25.

Upvotes: 0

Views: 122

Answers (1)

Andreas
Andreas

Reputation: 159096

I believe that comment by Joop Eggen identified the first "problem":

the first two printlns should be print - without newline.

When you change that, the output becomes:

                         +                        
                        +++                       
                       +++++                      
                      +++++++                     
                     +++++++++                    
                    +++++++++++                   
                   +++++++++++++                  
                  +++++++++++++++                 
                 +++++++++++++++++                
                +++++++++++++++++++               
               +++++++++++++++++++++              
              +++++++++++++++++++++++             
             +++++++++++++++++++++++++            
            +++++++++++++++++++++++++++           
           +++++++++++++++++++++++++++++          
          +++++++++++++++++++++++++++++++         
         +++++++++++++++++++++++++++++++++        
        +++++++++++++++++++++++++++++++++++       
       +++++++++++++++++++++++++++++++++++++      
      +++++++++++++++++++++++++++++++++++++++     
     +++++++++++++++++++++++++++++++++++++++++    
    +++++++++++++++++++++++++++++++++++++++++++   
   +++++++++++++++++++++++++++++++++++++++++++++  
  +++++++++++++++++++++++++++++++++++++++++++++++ 
 +++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++
 +++++++++++++++++++++++++++++++++++++++++++++++++
  +++++++++++++++++++++++++++++++++++++++++++++++ 
   +++++++++++++++++++++++++++++++++++++++++++++  
    +++++++++++++++++++++++++++++++++++++++++++   
     +++++++++++++++++++++++++++++++++++++++++    
      +++++++++++++++++++++++++++++++++++++++     
       +++++++++++++++++++++++++++++++++++++      
        +++++++++++++++++++++++++++++++++++       
         +++++++++++++++++++++++++++++++++        
          +++++++++++++++++++++++++++++++         
           +++++++++++++++++++++++++++++          
            +++++++++++++++++++++++++++           
             +++++++++++++++++++++++++            
              +++++++++++++++++++++++             
               +++++++++++++++++++++              
                +++++++++++++++++++               
                 +++++++++++++++++                
                  +++++++++++++++                 
                   +++++++++++++                  
                    +++++++++++                   
                     +++++++++                    
                      +++++++                     
                       +++++                      
                        +++                       

The second "problem" is that the output is missing a single + on the right and on the bottom.

I'll leave it to you to figure that one out. It is after all your assignment, not ours.

Upvotes: 1

Related Questions