-2

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"

  • 10
    Count. Your. Braces! `{ (z==1);`??? Note that your question tells us **nothing** about what's wrong with this code. In the future, and now, if you have problem with code not compiling or not working, please post all error messages or take some time to describe your problem. Your efforts would be greatly appreciated. – Hovercraft Full Of Eels Nov 10 '14 at 23:28
  • 3
    Try = instead of == for assignment. – Sjlver Nov 10 '14 at 23:29

2 Answers2

1

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

0

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

Toby Blanchard
  • 216
  • 1
  • 5