This Java code is a simple program that reads an integer input from the user, and then compares it to the integer value 20 using an if else
statement.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner someObj = new Scanner(System.in);
System.out.println("-----------------------------");
System.out.println("Enter number to compare to 20");
int testNumber = someObj.nextInt();
if (testNumber < 20) {
System.out.println("testNumber is smaller than 20");
} else if (testNumber > 20) {
System.out.println("testNumber is greater than 20");
} else {
System.out.println("Equal Numbers");
}
}
}
Here's a block-by-block explanation of the code:
import java.util.Scanner;
public class Main {
The import
statement imports the Scanner
class from the java.util
package, which is used to read user input from the console. The Main
class is declared, which contains the main
method that serves as the entry point of the program.
public static void main(String[] args) {
Scanner someObj = new Scanner(System.in);
The main
method creates a new instance of the Scanner
class, which is used to read input from the console.
System.out.println("-----------------------------");
System.out.println("Enter number to compare to 20");
int testNumber = someObj.nextInt();
Two strings are printed to the console using the System.out.println
method to prompt the user to enter an integer value. The nextInt()
method of the Scanner
class is then used to read the user input as an integer and store it in the variable testNumber
.
if (testNumber < 20) {
System.out.println("testNumber is smaller than 20");
} else if (testNumber > 20) {
System.out.println("testNumber is greater than 20");
} else {
System.out.println("Equal Numbers");
}
An if else
statement is used to compare the value of testNumber
to the integer value 20.
If testNumber
is less than 20, the first block of code is executed, which prints a message to the console indicating that testNumber
is smaller than 20.
If testNumber
is greater than 20, the second block of code is executed, which prints a message to the console indicating that testNumber
is greater than 20.
If testNumber
is equal to 20, the third block of code is executed, which prints a message to the console indicating that the two numbers are equal.
Our program demonstrates how to read input from the console using the Scanner
class, and how to use an if else
statement to perform conditional branching based on the value of a variable.
User Input
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner someObj = new Scanner(System.in);
System.out.println("------------------");
System.out.println("Number: ");
int testNumber = someObj.nextInt();
System.out.println("Compare To: ");
int compareTo = someObj.nextInt();
if (testNumber < compareTo) {
System.out.println(testNumber + " is smaller than " + compareTo);
} else if (testNumber > compareTo) {
System.out.println(testNumber + " is greater than " + compareTo);
} else {
System.out.println("Equal Numbers");
}
}
}
This is a Java program that takes input from the user using the Scanner class and compares two integers using if-else statements. The program prompts the user to enter two integers, testNumber
and compareTo
, and then compares the two numbers to determine whether testNumber
is less than, greater than, or equal to compareTo
.
The program begins by importing the Scanner
class and creating a new instance of the class with the variable someObj
.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner someObj = new Scanner(System.in);
The program then prompts the user to enter an integer value for testNumber
and reads the value from the console using the nextInt()
method of the Scanner
class.
System.out.println("------------------");
System.out.println("Number: ");
int testNumber = someObj.nextInt();
The program then prompts the user to enter a second integer value for compareTo
and reads the value from the console using the nextInt()
method of the Scanner
class.
System.out.println("Compare To: ");
int compareTo = someObj.nextInt();
The program then compares the two integer values using if-else statements.
If testNumber
is less than compareTo
, the program prints a message indicating that testNumber
is smaller than compareTo
.
If testNumber
is greater than compareTo
, the program prints a message indicating that testNumber
is greater than compareTo
.
Otherwise, if testNumber
is equal to compareTo
, the program prints a message indicating that the two numbers are equal.
if (testNumber < compareTo) {
System.out.println(testNumber + " is smaller than " + compareTo);
} else if (testNumber > compareTo) {
System.out.println(testNumber + " is greater than " + compareTo);
} else {
System.out.println("Equal Numbers");
}
Our program demonstrates how to use if-else statements to compare two integer values and print messages based on the result of the comparison.
No comments:
Post a Comment