首页 > 解决方案 > How do I edit this to fit what is required?

问题描述

I am writing this program for my class. It requires my return to be

[0.4, 0.35, 0.3, 0.25, 0.2, 0.15, 0.1, 0.05, 0.0]

However, I only manage to get to

0.4, 0.35, 0.3, 0.25, 0.2, 0.15, 0.1, 0.05, 0.0]

Is there any way to change anything inside the nestedCircle to fulfill the requirement?

public class HelloWorld {
    public static void main(String []args) {
        System.out.println(nestedCircle(0.5, 0.5, 0.4, 0.05, ""));
    }

    public static String nestedCircle (double x, double y, double radius, double diff, String radiusList) {
        // your code goes here. Task 1.
        radius = (double) Math.floor(radius * 100) / 100;
        if (radius <= 0) {
            return "0.0]";
        } else {
            radiusList = radius + ", " + nestedCircle(x, y, radius-diff, diff, radiusList);
        }
        return radiusList;
    }
}

标签: java

解决方案


As you can only modify what's inside, some alternatives in order to check if you need to append [ at the beginning of your string:

1. System.Properties

2. Env Variables

3. Do stupid things - (recommended)

4. Mom, I may be dumb


-1- System.Properties

String nestedCircle (double x, double y, double r, double d, String radiusList) 
{
   boolean start=false;
   if (System.getProperty("startnested")==null)
   {
      System.setProperty("startnested","true");
      start=true;
   }         
   r = (double) Math.floor(r * 100) / 100;
   if (r <= 0) 
      return "0.0]";
   radiusList = (start ? "[" : "") +r+ ", " +nestedCircle(x, y, r-d, d, radiusList);
   
   return radiusList;
}

-2- Reflection and envs

String nestedCircle (double x, double y, double r, double d, String radiusList) 
{
   boolean start = false;
   Map<String, String> env = System.getenv();
   Field field = env.getClass().getDeclaredField("m");
   field.setAccessible(true);
   if (((Map<String, String>) field.get(env)).get("nested") == null)
   {
      (((Map<String, String>) field.get(env)).put("nested","true"); 
       start=true;
   }    
           
   r = (double) Math.floor(r * 100) / 100;
   if (r <= 0) 
     return "0.0]";
   radiusList = (start ? "[" : "") + r + ", " +nestedCircle(x, y, r-d, d, radiusList);
   
   return radiusList;
}

-3- Just implement the weirdest idea you could think of

This is strongly recommended.

String nestedCircle (double x, double y, double r, double d, String radiusList) 
{
   boolean start=false;
   try 
   {
     System.in.available();
     start=true;
     new Scanner(System.in).close();
   } catch (IOException e) {}
   
   r = (double) Math.floor(r * 100) / 100;
   if (r <= 0) 
      return "0.0]";
   radiusList = (start ? "[" : "") +r+", "+nestedCircle(x, y, r-d, d, radiusList);
   
   return radiusList;
}

Code maintainers will love this snippet so much. They just love these things a lot. It's a disgrace to software engineering, but who cares anyway.


-4- Mom, am I dumb?

The answer to the question is "probably, son".

Add the initial bracket unconditionally and replace it (.replace("[" , "")) for each result of the recursive calls. The root invocation won't be affected, leaving the initial [ properly. And that's it.

String nestedCircle (double x, double y, double r, double d, String radiusList) 
{ 
   r = (double) Math.floor(r * 100) / 100;
   if (r <= 0) 
      return "0.0]";
   
   return "[" + r + ", "+ nestedCircle(x,y,r-d,d,radiusList).replace("[" , "");
}

.substring(1) would probably be more appropiate, but then the last return "0.0]" should include a dummy prefix, such as a blank space or a character.

String nestedCircle (double x, double y, double r, double d, String radiusList) 
{  
   r = (double) Math.floor(r * 100) / 100;
   if (r <= 0) 
       return "@0.0]";
 
   return "[" + r + ", "+ nestedCircle(x,y,r-d,d,radiusList).substring(1);    
}

:_ )


-5- Yes, son


Conclusions

1. Approach nº 3 is recommended


推荐阅读