192k views
3 votes
Create a conditional expression that evaluates to string "negative" if userVal is less than 0, and "non-negative" otherwise. Ex: If userVal is -9, out

User Megubyte
by
5.3k points

1 Answer

3 votes

Answer:

import java.util.Scanner;

public class Main

{

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.print("Enter a number: ");

int userVal = input.nextInt();

String aString;

if(userVal < 0)

aString = "negative";

else

aString = "non-negative";

System.out.println(aString);

}

}

Step-by-step explanation:

Ask the user to enter a number and set it to userVal

Check the value of userVal. If it is smaller than 0, set the string as "negative". If it is not, set it as "non-negative"

Print the string

User Pfctgeorge
by
5.0k points