java if-else语句
我是一名新生,目前正在尝试使用java if-else语句 我已经参加了我的课程,我需要说,如果这段代码在所有4个错误(错误11、错误12、错误13和错误14)中得到0个错误,我需要显示文本“答案”。 这段代码在没有if-else语句的情况下运行,这两行代码中有两个错误。 请告诉我怎么做java if-else语句,java,Java,我是一名新生,目前正在尝试使用java if-else语句 我已经参加了我的课程,我需要说,如果这段代码在所有4个错误(错误11、错误12、错误13和错误14)中得到0个错误,我需要显示文本“答案”。 这段代码在没有if-else语句的情况下运行,这两行代码中有两个错误。 请告诉我怎么做 public static void deltaR() { int x; int x11, x12, x13, x14; int x21, x22, x23, x24; //inputs dou
public static void deltaR()
{
int x;
int x11, x12, x13, x14;
int x21, x22, x23, x24; //inputs
double w10, w11, w12;//weights for first neuron
int d11, d12, d13, d14;//desired output for first neuron
double net11, net12, net13, net14;//sum of weights times inputs
int y11, y12, y13, y14;//outputs
int error11, error12, error13, error14;//error
//double w20, w21, w22;//weights for second neuron
//int d21, d22, d23, d24;//desired output for second neuron
//double net21, net22, net23, net24;//sum of weights times input
//int y21, y22, y23, y24;//outputs
//int error21, error22, error23, error24;//error
if (error11 = 0, error12 = 0, error13 = 0, error14 = 0)
{
System.out.println("Answer");
}
else if (error11 != 0, error12 != 0, error13 != 0, error14 != 0)
{
double coe=0.5;//learning coefficient
x=1;
x11=0;
x12=0;
x13=1;
x14=1;
x21=0;
x22=1;
x23=0;
x24=1;
d11= 0;
d12= 1;
d13= 0;
d14= 1;
w10=0.5;
w11=-1;
w12=1.5;
net11=(x*w10 + x11*w11 + x21*w12);
net12=(x*w10 + x12*w11 + x22*w12);
net13=(x*w10 + x13*w11 + x23*w12);
net14=(x*w10 + x14*w11 + x24*w12);
if (net11>=0)
y11=1;
else
y11=0;
if (net12>=0)
y12=1;
else
y12=0;
if (net13>=0)
y13=1;
else
y13=0;
if (net14>=0)
y14=1;
else
y14=0;
error11=(d11-y11);
error12=(d12-y12);
error13=(d13-y13);
error14=(d14-y14);
System.out.println("net value 1: "+net11);
System.out.println("net value 2: "+net12);
System.out.println("net value 3: "+net13);
System.out.println("net value 4: "+net14);
System.out.println("output 1: "+y11);
System.out.println("output 2: "+y12);
System.out.println("output 3: "+y13);
System.out.println("output 4: "+y14);
System.out.println("error1: "+error11);
System.out.println("error2: "+error12);
System.out.println("error3: "+error13);
System.out.println("error4: "+error14);
}
}
}
你不能那样用逗号,你需要用逗号 如果您想要
或
,请使用|
,如果您想要和
请使用&
:
// If ANY of the error variables are 0 the if clause will execute
if(error11 == 0 || error12 == 0 || error13 == 0 || error14 == 0)
// If ALL of the error variables are 0 the if clause will execute
if(error11 == 0 && error12 == 0 && error13 == 0 && error14 == 0)
您还使用赋值运算符=
,而不是相等运算符=
。你应该仔细阅读你的文章
此外,如果总是希望在if
子句不正确时运行else
子句,则无需添加第二个if
条件:
if(error11 == 0 || error12 == 0 || error13 == 0 || error14 == 0)
{
}
else
{
// Code here will run if the above `if` clause evaluates to false
}
与您的问题无关,但作为一种风格,将变量命名为
x
、y
、d
和w
(带数字或不带数字)是不好的做法-使用更多描述性名称,可读性将提高
拥有相关变量的数字表明您应该使用
你不能那样用逗号,你需要用逗号 如果您想要
或
,请使用|
,如果您想要和
请使用&
:
// If ANY of the error variables are 0 the if clause will execute
if(error11 == 0 || error12 == 0 || error13 == 0 || error14 == 0)
// If ALL of the error variables are 0 the if clause will execute
if(error11 == 0 && error12 == 0 && error13 == 0 && error14 == 0)
您还使用赋值运算符=
,而不是相等运算符=
。你应该仔细阅读你的文章
此外,如果总是希望在if
子句不正确时运行else
子句,则无需添加第二个if
条件:
if(error11 == 0 || error12 == 0 || error13 == 0 || error14 == 0)
{
}
else
{
// Code here will run if the above `if` clause evaluates to false
}
与您的问题无关,但作为一种风格,将变量命名为
x
、y
、d
和w
(带数字或不带数字)是不好的做法-使用更多描述性名称,可读性将提高
拥有相关变量的数字表明您应该使用
在
if
语句中使用逗号是不可能的。必须使用逻辑运算符:&
表示和
,以及|
表示或
如果不可能在语句中使用逗号。必须使用逻辑运算符:&
表示和
,以及|
表示或
检查多个条件时,需要指定是AND条件(&&&
)还是or条件(|
)
|
将在遇到true
表达式时停止计算布尔表达式,并返回true
,否则返回false
。例如:
if (userIsMale || userIsFemale) { /* user is male OR female */ }
if (userIsHuman && userIsMale) { /* user is human AND is male */ }
&&
将在遇到false
表达式时停止计算布尔表达式,并返回false
,否则返回true
。例如:
if (userIsMale || userIsFemale) { /* user is male OR female */ }
if (userIsHuman && userIsMale) { /* user is human AND is male */ }
检查多个条件时,需要指定是AND条件(&&&
)还是or条件(|
)
|
将在遇到true
表达式时停止计算布尔表达式,并返回true
,否则返回false
。例如:
if (userIsMale || userIsFemale) { /* user is male OR female */ }
if (userIsHuman && userIsMale) { /* user is human AND is male */ }
&&
将在遇到false
表达式时停止计算布尔表达式,并返回false
,否则返回true
。例如:
if (userIsMale || userIsFemale) { /* user is male OR female */ }
if (userIsHuman && userIsMale) { /* user is human AND is male */ }
要测试您似乎想要测试的内容,最简单的方法是:
if ((error11 | error 12 | error13 | error 14) == 0) { //all are zero
//...do stuff
} else if ((error11 & error12 & error13 & error14) != 0) { //all are not zero
//...do different stuff
}
这将使用整数位运算
但是,您的代码似乎缺少这种情况,因为有些错误为零,有些则不是——或者至少,这种情况不起任何作用。如果该案例不应该是空的,则可以在末尾添加一个else
,或者通过将该案例编写为else
而不是If
测试,将其加入到其他案例中。最简单的测试方法是:
if ((error11 | error 12 | error13 | error 14) == 0) { //all are zero
//...do stuff
} else if ((error11 & error12 & error13 & error14) != 0) { //all are not zero
//...do different stuff
}
这将使用整数位运算
但是,您的代码似乎缺少这种情况,因为有些错误为零,有些则不是——或者至少,这种情况不起任何作用。如果这种情况不应该是什么,可以在末尾添加带有else
的行为,或者将其作为else
而不是作为If
test写入其他情况之一,将其加入其中一种情况中。用于数学比较,可以使用以下方法
if (error11 == 0 && error12 == 0 && error13 == 0 && error14 == 0)
假设errorXX为0或1,则可以替换为以下值
if (error11 + error12 + error13 + error14 == 0)
及
变成
if (error11 * error12 * error13 * error14 == 0)
double net11=(x*w10 + x11*w11 + x21*w12);
System.out.println("error1: "+ (net11 >=0 ? -1 : 0));
但是,应该使用布尔值,而不是使用int表示真/假。这就是它的目的。e、 g
if (!(error11 || error12 || error13 || error14))
您可以简化代码,使之更简单
d11= 0;
net11=(x*w10 + x11*w11 + x21*w12);
if (net11>=0)
y11=1;
else
y11=0;
error11=(d11-y11);
System.out.println("error1: "+error11);
变成
if (error11 * error12 * error13 * error14 == 0)
double net11=(x*w10 + x11*w11 + x21*w12);
System.out.println("error1: "+ (net11 >=0 ? -1 : 0));
对于数学比较,您可以使用以下内容
if (error11 == 0 && error12 == 0 && error13 == 0 && error14 == 0)
假设errorXX为0或1,则可以替换为以下值
if (error11 + error12 + error13 + error14 == 0)
及
变成
if (error11 * error12 * error13 * error14 == 0)
double net11=(x*w10 + x11*w11 + x21*w12);
System.out.println("error1: "+ (net11 >=0 ? -1 : 0));
但是,应该使用布尔值,而不是使用int表示真/假。这就是它的目的。e、 g
if (!(error11 || error12 || error13 || error14))
您可以简化代码,使之更简单
d11= 0;
net11=(x*w10 + x11*w11 + x21*w12);
if (net11>=0)
y11=1;
else
y11=0;
error11=(d11-y11);
System.out.println("error1: "+error11);
变成
if (error11 * error12 * error13 * error14 == 0)
double net11=(x*w10 + x11*w11 + x21*w12);
System.out.println("error1: "+ (net11 >=0 ? -1 : 0));
了解逻辑运算。。它们是或(| |)和(&&)等于(==)和不等于(!)了解逻辑运算。。它们是或(| |)和(&&&)等于(==)和不等于(!)您是否希望使用==而不是=?@Kumu-如果,则终止代码>在右大括号后(error14==0)代码>)。这是不正确的语法。删除该
。您是否希望使用==而不是=?@Kumu-如果使用终止代码>在右大括号后(error14==0)代码>)。这是不正确的语法。删除该代码>。感谢所有的答案,现在我将其更改为以下if(error11==0&&error1