用Java语言求解一个困难的双变量方程
我的作业要求执行以下操作:用Java语言求解一个困难的双变量方程,java,equation-solving,Java,Equation Solving,我的作业要求执行以下操作: 搜索2:搜索x*x+y*y-12x-10y+36=0的解决方案。在x和y中从0搜索到10,在移动到下一个x之前搜索每个y值。打印找到的前三个解决方案。(注意-此处贴有标签的断点很方便!) 我搞不懂这其中的逻辑。我想我必须使用2个以上的循环,但不确定。 这就是我到目前为止所做的(它只是重复(6,0)): for(int j=0;j您正在使用一个额外的while循环,该循环运行不稳定 while (((i * i) + (j * j) - (12 * i) - (10 *
搜索2:搜索x*x+y*y-12x-10y+36=0的解决方案。在x和y中从0搜索到10,在移动到下一个x之前搜索每个y值。打印找到的前三个解决方案。(注意-此处贴有标签的断点很方便!) 我搞不懂这其中的逻辑。我想我必须使用2个以上的循环,但不确定。
这就是我到目前为止所做的(它只是重复(6,0)):
for(int j=0;j您正在使用一个额外的while
循环,该循环运行不稳定
while (((i * i) + (j * j) - (12 * i) - (10 * j) + 36) == 0) {
System.out.println("(" + i + ", " + j + ")");
}
第一次计算为true时-即当它达到(6,0)时-它将继续运行,因为i
和j
未在内部修改
如果
你需要用一个替换它,仔细看看你的内部while循环。一旦找到方程的解,i
和j
永远不会改变,公式的计算结果总是0
,从而产生一个无限循环
为了清楚起见,将i
和j
重命名为x和y可能也是明智的。不过,你的思路基本上是正确的。别忘了,你只需要打印前三个解决方案。我帮不了你多少忙,因为这是一个非常简单的概念,但想想你需要循环什么,它可能会让你更容易使用i或j的x和y安装。也只打印(6,0),因为这正是您告诉它使用while循环所做的
一个提示,while循环应该有一个停止其功能的语句,比如如果x<4,如果x大于或等于,那么它将继续在循环外运行。for(int j=0;j这是一个不错的尝试。因为您非常接近,我将向您展示一个有效的解决方案。基本上,您需要做三件事:
for (int j = 0; j <= 10; j++)
{
for (int i = 0; i <= 10; i++)
{
if (((i * i) + (j * j) - (12 * i) - (10 * j) + 36) == 0)
{
System.out.println("(" + i + ", " + j + ")");
return;
}
}
}
将while
更改为if
使用一个变量来计算您找到解决方案的次数,这样您就可以在三点停止
添加一个标签,以便可以从内环中断开外环
为了清晰起见,我还建议您使用与问题相同的变量名,即x
和y
int count = 0;
outerLoop:
for (int y = 0; y <= 10; y++) {
for (int x = 0; x <= 10; x++) {
if (x * x + y * y - 12 * x - 10 * y + 36 == 0) {
System.out.println("(" + x + ", " + y + ")");
if (++count == 3)
break outerLoop;
}
}
}
很抱歉给您添麻烦,但本课程的一部分内容是良好的编码风格和实践。public class EquationSolver{
public class EquationSolver {
public static void main(String[] args) {
int found = 0;
searchSolutions:
for (int y = 0; y <= 10; y++) {
for (int x = 0; x <= 10; x++) {
if (((x * x) + (y * y) - (12 * x) - (10 * y) + 36) == 0) {
System.out.println("(" + x + ", " + y + ")");
found ++;
if (found == 3) {
break searchSolutions;
}
}
}
}
}
}
公共静态void main(字符串[]args){
int=0;
搜索解决方案:
很长一段时间以来,我上一次写了一些类似的作业……只是为了好玩:)
@例如,当我点击submit时,我就看到了错误。我现在编辑。哦,好吧!我把“I”for循环放在“j”之前,以使其正确计算。我明白了!我现在将在原始帖子中发布解决方案!请注意,你在那里做的中断只会中断内部for
循环。请仔细查看作业中的最后一个注释!
int count = 0;
outerLoop:
for (int y = 0; y <= 10; y++) {
for (int x = 0; x <= 10; x++) {
if (x * x + y * y - 12 * x - 10 * y + 36 == 0) {
System.out.println("(" + x + ", " + y + ")");
if (++count == 3)
break outerLoop;
}
}
}
(6, 0)
(3, 1)
(9, 1)
public class EquationSolver {
public static void main(String[] args) {
int found = 0;
searchSolutions:
for (int y = 0; y <= 10; y++) {
for (int x = 0; x <= 10; x++) {
if (((x * x) + (y * y) - (12 * x) - (10 * y) + 36) == 0) {
System.out.println("(" + x + ", " + y + ")");
found ++;
if (found == 3) {
break searchSolutions;
}
}
}
}
}
}
public class Main {
public static void main(String[] args) {
int[] range = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
boolean isSolutionFound = Boolean.FALSE;
int solCounter = 0;
searchSolutions:
for (Integer y : range) {
for (Integer x : range) {
isSolutionFound = checkForSolution(x, y);
if (isSolutionFound) {
printSolution(x, y);
solCounter++;
}
if (solCounter == 3)
break searchSolutions;
}
}
}
private static void printSolution(Integer x, Integer y) {
System.out.println(x + "," + y); // use some fancy formatting instead
}
private static boolean checkForSolution(int x, int y) {
if (x * x + y * y - 12 * x - 10 * y + 36 == 0)
return true;
else
return false;
}
}