public class TestNumbers{ public static void main(String[] args) { // Get a random number from -10 up to 10. int number = (int) (Math.random() * 21) - 10; System.out.println("The number is " + number); // is it positive? if (number > 0) { System.out.println(number + " is positive!"); } // is it 0? if (number == 0) { System.out.println(number + " is zero!"); } // is it negative? // Add another if statement }}
Question
public class TestNumbers{ public static void main(String[] args) { // Get a random number from -10 up to 10. int number = (int) (Math.random() * 21) - 10; System.out.println("The number is " + number); // is it positive? if (number > 0) { System.out.println(number + " is positive!"); } // is it 0? if (number == 0) { System.out.println(number + " is zero!"); } // is it negative? // Add another if statement }}
Solution
Sure, here is the missing part of the code:
// is it negative?
if (number < 0)
{
System.out.println(number + " is negative!");
}
This code checks if the number is less than 0, and if it is, it prints out that the number is negative.
Similar Questions
Write a program to find whether a given integer is positive or negative.Sample Input 1:Enter the number:45Sample Output 1:45 is a positive numberSample Input 2: Enter the number:-364Sample Output 2:-364 is a negative numberScanner sc = new Scanner(System.in);public static void main(String[] args) {import java.util.*;public class Main {if(num>=0)System.out.println(num+" is a negative number");elseSystem.out.println("Enter the number:");int num=sc.nextInt(); }}System.out.println(num+" is a positive number");
Determine output of the following program.public class Test{ public static void main(String args[]){ System.out.println( Math.floor( Math.random( ) ) ) ; }}1.010.00.00.5
public class Hello{ public static void main(String args[]) { int num = 190; String rev_num = " " ; while (num!=0){ int digit = num%10; rev_num = rev_num+digit; num = num /= 10 ; } System.out.println((Integer.parseInt(rev_num)));} } Exception in thread "main" java.lang.NumberFormatException: For input string: " 091" at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67) at java.base/java.lang.Integer.parseInt(Integer.java:654) at java.base/java.lang.Integer.parseInt(Integer.java:786)
Write a program to check whether a number is positive or negative. Take an integer, num, as an input from the console using input() function and print the result to the console as shown in the sample test case. Sample Input and Output 1: num: 02 positive Sample Input and Output 2: num: -52142 negative Sample Input and Output 3: num: 0 zero
class Test { public static void main(String args[]) { System.out.println(fun()); } static int fun() { static int x= 0; return ++x; }}
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.