使用Scanner类的方法从用户那里读取一个数字。检查给定数字是大于,小于还是等于0。如果大于,则给定数字为正,如果较小的给定数字为负。其他给定的数字既不是正数也不是负数。
import java.util.Scanner; public class PositiveOrNegative { public static void main(String args[]){ int num; System.out.println("Enter a number ::"); Scanner sc = new Scanner(System.in); num = sc.nextInt(); if (num > 0){ System.out.println("Given number is a positive integer"); } else if(num < 0){ System.out.println("Given number is a negative integer"); } else { System.out.println("Given number is neither positive nor negative integer"); } } }
Enter a number :: 55 Given number is a positive integer
Enter a number :: -88 Given number is a negative integer