Beginner at Java: I need to figure out how to print a number which is odd and negative and fits in a formula.

I am really new to java and wanted to know how I can print an int which is both negative and odd and if so, needs to be put into the formula (n^2+1) . I am not looking particularly for answers (although I would appreciate them greatly) I just generally want to better my knowledge of java. Thanks in advanced.

Re: Beginner at Java: I need to figure out how to print a number which is odd and negative and fits in a formula.

Quote:

how I can print an int

Can you use the System.out.println() method to print it?

Re: Beginner at Java: I need to figure out how to print a number which is odd and negative and fits in a formula.

Re: Beginner at Java: I need to figure out how to print a number which is odd and negative and fits in a formula.

That answers the question. Did you have any other questions?

Re: Beginner at Java: I need to figure out how to print a number which is odd and negative and fits in a formula.

I was wondering how I could go about printing a number both negative and odd using "if" statements and (I'm assuming but not entirely sure) boolean?

Re: Beginner at Java: I need to figure out how to print a number which is odd and negative and fits in a formula.

Are you asking how to determine if a number is odd and negative?

A test for even or odd is to compare the results of theNumber % 2 : 1 is odd, 0 is even

A test for negative is if theNumber < 0

Re: Beginner at Java: I need to figure out how to print a number which is odd and negative and fits in a formula.

Thank you I will try this and come back

--- Update ---

That was helpful (i now understand the modulus operator) but I am getting problems in my code where it is not converting to boolean. See I'm trying to get the code to run with two requirements but I can't seem to do that. This is my code for the int so far:

public static void main(String[] args){

int n=11;

if (n<0){

if (n%2);

System.out.println(n^2+1));

}

Re: Beginner at Java: I need to figure out how to print a number which is odd and negative and fits in a formula.

Quote:

I am getting problems

Please copy the full text of the error messages and paste it here.

Please edit your post and wrap your code with

[code=java]

<YOUR CODE HERE>

[/code]

to get highlighting and preserve formatting.

Re: Beginner at Java: I need to figure out how to print a number which is odd and negative and fits in a formula.

Sure! Hold on...

Code java:

package variables;
public class SimpleCalc {
public static void main(String[] args){
int n=11;
if (n<0){
if (n%2); //Error Message occurs here: Mismatch - cannot convert to boolean
System.out.println(n^2+(n/2));
}
if (n>=0) { //The ones after this are fine
System.out.println(n^3*(n-1));
}
if (n>=0) {
System.out.println(n^-1*(3*n));
}
}
}

Re: Beginner at Java: I need to figure out how to print a number which is odd and negative and fits in a formula.

The expression in the if statement must use a boolean operator that returns a boolean result. % returns a numeric value.

See the tutorial for a list of operators that return a boolean result:

Equality, Relational, and Conditional Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)

The if statement requires a boolean value, not a numeric value.

a == b is a boolean value

a + b is a numeric value

(a + b) > 3 is a boolean value