java - 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?
- the
nestedCircle
must contain a recursion - can only change everything below
nestedCircle
- can x, y are just part of the given question so it could be ignored
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;
}
}
解决方案
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
推荐阅读
- python-3.x - 缓存解析的文档
- windows - Windows 10 1903 更新后 WdfWaitLockCreate 函数重新运行 STATUS_INSUFFICIENT_RESOURCES
- java - 无法使用 http post call 创建 github 发行说明
- pyarrow - 是否有 Python 模块可以使用 pyarrow 读取 avro 文件?
- python - 如何在 Python 中读取叶多边形弹出窗口
- python - 训练具有多个特征的 sklearn 分类器
- node.js - 无法使用 nodeJS 在服务器端设置 Cookie
- google-maps - 中心谷歌地图vue组件
- excel - VBA获取单击以在Excel 2016中打开用户框的单元格的行和列索引
- chocolatey - 以下命令的目的是什么: choco install Chocolatey