不确定“&&”条件在 Java 中的工作方式
Unsure of the way"&&" conditional works in Java
我的病情有一些基本问题,我不知道为什么。
我想要的是 运行 这个循环,而 e1
和 e2
和 e3
比 tol
大,但它只考虑了e3>tol
条件,并忽略它之前的两个。
这是代码:
do{
....
}while((e1>tol)&&(e2>tol)&&(e3>tol));
我相信它与 && 或我将它们与同一变量进行比较有关 tol
但我仍然不确定为什么。
我设法让它与三个嵌套的 ifs 一起工作:
do{
.....
if(e1<tol)
if(e2<tol)
if(e3<tol)
b=true;
}while(b==false);
但这不是我需要的方式。
感谢您的宝贵时间。
这是完整代码:
public class GaussSeidel {
public static void op(double tol){
double e1=0,e2=0,e3=0;
double fx1,fx2,fx3;
double x1=0,x2=0,x3=0;
int n=0;
do{
n++;
System.out.println(n+".- x1= "+x1+"/x2= "+x2+"/x3= "+x3+
"/E1= "+e1+"/E2= "+e2+"/E3= "+e3+"\n");
fx1=((27-x2-x3)/-2);
e1=Math.abs((fx1-x1)/fx1);
x1=fx1;
fx2=((85+2*x1-x3)/4);
e2=Math.abs((fx2-x2)/fx2);
x2=fx2;
fx3=((109-x1+3*x2)/6);
e3=Math.abs((fx3-x3)/fx3);
x3=fx3;
}while((e1>tol)&&(e2>tol)&&(e3>tol));
System.out.println(n+".- x1= "+x1+"/x2= "+x2+"/x3= "+x3+
"/E1= "+e1+"/E2= "+e2+"/E3= "+e3+"\n");
}
public static void main (String[]args){
op(Double.parseDouble
(JOptionPane.showInputDialog("Tolerance")));
}
}
如果我删除其他两个条件并且只使用 }while(e1>tol);
它有效并且其他两个也是如此,如果我单独条件它们,它有效,我的问题是在尝试检查三个符合条件
我认为它不完全相同,在包含“&&”的同时,您期望所有三个 e* 在每次迭代中都大于 tol,而在第二个代码段中您没有完全相同,e1 e2 可能小于 tol 而不是 e3,这不会发生在第一个片段中。
HTH
尝试编写单元测试并在条件为真时调试条件并进入循环。您可以像这样错开代码来调试每一行:
do{
....
}while((e1>tol)
&&(e2>tol)
&&(e3>tol));
您的 if
陈述:
if (e1 < tol)
if (e2 < tol)
if (e3 < tol)
b = true;
等同于:
b = (e1 < tol && e2 < tol && e3 < tol);
即仅当 all 为真时才为真。
你的while
循环条件(b == false)
(最好写成(! b)
),当然是相反的。当反转这样的布尔条件时,你反转 一切 ,在你的情况下 &&
⟺ ||
,和 <
⟺ >=
,所以你得到:
} while (e1 >= tol || e2 >= tol || e3 >= tol);
如您所见,您最初的错误是您需要使用 ||
(或),而不是 &&
(和)。
=
是否应该存在,或者您的意思是 (e1 > tol || e2 > tol || e3 > tol)
,由您决定。
在简单的英语中,while (! b)
读作 "while not B" 或 "stop when B",并且由于 B 被定义为 "true if all of E1, E2, and E3 are less than TOL",因此合并它意味着 "stop when all of E1, E2, and E3 are less than TOL" .
使用更新后的 while
条件,读作 "continue while at least one of E1, E2, or E3 are greater than (or equal to) TOL"。
我的病情有一些基本问题,我不知道为什么。
我想要的是 运行 这个循环,而 e1
和 e2
和 e3
比 tol
大,但它只考虑了e3>tol
条件,并忽略它之前的两个。
这是代码:
do{
....
}while((e1>tol)&&(e2>tol)&&(e3>tol));
我相信它与 && 或我将它们与同一变量进行比较有关 tol
但我仍然不确定为什么。
我设法让它与三个嵌套的 ifs 一起工作:
do{
.....
if(e1<tol)
if(e2<tol)
if(e3<tol)
b=true;
}while(b==false);
但这不是我需要的方式。
感谢您的宝贵时间。
这是完整代码:
public class GaussSeidel {
public static void op(double tol){
double e1=0,e2=0,e3=0;
double fx1,fx2,fx3;
double x1=0,x2=0,x3=0;
int n=0;
do{
n++;
System.out.println(n+".- x1= "+x1+"/x2= "+x2+"/x3= "+x3+
"/E1= "+e1+"/E2= "+e2+"/E3= "+e3+"\n");
fx1=((27-x2-x3)/-2);
e1=Math.abs((fx1-x1)/fx1);
x1=fx1;
fx2=((85+2*x1-x3)/4);
e2=Math.abs((fx2-x2)/fx2);
x2=fx2;
fx3=((109-x1+3*x2)/6);
e3=Math.abs((fx3-x3)/fx3);
x3=fx3;
}while((e1>tol)&&(e2>tol)&&(e3>tol));
System.out.println(n+".- x1= "+x1+"/x2= "+x2+"/x3= "+x3+
"/E1= "+e1+"/E2= "+e2+"/E3= "+e3+"\n");
}
public static void main (String[]args){
op(Double.parseDouble
(JOptionPane.showInputDialog("Tolerance")));
}
}
如果我删除其他两个条件并且只使用 }while(e1>tol);
它有效并且其他两个也是如此,如果我单独条件它们,它有效,我的问题是在尝试检查三个符合条件
我认为它不完全相同,在包含“&&”的同时,您期望所有三个 e* 在每次迭代中都大于 tol,而在第二个代码段中您没有完全相同,e1 e2 可能小于 tol 而不是 e3,这不会发生在第一个片段中。 HTH
尝试编写单元测试并在条件为真时调试条件并进入循环。您可以像这样错开代码来调试每一行:
do{
....
}while((e1>tol)
&&(e2>tol)
&&(e3>tol));
您的 if
陈述:
if (e1 < tol)
if (e2 < tol)
if (e3 < tol)
b = true;
等同于:
b = (e1 < tol && e2 < tol && e3 < tol);
即仅当 all 为真时才为真。
你的while
循环条件(b == false)
(最好写成(! b)
),当然是相反的。当反转这样的布尔条件时,你反转 一切 ,在你的情况下 &&
⟺ ||
,和 <
⟺ >=
,所以你得到:
} while (e1 >= tol || e2 >= tol || e3 >= tol);
如您所见,您最初的错误是您需要使用 ||
(或),而不是 &&
(和)。
=
是否应该存在,或者您的意思是 (e1 > tol || e2 > tol || e3 > tol)
,由您决定。
在简单的英语中,while (! b)
读作 "while not B" 或 "stop when B",并且由于 B 被定义为 "true if all of E1, E2, and E3 are less than TOL",因此合并它意味着 "stop when all of E1, E2, and E3 are less than TOL" .
使用更新后的 while
条件,读作 "continue while at least one of E1, E2, or E3 are greater than (or equal to) TOL"。