51.2k views
4 votes
analyze the following code //enter an integer scanner input = newScanner(System.in); int number = input.nextInt(); if (number <= 0) Syste.out.println(number);

1 Answer

4 votes

Answer:

To analyze this code properly lets give each line a number

  1. Scanner input = new Scanner(System.in);
  2. int number = input.nextInt();
  3. if (number <= 0)
  4. System.out.println(number);

Step-by-step explanation:

  • In line one, an object of the Scanner class is created with an instance variable called input. The Scanner allows for the receiving of user input from the keyboard
  • In line two, an int variable number is created and assigned to the next integer received from the keyboard using the nextInt() method of class Scanner.
  • In line three is an if statement to check if the number entered at the keyboard is less or equal to zero
  • Line 4 outputs the number if the condition in line 3 is true.
User Troubadour
by
5.1k points