在 Java 中求解具有两个变量的困难方程
我的作业要求执行以下操作:
Search2:搜索 x*x + y*y - 12x -10y + 36 = 0 的解。在 x 和 y 中从 0 到 10 搜索,在移动到下一个 x 之前搜索每个 y 值。打印找到的前三个解决方案。 (注意 - 带标签的中断在这里很方便!)
我无法弄清楚这一点的逻辑。我想我必须使用 2 个以上的循环,但不确定。
这就是我到目前为止所拥有的(它只是重复(6,0)):
for (int j = 0; j <= 10; j++) {
for (int i = 0; i <= 10; i++) {
while (((i * i) + (j * j) - (12 * i) - (10 * j) + 36) == 0) {
System.out.println("(" + i + ", " + j + ")");
}
}
}
更新
这是解决方案:
int t = 0;
for (int i = 0; i <= 10; i++) {
if (t == 3) {
break;
}
for (int j = 0; j <= 10; j++) {
if (((i * i) + (j * j) - (12 * i) - (10 * j) + 36) == 0) {
System.out.println("(" + i + ", " + j + ")");
t++;
}
}
}
My assignment says to do the following:
Search2: Search for a solution to x*x + y*y - 12x -10y + 36 = 0. Search from 0 to 10 in both x and y, searching every y value before moving to the next x. Print the first three solutions found. (Note - a labelled break is handy here!)
I can't figure out the logic for this. I think I have to use more than 2 loops but not sure.
This is what I have so far (It just repeats (6,0)):
for (int j = 0; j <= 10; j++) {
for (int i = 0; i <= 10; i++) {
while (((i * i) + (j * j) - (12 * i) - (10 * j) + 36) == 0) {
System.out.println("(" + i + ", " + j + ")");
}
}
}
UPDATE
Here is the solution:
int t = 0;
for (int i = 0; i <= 10; i++) {
if (t == 3) {
break;
}
for (int j = 0; j <= 10; j++) {
if (((i * i) + (j * j) - (12 * i) - (10 * j) + 36) == 0) {
System.out.println("(" + i + ", " + j + ")");
t++;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
不错的尝试。因为你已经很接近了,所以我会向你展示一个可行的解决方案。基本上,您需要做三件事:
while
更改为if
为了清楚起见,我还建议您使用与问题相同的变量名称 - 即
x
和y
。执行时,此代码会生成:
抱歉给您喂食,但这里的课程的一部分是良好的编码风格和实践。
Not a bad attempt. Because you're so close, I'll show you a working solution. Basically, you need to do three things:
while
toif
I also recommend you use variable names the same as the problem - ie
x
andy
- for clarity.When executed, this code produces:
Sorry for spoon-feeding you, but part of the lesson here is good coding style and practice.
您正在使用一个无限期运行的额外
while
循环。第一次计算结果为真时 - 即当它达到 (6,0) 时 - 它将继续运行,因为
i
和j
未在内部修改。您需要将其替换为
if
。You're using an extra
while
loop that runs indefinetly.The first time this evaluates to true - i.e. when it reaches (6,0) - it will keep running because
i
andj
are not modified inside.You need to replace it with an
if
.仔细看看你的内部 while 循环。一旦找到方程的解,
i
和j
就永远不会改变,并且公式的计算结果始终为0
,从而导致无限循环。为了清楚起见,将
i
和j
重命名为 x 和 y 也可能是明智的。不过,你基本上走在正确的轨道上。不要忘记您只需打印前三个解决方案。Take a good look at the inner while loop you have. Once the solution to the equation is found,
i
andj
never change, and the formula always evaluates to0
, resulting in an infinite loop.It might also be wise to rename
i
andj
to x and y, for clarity. You're mostly on the right track though. Don't forget you only have to print the first three solutions.我不会给你太多帮助,因为这是一个非常简单的概念,但想想你需要循环什么,它可能会更容易使用 x 和 y 代替 i 或 j。另外,您仅打印 (6,0),因为这正是您告诉它对 while 循环执行的操作。
一个提示,您的 while 循环应该有一个停止其功能的语句,假设 if x < 4、如果它大于或= to 则它将在循环外继续。
I wont help you much since this is a pretty simple concept, but think about what you need to loop through, it might make it easier to instead use x and y instad of i or j. Also your printing only (6,0) because thats exactly what you told it to do with the while loop.
One hint, your while loop should have a statement that stops its function, lets say if x < 4, if it ever is greater or = to than it will continue outside the loop.
我上次写这类作业已经很久了……只是为了好玩:)
Long time I last wrote some homework of that sort...just for the fun of it :)
记住
return;
部分很重要,否则尽管您已经找到了解决方案,但您仍然会搜索解决方案。如果您不需要更多解决方案,那么您应该省略“return”语句。It is import to remember the
return;
part as you would otherwise still search for solutions although you already found one. If you wan't more solutions, then you should should omit the ´return´ statement.