Object-Oriented Programming with Java, part I – Exercise 14

This program asks for user input and then prints a statement saying whether the number was positive or not.

My Code

import java.util.Scanner;

public class PositiveValue {

    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);

        // Type your program here:
        System.out.println("Type a number: ");
        int num = Integer.parseInt(reader.nextLine());

        if (num > 0) {
            System.out.println("The number is positive.");
        } else {
            System.out.println("The number is not positive.");
        }

    }
}

Model Code

import java.util.Scanner;

 

public class PositiveValue {

 

    public static void main(String[] args) {

        Scanner reader = new Scanner(System.in);

 

        // Type your program here:

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

        int luku = Integer.parseInt(reader.nextLine());

 

        System.out.println(""); 

        if (luku > 0) {

            System.out.println("The Number is positive.");

        } else {

            System.out.println("The Number is not positive.");

        }

    }

}

Leave a comment