Below is a simple program to compare two numbers using Java. It is the solution to Deitel’s Java How to Program (10th Edition) Chapter 2 Exercise 2.16.
 
Question: Write an application that asks the user to enter two integers, obtains them from the user and displays the larger number followed by the words “is larger”. If the numbers are equal, print the message “These numbers are equal”.

Comparing Integers in Java
Sample program run
 

2 Comments

  1. Yes you can use else if but the instruction in the textbook says "use programming techniques you have learned so far". That means, even though the problem can be solved using another and probably better method, do not use it unless it has been taught in the previous or present chapter.
    In this case, the else if selection statement have not been taught in chapter two of the textbook.

Write A Comment