nested if statedments, what am i doing wrong

261 views Asked by At

homework question is-----Write code that tests the variable x to determine whether it is greater than 0. If x is greater than 0, the code should test the variable y to determine whether it is less than 20. If y is less than 20, the code should assign 1 to the variable z. If y is not less than 20, the code should assign 0 to the variable z.

What i have at the moment is


import javax.swing.JOptionPane;
public class jjjd {

    public static void main(String[] args) {

    int x=0;
    String input;

    input=JOptionPane.showInputDialog("Enter a number for x");
    x=Integer.parseInt(input);


    if (x>0)
        if (y<20)
        {   (z==1);
    }   

    else
    {  
        z==0;
    }


    }




}
}

-------------------------------------------- EDIT import javax.swing.JOptionPane; public class jjjd {

public static void main(String[] args) {

int x=0;
String input;

input=JOptionPane.showInputDialog("Enter a number for x");
x=Integer.parseInt(input);


if (x>0) {
    if (y<20)
    {(z=1);}
}


else
{

    z=0;
}   



}

}

thats my new code!

the error im getting is the (z=0) under the else is "not a statement"

2

There are 2 answers

0
Toby Blanchard On

To assign a value to a variable you use = not ==

0
thatsimplekid On

You have misused your braces ({}). You need to make sure that you close all braces after opening them or the java compiler will return an error.

Also make sure to use '=' for assignment and '==' for checking variables.

Hope this helps!

import javax.swing.JOptionPane;
public class jjjd {
    public static void main(String[] args) {
        int x=0;
        int y=0;
        int z=0;
        String input;

        input=JOptionPane.showInputDialog("Enter a number for x");
        x=Integer.parseInt(input);

        if (x>0) {
            if (y<20) {
                z=1;
            }
        } else {  
            z=0;
        }
    }
}

EDIT - OP you haven't created the variable 'z' or even 'y'. Make sure to use 'int z=0;' and 'int y=0;' at the top of your code with the 'int x=0;' I have updated my code to show this