java if else statement
I am a new student who is trying to use java if else statements at the moment.
I have attched my class and i need to say if this code get 0 errors for all 4 errors(error11, error12, error13 and error14), need to display the text "Answer". This code was working without th开发者_开发知识库e if else statements and there are 2 errors in those 2 lines. Please explain me how to do it?
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);
}
}
}
You can't use commas like that - you need to use boolean logical operators.
If you want or
, use ||
, if you want and
use &&
:
// 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)
You are also using the assignment operator =
instead of the equality operator ==
. You should read up on your operators.
Additionally, if you always want the else
clause to run when the if
clause is incorrect, you do not need to add the second if
conditional:
if(error11 == 0 || error12 == 0 || error13 == 0 || error14 == 0)
{
}
else
{
// Code here will run if the above `if` clause evaluates to false
}
Not related to your question, but as a matter of style, naming your variables x
, y
, d
and w
(with the numbers or without) is bad practice - use more descriptive names and readability will increase.
Having numbers for related variables suggests that you should be using arrays.
int[] inputs = new int[9]; // inputs is an array of 9 integers
When checking multiple conditions, you need to specify if it's an AND condition (&&
) or an OR condition (||
).
||
will stop evaluating the boolean expression once it encounters a true
expression, and return true
, or return false
otherwise. For example:
if (userIsMale || userIsFemale) { /* user is male OR female */ }
&&
will stop evaluating the boolean expression once it encounters a false
expression, and return false
, or return true
otherwise. For example:
if (userIsHuman && userIsMale) { /* user is human AND is male */ }
For mathematical comparisons you can use the following.
if (error11 == 0 && error12 == 0 && error13 == 0 && error14 == 0)
can be replaced with the following assuming errorXX is 0 or 1.
if (error11 + error12 + error13 + error14 == 0)
and
if (error11 == 0 || error12 == 0 || error13 == 0 || error14 == 0)
becomes
if (error11 * error12 * error13 * error14 == 0)
However, instead of using an int for true/false you should use a boolean. Thats what its for. e.g.
if (!(error11 || error12 || error13 || error14))
You can simplify the code so.
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);
becomes
double net11=(x*w10 + x11*w11 + x21*w12);
System.out.println("error1: "+ (net11 >=0 ? -1 : 0));
Using commas in if
statements is impossible. You must use logical operators: &&
which means and
, and ||
which means or
.
The easiest way to test for what it seems you want to test is:
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
}
this makes use of the integer bitwise operations.
However, your code seems to be missing the case for some errors are zero, some are not - or at least, that case does nothing. If that case shouldn't be nothing, either add it's behavior with an else
on the end or join it into one of the other cases by writing that case as an else
instead of as an if
test.
learn about logical operation.. they are OR (||) AND (&&) EQUAL TO (==) and NOT (!)
精彩评论